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

2022-03-28 Thread Jelle Zijlstra
Jelle Zijlstra added the comment: I believe the attrs code wouldn't work if a method is decorated with a decorator that wraps the original function, such as @functools.cache. -- nosy: +JelleZijlstra ___ Python tracker

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: Note: Implementing a metaclass in Python is hard, it's easy to mess up with closures: see bpo-29270 "ctypes: fail to create a _ctypes._SimpleCData subclass using a closure like calling super() without arguments". type.__new__() is called twice on the same

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: > The same problem exists at the function level: see bpo-39805: "Copying > functions doesn't actually copy them". See also bpo-14369 "make function __closure__ writable". -- ___ Python tracker

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: bpo-32176 "Zero argument super is broken in 3.6 for methods with a hacked __class__ cell" added test_code.test_closure_injection() and fixed the CO_NOFREE flag in the code object constructor (types.CodeType). --

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: > If I understand correctly, a cell content can be modified since Python 3.7: > since commit 64505a1f6c0af4574e17e823b27ffe24eca44df5 of bpo-30486 Moreover, it's possible to create a cell object since Python 3.8, commit

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: See also the types.new_class() function: https://docs.python.org/dev/library/types.html#types.new_class Oh, I didn't know this function! -- ___ Python tracker

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: > * old fixed attrs issue: https://github.com/python-attrs/attrs/issues/102 > * attrs issue with Python 3.11: > https://github.com/python-attrs/attrs/issues/907 > * dataclasses issues with slots=True: https://bugs.python.org/issue46404 Similar bug without

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: The same problem exists at the function level: see bpo-39805: "Copying functions doesn't actually copy them". For example, copy.deepcopy(func) returns func unchanged if it's a function. Example: --- import copy def make_closure(): closure = [] def

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: In the Python C API, PEP 384 added PyType_FromSpec(). There is also PyStructSequence_NewType(). PEP 3121 proposed PyType_Copy() but it was never implemented: see bpo-3760. But in C, closures are implemented using a module state, or previously using a global

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: If I understand correctly, a cell content can be modified since Python 3.7: since commit 64505a1f6c0af4574e17e823b27ffe24eca44df5 of bpo-30486: bpo-30486: Allow setting cell value (#1840) Antoine Pitrou created bpo-30486 for cloudpickle: "There are use

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: More recent copy.copy() change: commit 5c1c3b4f197c57952760be37d77d73669284a607 of bpo-11480: Issue #11480: Fixed copy.copy to work with classes with custom metaclasses. +try: +issc = issubclass(cls, type) +except TypeError: # cls is not

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: pickle.dump(x) checks if x is a type since commit f048a8f6d79173cc1da1bf12c60ae06fea36762c (March 2002) of bpo-494904: Pickler.save(): Fix for SF bug #494904: Cannot pickle a class with a metaclass, reported by Dan Parisien. +if

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

2022-03-28 Thread STINNER Victor
STINNER Victor 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:

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

2022-03-28 Thread STINNER Victor
STINNER Victor added the comment: It seems like the copy module doesn't support copying a class. copy.deepcopy(cls) doesn't copy a class but returns its argument, the class unchanged. -- ___ Python tracker

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

2022-03-28 Thread Dong-hee Na
Change by Dong-hee Na : -- nosy: +corona10 ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

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

2022-03-28 Thread STINNER Victor
New submission from STINNER Victor : Class decorarators of attrs and stdlib dataclasses modules have to copy a class to *add* slots: * old fixed attrs issue: https://github.com/python-attrs/attrs/issues/102 * attrs issue with Python 3.11: https://github.com/python-attrs/attrs/issues/907 *