-
-
Notifications
You must be signed in to change notification settings - Fork 178
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
Decorator fix #28
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4ccbdcb
Creates a safe mode for _import_module, which returns None when the m…
matsjoyce a75d424
Fixes assertion errors caused by pickling a decorator function from a…
matsjoyce 20ac58c
Change _import_module safe default to True, and simplified _locate_fu…
matsjoyce 3b05f0e
Back to safe=False for _import_module
matsjoyce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
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__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be further simplified by |
||
return found is obj | ||
|
||
@register(CodeType) | ||
|
@@ -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) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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