From b54a5f26fa87a6859aa259baad01595cfd810479 2013-12-17 22:18:47 From: MinRK Date: 2013-12-17 22:18:47 Subject: [PATCH] make @interactive decorator friendlier with dill sets the decorated function globals to main, in addition to just the module name. This helps dill treat it as if it were defined locally, and the function actually behaves the same locally as it does on engines. --- diff --git a/IPython/parallel/util.py b/IPython/parallel/util.py index f914953..a688d1d 100644 --- a/IPython/parallel/util.py +++ b/IPython/parallel/util.py @@ -27,6 +27,7 @@ try: from signal import SIGKILL except ImportError: SIGKILL=None +from types import FunctionType try: import cPickle @@ -221,8 +222,16 @@ def interactive(f): This results in the function being linked to the user_ns as globals() instead of the module globals(). """ - f.__module__ = '__main__' - return f + mainmod = __import__('__main__') + + # build new FunctionType, so it can have the right globals + # interactive functions never have closures, that's kind of the point + f2 = FunctionType(f.__code__, mainmod.__dict__, + f.__name__, f.__defaults__, + ) + # associate with __main__ for uncanning + f2.__module__ = '__main__' + return f2 @interactive def _push(**ns):