summaryrefslogtreecommitdiff
path: root/Cython/Compiler/ParseTreeTransforms.py
diff options
context:
space:
mode:
authorRobert Bradshaw <robertwb@gmail.com>2017-05-15 16:39:49 -0700
committerRobert Bradshaw <robertwb@gmail.com>2017-05-15 16:39:49 -0700
commit2002ddfb4a9056c531d4b2fbda654d97bd2e92cf (patch)
tree649cc29efbb7cb444bbf738d21309baa3c5af241 /Cython/Compiler/ParseTreeTransforms.py
parentd33f4844e01b2382b7548ad6ac12cdadc2f6c91c (diff)
downloadcython-2002ddfb4a9056c531d4b2fbda654d97bd2e92cf.tar.gz
Handle Python subclasses of Cdef classes.
Diffstat (limited to 'Cython/Compiler/ParseTreeTransforms.py')
-rw-r--r--Cython/Compiler/ParseTreeTransforms.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py
index c2e6f8d2f..ed4d92f9f 100644
--- a/Cython/Compiler/ParseTreeTransforms.py
+++ b/Cython/Compiler/ParseTreeTransforms.py
@@ -1603,10 +1603,17 @@ if VALUE is not None:
unpickle_func_name = '__pyx_unpickle_%s' % node.class_name
unpickle_func = TreeFragment(u"""
- def %(unpickle_func_name)s(%(args)s):
+ def %(unpickle_func_name)s(__pyx_type, __pyx_state, %(args)s):
cdef %(class_name)s result
- result = %(class_name)s.__new__(%(class_name)s)
+ result = %(class_name)s.__new__(__pyx_type)
%(assignments)s
+ if hasattr(result, '__setstate__'):
+ result.__setstate__(__pyx_state)
+ elif hasattr(result, '__dict__'):
+ result.__dict__.update(__pyx_state)
+ elif __pyx_state is not None:
+ from pickle import PickleError
+ raise PickleError("Unexpected state: %%s" %% __pyx_state)
return result
""" % {
'unpickle_func_name': unpickle_func_name,
@@ -1620,7 +1627,13 @@ if VALUE is not None:
pickle_func = TreeFragment(u"""
def __reduce__(self):
- return %s, (%s)
+ if hasattr(self, '__getstate__'):
+ state = self.__getstate__()
+ elif hasattr(self, '__dict__'):
+ state = self.__dict__
+ else:
+ state = None
+ return %s, (type(self), state, %s)
""" % (unpickle_func_name, ', '.join('self.%s' % v for v in all_members_names)),
level='c_class', pipeline=[NormalizeTree(None)]).substitute({})
pickle_func.analyse_declarations(node.scope)