[New-bugs-announce] [issue30275] pickle doesn't work in compile/exec

Jeff Zhang report at bugs.python.org
Thu May 4 20:20:05 EDT 2017


New submission from Jeff Zhang:

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

----------
components: Library (Lib)
messages: 293032
nosy: Jeff Zhang
priority: normal
severity: normal
status: open
title: pickle doesn't work in compile/exec
type: enhancement
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30275>
_______________________________________


More information about the New-bugs-announce mailing list