You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect pickling behavior in the c implemented classmethod descriptors #95196
While it passes without raising any errors, classmethod_descriptor differs from unpickled_classmethod_descriptor. Instead of the original descriptor, the pickling process yields the function itself:
This behavior differs from python implemented classmethods:
import pickle
class A:
@classmethod
def method(cls):
pass
classmethod_descriptor = A.__dict__["method"]
# Any of these lines will raise "TypeError: cannot pickle 'classmethod' object"
pickle.dumps(classmethod_descriptor, protocol=5)
classmethod_descriptor.__reduce_ex__(5)
Since it fails with normal classmethods, I would expect it to fail with their c counterparts as well so their behavior would match.
Either that, or having both of them succeed during the pickling process, in which case, __reduce_ex__ should return a valid result in both cases.
The bug occurs with other c implemented class methods as well:
Since this feature never worked correctly, and pickling of Python implemented classmethod descriptors and C and Python implemented staticmethod descriptors is not supported, and there are no common use cases for this (otherwise we would get such report years ago), and the correct implementation is not trivial if not use eval(), I thing that the best way to fix this is to disable it. There are no existing tests for this, and it looks that this code was added by mistake and was never tested.
Activity
UltimateLobster commentedon Aug 23, 2022
#96162
Opened a PR to solve the issue
serhiy-storchaka commentedon Aug 29, 2022
Since this feature never worked correctly, and pickling of Python implemented classmethod descriptors and C and Python implemented staticmethod descriptors is not supported, and there are no common use cases for this (otherwise we would get such report years ago), and the correct implementation is not trivial if not use
eval(), I thing that the best way to fix this is to disable it. There are no existing tests for this, and it looks that this code was added by mistake and was never tested.pythongh-95196: Disable incorrect pickling of the C implemented class…
gh-95196: Disable incorrect pickling of the C implemented classmethod…
pythongh-95196: Disable incorrect pickling of the C implemented class…
pythongh-95196: Disable incorrect pickling of the C implemented class…
gh-95196: Disable incorrect pickling of the C implemented classmethod…
gh-95196: Disable incorrect pickling of the C implemented classmethod…
Merge branch 'main' into dictwatch
pythongh-95196: Disable incorrect pickling of the C implemented class…
Move builtin classmethod_descriptor to a different test