Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorator fix #28

Merged
merged 4 commits into from
Apr 1, 2014
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions dill/dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,22 +404,24 @@ def _dict_from_dictproxy(dictproxy):
_dict.pop('__weakref__', None)
return _dict

def _import_module(import_name):
if '.' in import_name:
items = import_name.split('.')
module = '.'.join(items[:-1])
obj = items[-1]
else:
return __import__(import_name)
return getattr(__import__(module, None, None, [obj]), obj)
def _import_module(import_name, safe=True):
try:
if '.' in import_name:
items = import_name.split('.')
module = '.'.join(items[:-1])
obj = items[-1]
else:
return __import__(import_name)
return getattr(__import__(module, None, None, [obj]), obj)
except (ImportError, AttributeError):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be safer to have Exception here

if safe:
return None
raise

def _locate_function(obj, session=False):
if obj.__module__ == '__main__': # and session:
return False
try:
found = _import_module(obj.__module__ + '.' + obj.__name__)
except:
return False
found = _import_module(obj.__module__ + '.' + obj.__name__)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be further simplified by return obj is _import_module(obj.__module__ + '.' + obj.__name__)

return found is obj

@register(CodeType)
Expand Down Expand Up @@ -459,6 +461,12 @@ def save_module_dict(pickler, obj):
pickler.write(bytes('c__main__\n__dict__\n', 'UTF-8'))
else:
pickler.write('c__main__\n__dict__\n') #XXX: works in general?
elif '__name__' in obj and obj != _main_module.__dict__ \
and obj is getattr(_import_module(obj['__name__'], safe=True), '__dict__', None):
if PYTHON3:
pickler.write(bytes('c%s\n__dict__\n' % obj['__name__'], 'UTF-8'))
else:
pickler.write('c%s\n__dict__\n' % obj['__name__'])
else:
log.info("D2: <dict%s" % str(obj.__repr__).split('dict')[-1]) # obj
StockPickler.save_dict(pickler, obj)
Expand Down