Skip to content

Commit

Permalink
wip: working solution?
Browse files Browse the repository at this point in the history
  • Loading branch information
emfdavid committed Aug 30, 2021
1 parent 4bdc5df commit 770c5a0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
7 changes: 6 additions & 1 deletion dill/_dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def _trace(boolean):

stack = dict() # record of 'recursion-sensitive' pickled objects

import abc

import os
import sys
diff = None
Expand Down Expand Up @@ -1398,12 +1400,15 @@ def save_type(pickler, obj):
elif obj.__module__ == '__main__':
if issubclass(type(obj), type):
# try: # used when pickling the class as code (or the interpreter)
if is_dill(pickler, child=True) and not pickler._byref:
if is_dill(pickler, child=True) and not pickler._byref and not issubclass(type(obj), abc.ABCMeta):
# thanks to Tom Stepleton pointing out pickler._session unneeded
_t = 'T2'
log.info("%s: %s" % (_t, obj))
_dict = _dict_from_dictproxy(obj.__dict__)
# except: # punt to StockPickler (pickle by class reference)
if issubclass(type(obj), abc.ABCMeta):
StockPickler.save_type(pickler, obj)
return
else:
log.info("T5: %s" % obj)
name = getattr(obj, '__qualname__', getattr(obj, '__name__', None))
Expand Down
30 changes: 27 additions & 3 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@
#extend(False)
#import cloudpickle as pickle

from abc import ABC, abstractmethod


class OneTwoThree(ABC):

@abstractmethod
def foo(self):
pass

class EasyAsAbc(OneTwoThree):

def foo(self):
print('bar')

# helper objects
class _class:
def _method(self):
Expand All @@ -33,13 +47,17 @@ def _method(self):
special['LambdaType'] = _lambda = lambda x: lambda y: x
special['MethodType'] = _method = _class()._method
special['UnboundMethodType'] = _class._method
special['EasyAsABC'] = EasyAsAbc
special['easy_as_abc'] = EasyAsAbc()
special['OneTwoThree'] = OneTwoThree
objects.update(special)

def pickles(name, exact=False):
"""quick check if object pickles with dill"""
obj = objects[name]
try:
pik = pickle.loads(pickle.dumps(obj))
res = pickle.dumps(obj)
pik = pickle.loads(res)
if exact:
try:
assert pik == obj
Expand All @@ -49,8 +67,13 @@ def pickles(name, exact=False):
else:
assert type(obj) == type(pik)
except Exception:
print ("fails: %s %s" % (name, type(obj)))
print("fails: %s %s" % (name, type(obj)))
raise

def test_special():
pickles("EasyAsABC", exact=False)
pickles("OneTwoThree", exact=False)
pickles("easy_as_abc", exact=False)

def test_objects():
for member in objects.keys():
Expand All @@ -59,4 +82,5 @@ def test_objects():


if __name__ == '__main__':
test_objects()
# test_objects()
test_lambda()

0 comments on commit 770c5a0

Please sign in to comment.