Skip to content

Commit

Permalink
support NEWOBJ_EX
Browse files Browse the repository at this point in the history
  • Loading branch information
ewfian committed Apr 16, 2023
1 parent 8fa9362 commit b32a08f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/objex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class MyClass:
def __getnewargs_ex__(self):
return ('arg1', 345), {'base': 16, "test": True}

import pickle

x = MyClass()
x.abc = 666

filehandler = open(b"objex.pkl", "wb")
pickle.dump(x, filehandler)
11 changes: 11 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ export class Parser {
stack.push(obj);
break;
}
case OP.NEWOBJ_EX: {
const kwargs = stack.pop();
const args = stack.pop();
const cls = stack.pop();
const obj = this.newObject(cls, ...args);
if (obj.__setnewargs_ex__) {
obj.__setnewargs_ex__(kwargs);
}
stack.push(obj);
break;
}
case OP.PERSID:
stack.push(this.persistent_load(reader.line()));
break;
Expand Down
8 changes: 8 additions & 0 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export class Registry extends Map<string, new (...args: any[]) => any> {
} as unknown as new (...args: any[]) => any;
PObject.prototype.module = module;
PObject.prototype.name = name;
PObject.prototype.__setnewargs_ex__ = function (kwargs: any) {
Object.defineProperty(this, 'kwargs', {
value: kwargs,
enumerable: false,
configurable: false,
writable: false,
});
};
return PObject;
}
}

0 comments on commit b32a08f

Please sign in to comment.