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

pickle doesn't work in compile/exec #74461

Closed
JeffZhang mannequin opened this issue May 5, 2017 · 2 comments
Closed

pickle doesn't work in compile/exec #74461

JeffZhang mannequin opened this issue May 5, 2017 · 2 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@JeffZhang
Copy link
Mannequin

JeffZhang mannequin commented May 5, 2017

BPO 30275
Nosy @brettcannon

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-05-05.21:20:27.135>
created_at = <Date 2017-05-05.00:20:05.496>
labels = ['invalid', 'type-feature', 'library']
title = "pickle doesn't work in compile/exec"
updated_at = <Date 2017-05-05.21:20:27.133>
user = 'https://bugs.python.org/JeffZhang'

bugs.python.org fields:

activity = <Date 2017-05-05.21:20:27.133>
actor = 'brett.cannon'
assignee = 'none'
closed = True
closed_date = <Date 2017-05-05.21:20:27.135>
closer = 'brett.cannon'
components = ['Library (Lib)']
creation = <Date 2017-05-05.00:20:05.496>
creator = 'Jeff Zhang'
dependencies = []
files = []
hgrepos = []
issue_num = 30275
keywords = []
message_count = 2.0
messages = ['293032', '293139']
nosy_count = 2.0
nosy_names = ['brett.cannon', 'Jeff Zhang']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue30275'
versions = ['Python 3.5']

@JeffZhang
Copy link
Mannequin Author

JeffZhang mannequin commented May 5, 2017

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

@JeffZhang JeffZhang mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 5, 2017
@brettcannon
Copy link
Member

It won't work without the global namespace as pickle needs to be able to figure out where the function lives (the function itself doesn't get pickled, just the name of the function).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant