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

Difference between pickle implementations for function objects #58544

Closed
sbt mannequin opened this issue Mar 16, 2012 · 3 comments
Closed

Difference between pickle implementations for function objects #58544

sbt mannequin opened this issue Mar 16, 2012 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sbt
Copy link
Mannequin

sbt mannequin commented Mar 16, 2012

BPO 14336
Nosy @pitrou, @avassalotti, @serhiy-storchaka

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 2021-12-10.09:48:17.789>
created_at = <Date 2012-03-16.19:03:38.764>
labels = ['type-bug', 'library']
title = 'Difference between pickle implementations for function objects'
updated_at = <Date 2021-12-10.09:48:17.789>
user = 'https://bugs.python.org/sbt'

bugs.python.org fields:

activity = <Date 2021-12-10.09:48:17.789>
actor = 'iritkatriel'
assignee = 'none'
closed = True
closed_date = <Date 2021-12-10.09:48:17.789>
closer = 'iritkatriel'
components = ['Library (Lib)']
creation = <Date 2012-03-16.19:03:38.764>
creator = 'sbt'
dependencies = []
files = []
hgrepos = []
issue_num = 14336
keywords = []
message_count = 3.0
messages = ['156069', '222234', '288152']
nosy_count = 5.0
nosy_names = ['pitrou', 'alexandre.vassalotti', 'BreamoreBoy', 'sbt', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue14336'
versions = ['Python 2.7']

@sbt
Copy link
Mannequin Author

sbt mannequin commented Mar 16, 2012

When pickling a function object, if it cannot be saved as a global the C implementation falls back to using copyreg/reduce/reduce_ex.

The comment for the changeset which added this fallback claims that it is for compatibility with the Python implementation. See

http://hg.python.org/cpython/rev/c6753db9c6af

However, the current python implementations do not have any such fallback.

This affects both 2.x and 3.x.

For example

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle, cPickle, copy_reg
>>> def f():
...   pass
...
>>> _f = f
>>> del f
>>> copy_reg.pickle(type(_f), lambda obj: (str, ("FALLBACK",)))
>>> cPickle.dumps(_f)
"c__builtin__\nstr\np1\n(S'FALLBACK'\np2\ntp3\nRp4\n."
>>> pickle.dumps(_f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "c:\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "c:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\Python27\lib\pickle.py", line 748, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <function f at 0x0299A470>: it's not found as __main__.f

I don't know what should be done. I would be tempted to always fall back to copyreg/reduce/reduce_ex when save_global fails (not just for function objects) but that might make error messages less helpful.

@sbt sbt mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 16, 2012
@BreamoreBoy
Copy link
Mannequin

BreamoreBoy mannequin commented Jul 3, 2014

I couldn't find the code referenced in the changeset in the new _pickle.c. However I did find the comment that I've pasted below so I believe this issue only applies to 2.7.

/* The old cPickle had an optimization that used switch-case statement
dispatching on the first letter of the type name. This has was removed
since benchmarks shown that this optimization was actually slowing
things down. */

@serhiy-storchaka
Copy link
Member

In 3.x the fallback was removed by 6bd1f0a27e8e. If this is 2.7 only issue I prefer to close it as "won't fix".

See also bpo-26959.

@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-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants