Skip to content

Commit 1d5feee

Browse files
committed
fix pickling of bytes subclasses
1 parent 5c37579 commit 1d5feee

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesBuiltins.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CONTAINS__;
3535
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
3636
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
37+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETNEWARGS__;
3738
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GE__;
3839
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GT__;
3940
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IMOD__;
@@ -2788,4 +2789,13 @@ protected static byte[] getBytes(PythonObjectLibrary lib, Object object) {
27882789
throw new IllegalStateException(e);
27892790
}
27902791
}
2792+
2793+
@Builtin(name = __GETNEWARGS__, minNumOfPositionalArgs = 1)
2794+
@GenerateNodeFactory
2795+
public abstract static class GetNewargsNode extends PythonUnaryBuiltinNode {
2796+
@Specialization
2797+
PTuple doBytes(PBytes self) {
2798+
return factory().createTuple(new Object[]{factory().createBytes(self.getSequenceStorage())});
2799+
}
2800+
}
27912801
}

graalpython/lib-graalpython/object.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ def _get_new_arguments(obj):
7171

7272
def reduce_newobj(obj):
7373
cls = obj.__class__
74-
if not cls.__dict__.get('__new__', None):
74+
if not hasattr(cls, '__new__'):
7575
raise TypeError("cannot pickle '{}' object".format(cls.__name__))
7676

7777
args, kwargs = _get_new_arguments(obj)
7878
import_copyreg()
7979

80+
hasargs = args is not None
81+
8082
if kwargs is None or len(kwargs) == 0:
8183
newobj = copyreg.__newobj__
8284
newargs = (cls, ) + (args if args else tuple())
@@ -93,8 +95,9 @@ def reduce_newobj(obj):
9395
try:
9496
getstate = obj.__getstate__
9597
except AttributeError:
98+
required = not hasargs and not isinstance(obj, (list, dict))
9699
itemsize = getattr(type(obj), '__itemsize__', 0)
97-
if itemsize != 0:
100+
if required and itemsize != 0:
98101
raise TypeError("cannot pickle '{}' object".format(cls.__name__))
99102

100103
state = getattr(obj, "__dict__", None)

0 commit comments

Comments
 (0)