[issue47143] Add functools.copy_class() which updates closures

STINNER Victor report at bugs.python.org
Mon Mar 28 11:41:35 EDT 2022


STINNER Victor <vstinner at python.org> added the comment:

The pickle module doesn't copy a type but gets it from its module. The Python implementation is pickle._Pickler.save_type() which calls pickle._Pickler.save_global().

The cloudpickle module doesn't copy types neither: same behavior than pickle.

Example:
---
import pickle
import pickletools

class A:
    pass

data = pickle.dumps(A)
pickletools.dis(data)
---

Output:
---
    0: \x80 PROTO      4
    2: \x95 FRAME      18
   11: \x8c SHORT_BINUNICODE '__main__'
   21: \x94 MEMOIZE    (as 0)
   22: \x8c SHORT_BINUNICODE 'A'
   25: \x94 MEMOIZE    (as 1)
   26: \x93 STACK_GLOBAL
   27: \x94 MEMOIZE    (as 2)
   28: .    STOP
highest protocol among opcodes = 4
---

In short, it's implemented as:

    getattr(__import__('__main__'), 'A')

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue47143>
_______________________________________


More information about the Python-bugs-list mailing list