GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Pickle ignores custom getstate methods on TextIOWrapper in Python 3.12 · Issue #122559 · python/cpython

Pickle ignores custom getstate methods on TextIOWrapper in Python 3.12 #122559

New issue
Copy link
New issue
Copy link
Closed
Closed
Pickle ignores custom getstate methods on TextIOWrapper in Python 3.12#122559
Copy link
Assignees
serhiy-storchaka
Labels
topic-IOtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@SimonSorgQC
SimonSorgQC
opened on Aug 1, 2024
Issue body actions

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

import pickle
from io import BytesIO, TextIOWrapper

class EncodedFile(TextIOWrapper):
	def __getstate__(self):
		return "string"
	def __setstate__(self, state):
		pass

file = EncodedFile(BytesIO(b"string"))
pickle.dumps(file)

This works in Python 3.11 and 3.10, but fails in 3.12 with

pickle.dumps(file)
TypeError: cannot pickle 'EncodedFile' instances

CPython versions tested on:

3.10, 3.11, 3.12

Operating systems tested on:

macOS

Linked PRs

  • gh-122559: Synchronize C and Python implementation of the io module about pickling #122628
  • [3.13] gh-122559: Synchronize C and Python implementation of the io module about pickling (GH-122628) #133381

Activity

  1. SimonSorgQC
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Aug 1, 2024
  2. picnixz
    added this to Pickle and copy issues 🥒on Aug 1, 2024
  3. tomasr8 commented on Aug 1, 2024

    @tomasr8
    tomasr8
    on Aug 1, 2024
    Member
    More actions

    Seems like it was added in this PR: https://github.com/python/cpython/pull/101948/files#diff-f6d1380274f6859688c821fced58fb1c829d8acad44ae252ba988e220e1080b3R3240

  4. SimonSorgQC commented on Aug 1, 2024

    @SimonSorgQC
    SimonSorgQC
    on Aug 1, 2024
    Author
    More actions

    Interesting! While it makes sense that the original IOBase can not be pickled, I feel like it should be possible for subclasses overriding getstate

  5. SimonSorgQC commented on Aug 1, 2024

    @SimonSorgQC
    SimonSorgQC
    on Aug 1, 2024
    Author
    More actions

    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.

  6. serhiy-storchaka commented on Aug 2, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Aug 2, 2024
    Member
    More actions

    Pickling file objects was explicitly forbidden in bpo-10180 by adding __getstate__ methods that raise TypeError. These methods were removed from the C implementation in bpo-33138, because these classes were recognized as non-pickleable by default. Then these classes were made heap types in #101948 (gh-101819), and new __reduce__ and __reduce_ex__ methods that raise TypeError were added, because condition that prevented them from pickling was gone.

    The right way is to remove __reduce__ and __reduce_ex__ methods and restore __getstate__ methods. This will make the C implementation consistent with the Python implementation.

  7. serhiy-storchaka
    self-assigned this
    on Aug 2, 2024
  8. serhiy-storchaka
    added
    topic-IO
    on Aug 2, 2024
  9. serhiy-storchaka commented on Aug 2, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Aug 2, 2024
    Member
    More actions

    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.

  10. serhiy-storchaka
    added a commit that references this issue on Aug 2, 2024

    pythongh-122559: Synchronize C and Python implementation of the io mo…

    f6e66a6
  11. serhiy-storchaka commented on Aug 2, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Aug 2, 2024
    Member
    More actions

    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 __reduce__ and __reduce_ex__ in your subclass to default:

    class EncodedFile(TextIOWrapper):
        ...
        __reduce__ = object.__reduce__
        __reduce_ex__ = object.__reduce_ex__
  12. serhiy-storchaka
    added 3 commits that reference this issue on May 4, 2025

    gh-122559: Synchronize C and Python implementation of the io module a…

    e9253eb

    [3.13] pythongh-122559: Synchronize C and Python implementation of th…

    4f0dbd0

    [3.13] gh-122559: Synchronize C and Python implementation of the io m…

    973e2d3
  13. serhiy-storchaka
    closed this as completedon May 4, 2025
  14. github-project-automation
    moved this to Done in Pickle and copy issues 🥒on May 4, 2025
  15. diegorusso
    added a commit that references this issue on May 4, 2025

    Merge remote-tracking branch 'origin/main' into call-len

    c03babc
  16. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-122559: Synchronize C and Python implementation of the io mo…

    ed858b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @serhiy-storchaka
    serhiy-storchaka

Labels

topic-IOtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

  • Pickle and copy issues 🥒
    Status
    Done
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.