Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OBJ and BINSTRING #82

Merged
merged 6 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions fickling/fickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,26 @@ def run(self, interpreter: Interpreter):
return objs


class Obj(Opcode):
name = "OBJ"

def run(self, interpreter: Interpreter):
args = []
while interpreter.stack:
arg = interpreter.stack.pop()
if isinstance(arg, MarkObject):
break
args.insert(0, arg)
else:
raise ValueError("Exhausted the stack while searching for a MarkObject!")
kls = args.pop(0)
# TODO Verify paths for correctness
if args or hasattr(kls, "__getinitargs__") or not isinstance(kls, type):
interpreter.stack.append(ast.Call(kls, args, []))
else:
interpreter.stack.append(ast.Call(kls, kls, []))


class ShortBinUnicode(DynamicLength, ConstantOpcode):
name = "SHORT_BINUNICODE"
priority = 5000
Expand Down Expand Up @@ -1469,6 +1489,22 @@ def validate(cls, obj):
return obj


class BinString(DynamicLength, ConstantOpcode):
name = "BINSTRING"
priority = ShortBinBytes.priority + 1
length_bytes = 4
signed = True
Boyan-MILANOV marked this conversation as resolved.
Show resolved Hide resolved

def encode_body(self) -> bytes:
return repr(self.arg).encode("utf-8")

@classmethod
def validate(cls, obj):
if not isinstance(obj, str):
raise ValueError(f"String must be instantiated from a str, not {obj!r}")
return super().validate(obj)


class BinBytes(ShortBinBytes):
name = "BINBYTES"
priority = ShortBinBytes.priority + 1
Expand Down
5 changes: 5 additions & 0 deletions test/test_crashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@ def test_pop_mark(self):
"""Tests the correctness of the POP_MARK opcode by using the bytecode from https://github.com/mindspore-ai/mindspore/issues/183
This can be simplified to allow for the correctness of additional opcodes to be tested"""
pass

@unparse_test(io.BytesIO(b'(cos\nsystem\nS"whoami"\no.'))
def test_obj(self):
"""Tests the correctness of the OBJ opcode"""
pass
Loading