Skip to content

Commit

Permalink
Lookup qualname instead of name in Python 3 (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
anivegesana committed Jun 6, 2022
1 parent 281b069 commit bc57b45
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions dill/_dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,13 +1230,35 @@ def _import_module(import_name, safe=False):
return None
raise

# https://github.com/python/cpython/blob/a8912a0f8d9eba6d502c37d522221f9933e976db/Lib/pickle.py#L322-L333
def _getattribute(obj, name):
for subpath in name.split('.'):
if subpath == '<locals>':
raise AttributeError("Can't get local attribute {!r} on {!r}"
.format(name, obj))
try:
parent = obj
obj = getattr(obj, subpath)
except AttributeError:
raise AttributeError("Can't get attribute {!r} on {!r}"
.format(name, obj))
return obj, parent

def _locate_function(obj, pickler=None):
if obj.__module__ in ['__main__', None] or \
pickler and is_dill(pickler, child=False) and pickler._session and obj.__module__ == pickler._main.__name__:
module_name = getattr(obj, '__module__', None)
if module_name in ['__main__', None] or \
pickler and is_dill(pickler, child=False) and pickler._session and module_name == pickler._main.__name__:
return False

found = _import_module(obj.__module__ + '.' + obj.__name__, safe=True)
return found is obj
if hasattr(obj, '__qualname__'):
module = _import_module(module_name, safe=True)
try:
found, _ = _getattribute(module, obj.__qualname__)
return found is obj
except:
return False
else:
found = _import_module(module_name + '.' + obj.__name__, safe=True)
return found is obj


def _setitems(dest, source):
Expand Down

0 comments on commit bc57b45

Please sign in to comment.