Title: Incorrect pickling behavior in the c implemented classmethod descriptors · Issue #95196 · python/cpython · GitHub
Open Graph Title: Incorrect pickling behavior in the c implemented classmethod descriptors · Issue #95196 · python/cpython
X Title: Incorrect pickling behavior in the c implemented classmethod descriptors · Issue #95196 · python/cpython
Description: Bug report Hello there, this is my first bug report so apologies if there's something I miss. Consider the following code: import pickle classmethod_descriptor = dict.__dict__["fromkeys"] unpickled_classmethod_descriptor = pickle.loads(p...
Open Graph Description: Bug report Hello there, this is my first bug report so apologies if there's something I miss. Consider the following code: import pickle classmethod_descriptor = dict.__dict__["fromkeys"] unpickled...
X Description: Bug report Hello there, this is my first bug report so apologies if there's something I miss. Consider the following code: import pickle classmethod_descriptor = dict.__dict__["fromkeys&qu...
Opengraph URL: https://github.com/python/cpython/issues/95196
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect pickling behavior in the c implemented classmethod descriptors","articleBody":"# Bug report\r\n\r\nHello there, this is my first bug report so apologies if there's something I miss.\r\n\r\nConsider the following code:\r\n```\r\nimport pickle\r\n\r\nclassmethod_descriptor = dict.__dict__[\"fromkeys\"]\r\nunpickled_classmethod_descriptor = pickle.loads(pickle.dumps(classmethod_descriptor, protocol=5))\r\n```\r\n\r\nWhile 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:\r\n\r\n```\r\n\u003e\u003e\u003e classmethod_descriptor\r\n\u003cmethod 'fromkeys' of 'dict' objects\u003e\r\n\r\n\u003e\u003e\u003e unpickled_classmethod_descriptor\r\n\u003cfunction dict.fromkeys(iterable, value=None, /)\u003e\r\n```\r\n\r\nManually calling the descriptor's `__reduce_ex__` allows us to see the reason:\r\n```\r\n\u003e\u003e\u003e classmethod_descriptor.__reduce_ex__(5)\r\n(\u003cfunction getattr\u003e, (dict, 'fromkeys'))\r\n```\r\nThis behavior differs from python implemented classmethods:\r\n\r\n```\r\nimport pickle\r\n\r\nclass A:\r\n @classmethod\r\n def method(cls):\r\n pass\r\n\r\nclassmethod_descriptor = A.__dict__[\"method\"]\r\n\r\n# Any of these lines will raise \"TypeError: cannot pickle 'classmethod' object\"\r\npickle.dumps(classmethod_descriptor, protocol=5)\r\nclassmethod_descriptor.__reduce_ex__(5)\r\n```\r\nSince it fails with normal classmethods, I would expect it to fail with their c counterparts as well so their behavior would match. \r\nEither that, or having both of them succeed during the pickling process, in which case, `__reduce_ex__` should return a valid result in both cases. \r\n\r\nThe bug occurs with other c implemented class methods as well:\r\n```\r\nimport pickle\r\n\r\nfrom datetime import datetime\r\n\r\nclassmethod_descriptor = datetime.__dict__[\"now\"]\r\n\r\n# Returns False\r\npickle.loads(pickle.dumps(classmethod_descriptor, 5)) == classmethod_descriptor\r\n\r\n# Returns True\r\npickle.loads(pickle.dumps(classmethod_descriptor, 5)) == datetime.now\r\n```\r\n\r\n# Your environment\r\nPython Versions:\r\nI've tested it with both 3.10.5 and 3.11.04b\r\nOS - Windows 10 Pro 64 bit\r\n\r\n\u003c!--\r\nYou can freely edit this text. Remove any lines you believe are unnecessary.\r\n--\u003e\r\n","author":{"url":"https://github.com/UltimateLobster","@type":"Person","name":"UltimateLobster"},"datePublished":"2022-07-24T09:40:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/95196/cpython/issues/95196"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:90964593-6188-a391-f070-072440df244e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E7A:28DD5C:F77329:14C19A4:696991A9 |
| html-safe-nonce | 564ae519ea3473c0cb7ba251401573402896fb10d1720602320ef307c309dfe3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTdBOjI4REQ1QzpGNzczMjk6MTRDMTlBNDo2OTY5OTFBOSIsInZpc2l0b3JfaWQiOiI1Nzc2MjA0NjgyNTYzMTM3NjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a93778caaccd3951a6e51a1ed62cf8b827b4340cbca28ff9f4430771131b1a42 |
| hovercard-subject-tag | issue:1315858592 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/95196/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8e87786e2f163d31f2a09b8ef718062eaa0df3c153030b7dcebca6d3dea3b80f/python/cpython/issues/95196 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8e87786e2f163d31f2a09b8ef718062eaa0df3c153030b7dcebca6d3dea3b80f/python/cpython/issues/95196 |
| og:image:alt | Bug report Hello there, this is my first bug report so apologies if there's something I miss. Consider the following code: import pickle classmethod_descriptor = dict.__dict__["fromkeys"] unpickled... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | UltimateLobster |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3542e147982176a7ebaa23dfb559c8af16f721c03ec560c68c56b64a0f35e751 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | af80af7cc9e3de9c336f18b208a600950a3c187c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width