Skip to content
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

File objects should not pickleable #54389

Closed
pitrou opened this issue Oct 23, 2010 · 9 comments
Closed

File objects should not pickleable #54389

pitrou opened this issue Oct 23, 2010 · 9 comments
Labels
stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error

Comments

@pitrou
Copy link
Member

pitrou commented Oct 23, 2010

BPO 10180
Nosy @birkenfeld, @amauryfa, @pitrou, @avassalotti, @benjaminp, @alex
Dependencies
  • bpo-10173: Don't pickle TestCase instances in test_multiprocessing
  • Files
  • pickleio.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-11-05.21:16:14.377>
    created_at = <Date 2010-10-23.17:19:26.446>
    labels = ['type-bug', 'library', 'expert-IO']
    title = 'File objects should not pickleable'
    updated_at = <Date 2010-11-05.21:16:14.376>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2010-11-05.21:16:14.376>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-11-05.21:16:14.377>
    closer = 'pitrou'
    components = ['Library (Lib)', 'IO']
    creation = <Date 2010-10-23.17:19:26.446>
    creator = 'pitrou'
    dependencies = ['10173']
    files = ['19344']
    hgrepos = []
    issue_num = 10180
    keywords = ['patch']
    message_count = 9.0
    messages = ['119450', '119457', '119459', '119470', '119471', '119473', '119474', '119475', '120536']
    nosy_count = 6.0
    nosy_names = ['georg.brandl', 'amaury.forgeotdarc', 'pitrou', 'alexandre.vassalotti', 'benjamin.peterson', 'alex']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue10180'
    versions = ['Python 3.2']

    @pitrou
    Copy link
    Member Author

    pitrou commented Oct 23, 2010

    In Python 3, pickle accepts to serialize a file object, but the result is nonsensical when unpickled. I think we should explicitly forbid pickling of FileIO, Buffered{Reader,Writer} and TextIOWrapper objects.

    @pitrou pitrou added stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error labels Oct 23, 2010
    @birkenfeld
    Copy link
    Member

    Sounds like a good idea to me.

    @pitrou
    Copy link
    Member Author

    pitrou commented Oct 23, 2010

    Here is a patch, but bpo-10173 must probably be fixed first.

    @amauryfa
    Copy link
    Member

    The patch modifies _io.TextIOWrapper, but not _pyio.TextIOWrapper. Is there a reason?

    @pitrou
    Copy link
    Member Author

    pitrou commented Oct 23, 2010

    The patch modifies _io.TextIOWrapper, but not _pyio.TextIOWrapper. Is
    there a reason?

    Yes, two of them:

    • modifying _pyio.Buffered* is enough to trigger the TypeError
    • _pyio.StringIO inherits from TextIOWrapper, and it must be pickleable

    @alex
    Copy link
    Member

    alex commented Oct 23, 2010

    I don't see why Buffered or TextIO's shouldn't be pickleable, ISTM their pickleability should be based on what the underlying file obj is.

    @birkenfeld
    Copy link
    Member

    What would be the use case for that?

    @pitrou
    Copy link
    Member Author

    pitrou commented Oct 23, 2010

    I don't see why Buffered or TextIO's shouldn't be pickleable, ISTM
    their pickleability should be based on what the underlying file obj
    is.

    That could be. Right now, though, pickling them gives nonsensical
    results and I think it would be better to give an explicit error
    message.

    (a problem with making them pickleable is that, by exposing details of
    the internal structure in the pickle, you're limiting what
    implementation changes you can do in the future)

    @pitrou
    Copy link
    Member Author

    pitrou commented Nov 5, 2010

    Committed in r86220. I won't backport it since it would risk breaking existing code, although relying on this is really a bug in itself.

    @pitrou pitrou closed this as completed Nov 5, 2010
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants