-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Pickle ignores custom getstate methods on TextIOWrapper in Python 3.12 #122559
Comments
Seems like it was added in this PR: https://github.com/python/cpython/pull/101948/files#diff-f6d1380274f6859688c821fced58fb1c829d8acad44ae252ba988e220e1080b3R3240 |
Interesting! While it makes sense that the original IOBase can not be pickled, I feel like it should be possible for subclasses overriding getstate |
So I can verify that overriding reduce and reduce_ex worked for me. I would leave the issue open for now as I do not think getstate and setstate not working is nice - especially since reduce and reduce_ex of IOBase are buried in the C code. Please feel free to correct me if you think this has been resolved. |
Pickling file objects was explicitly forbidden in bpo-10180 by adding The right way is to remove |
It turned out that this issue is a bit more complex. There are more differences between implementations. Classes are not tested separately, existing tests do not cover all cases. I am working on this problem. |
…dule about pickling In the C implementation, remove __reduce__ and __reduce_ex__ methods that always raise TypeError and restore __getstate__ methods that always raise TypeErrori. This restores fine details of the pre-3.12 behavior and unifies both implementations.
I added tests as a guard against such type of regression, but in general there are no guaranties about pickleability (especially with protocols 0 and 1). In meantime, the workaround is to reset class EncodedFile(TextIOWrapper):
...
__reduce__ = object.__reduce__
__reduce_ex__ = object.__reduce_ex__ |
Bug report
Bug description:
So I am not entirely sure whether this is unintended behaviour, but it is definitely a noticeable change between 3.11 and 3.12 that is rather unintuitive
This works in Python 3.11 and 3.10, but fails in 3.12 with
CPython versions tested on:
3.10, 3.11, 3.12
Operating systems tested on:
macOS
Linked PRs
The text was updated successfully, but these errors were encountered: