This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Jeff Zhang
Recipients Jeff Zhang
Date 2017-05-05.00:20:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1493943605.77.0.767455241721.issue30275@psf.upfronthosting.co.za>
In-reply-to
Content
I want to use pickle in compile/exec, but it doesn't work for me. It only works when I use the global namespace. But I don't want to use global namespace, is there any way for that ? Thanks

>>> a = compile("def f():\n\t'hello'\nimport pickle\npickle.dumps(f)", "<stdin>", "exec")
>>> exec(a)            # works
>>> exec(a, {})        # fails  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in <module>
_pickle.PicklingError: Can't pickle <function f at 0x1050881e0>: it's not the same object as __main__.f
>>> exec(a, {'__name__': '__main__'})   # fails too
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in <module>
_pickle.PicklingError: Can't pickle <function f at 0x1050882f0>: it's not the same object as __main__.f
History
Date User Action Args
2017-05-05 00:20:06Jeff Zhangsetrecipients: + Jeff Zhang
2017-05-05 00:20:05Jeff Zhangsetmessageid: <1493943605.77.0.767455241721.issue30275@psf.upfronthosting.co.za>
2017-05-05 00:20:05Jeff Zhanglinkissue30275 messages
2017-05-05 00:20:04Jeff Zhangcreate