Skip to content

pickling files works with protocol=2. #43263

Closed

Description

@kirillsimonov
BPO 1474680
Nosy @pitrou, @avassalotti, @merwok
Files
  • disable_file_pickling-py2.diff
  • 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-08-05.07:20:40.957>
    created_at = <Date 2006-04-22.16:20:51.000>
    labels = ['type-bug', 'library']
    title = 'pickling files works with protocol=2.'
    updated_at = <Date 2010-08-05.07:20:40.955>
    user = 'https://bugs.python.org/kirillsimonov'

    bugs.python.org fields:

    activity = <Date 2010-08-05.07:20:40.955>
    actor = 'alexandre.vassalotti'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-08-05.07:20:40.957>
    closer = 'alexandre.vassalotti'
    components = ['Library (Lib)']
    creation = <Date 2006-04-22.16:20:51.000>
    creator = 'kirill_simonov'
    dependencies = []
    files = ['14378']
    hgrepos = []
    issue_num = 1474680
    keywords = ['patch']
    message_count = 5.0
    messages = ['60906', '89783', '89829', '105404', '112951']
    nosy_count = 5.0
    nosy_names = ['kirill_simonov', 'pitrou', 'alexandre.vassalotti', 'schmir', 'eric.araujo']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1474680'
    versions = ['Python 2.7']

    Activity

    1. kirillsimonov commented on Apr 22, 2006

      kirillsimonovmannequin
      MannequinAuthor
      Compare:
      >>> import pickle
      >>> pickle.dumps(file('/etc/passwd'))
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "/usr/lib/python2.4/pickle.py", line 1386, in dumps
          Pickler(file, protocol, bin).dump(obj)
        File "/usr/lib/python2.4/pickle.py", line 231, in dump
          self.save(obj)
        File "/usr/lib/python2.4/pickle.py", line 313, in save
          rv = reduce(self.proto)
        File "/usr/lib/python2.4/copy_reg.py", line 69, in
      _reduce_ex
          raise TypeError, "can't pickle %s objects" %
      base.__name__
      TypeError: can't pickle file objects
      
      with
      >>> pickle.dumps(file('/etc/passwd'), protocol=2)
      '\x80\x02c__builtin__\nfile\nq\x00)\x81q\x01.'

      Similarly, the __reduce__ method works for basic
      objects like str, int or dict with protocol=2, but
      doesn't work with protocol=1:

      >>> (1).__reduce_ex__(1)
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "/usr/lib/python2.4/copy_reg.py", line 69, in
      _reduce_ex
          raise TypeError, "can't pickle %s objects" %
      base.__name__
      TypeError: can't pickle int objects
      >>> (1).__reduce_ex__(2)
      (<function __newobj__ at 0xb7e5117c>, (<type 'int'>,
      1), None, None, None)
    2. added
      stdlib
      on Apr 22, 2006
    3. avassalotti commented on Jun 28, 2009

      @avassalotti
      Member

      That seems easy to fix by adding a dummy __reduce__ method to file. My
      only worry is this could break file subclasses which may have ad-hoc
      mechanisms implemented for pickling files.

    4. added
      type-bug
      on Jun 28, 2009
    5. pitrou commented on Jun 29, 2009

      @pitrou
      Member

      Pickling a file is tricky. You can't just pickle the construction
      parameters, you also need to save (and restore) the whole buffering
      state. I'm not sure a half-working pickle solution would make a good
      service to the user.

    6. merwok commented on May 9, 2010

      @merwok
      Member

      Antoine, I think the goal here is to disable pickling, not making it work.

      Alexandre, is it ok for a class to define pickle magic methods just to raise an exception? Doesn’t it break expectations?

      One question hasn’t been answered: When did the protocol change? Was it intentional or is it a bug?

    7. avassalotti commented on Aug 5, 2010

      @avassalotti
      Member

      Although it is tempting to fix this, it feels to much like a feature. I am closing as won't fix because we don't add new features to 2.x.

    8. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions