Skip to content

pickle and pickletools cli interface doesn't close input and output file. #85567

Description

@tirkarthi
BPO 41395
Nosy @avassalotti, @serhiy-storchaka, @corona10, @tirkarthi, @amiremohamadi, @Foxyblue, @achhina
PRs
  • bpo-41395: Close file objects in pickle and pickletools #21676
  • bpo-41395: use context manager to close filetype objects #21702
  • gh-85567: Register a cleanup function to close files for FileType objects in argparse #32257
  • 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 = None
    created_at = <Date 2020-07-25.14:12:10.580>
    labels = ['easy', 'type-bug', '3.8', '3.9', '3.10', 'library']
    title = "pickle and pickletools cli interface doesn't close input and output file."
    updated_at = <Date 2022-04-05.13:05:47.595>
    user = 'https://github.com/tirkarthi'

    bugs.python.org fields:

    activity = <Date 2022-04-05.13:05:47.595>
    actor = 'achhina'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-07-25.14:12:10.580>
    creator = 'xtreak'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41395
    keywords = ['patch', 'easy']
    message_count = 2.0
    messages = ['374269', '416776']
    nosy_count = 8.0
    nosy_names = ['alexandre.vassalotti', 'python-dev', 'serhiy.storchaka', 'corona10', 'xtreak', 'Amir', 's.williams-wynn', 'achhina']
    pr_nums = ['21676', '21702', '32257']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41395'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    Linked PRs

    Activity

    1. tirkarthi commented on Jul 25, 2020

      @tirkarthi
      MemberAuthor

      pickle and pickletools use argparse with FileType which is not automatically closed. Other cli interfaces like json [0], ast [1] use context manager to close filetype objects.

      pickle :

      'pickle_file', type=argparse.FileType('br'),

      mypickle

      >> import pickle
      >> with open("mypickle", "wb") as f: pickle.dump({"a": 1}, f)

      ./python -Wall -m pickle mypickle
      {'a': 1}
      sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='mypickle'>

      pickletools :

      cpython/Lib/pickletools.py

      Lines 2850 to 2855 in af08db7

      parser.add_argument(
      'pickle_file', type=argparse.FileType('br'),
      nargs='*', help='the pickle file')
      parser.add_argument(
      '-o', '--output', default=sys.stdout, type=argparse.FileType('w'),
      help='the file where the output should be written')

      ./python -Wall -m pickletools mypickle -o mypickle.py
      sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='mypickle'>
      sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='mypickle.py' mode='w' encoding='UTF-8'>

      [0]

      with options.infile as infile, options.outfile as outfile:

      [1]
      with args.infile as infile:

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Jul 25, 2020
    3. achhina commented on Apr 5, 2022

      achhinamannequin
      Mannequin

      Hi,

      First-time contributor here, I've made a patch in follow-up to the discussions that happened in Amir's patch in regards to this. I'd appreciate it if someone would be able to take a look and review it!

      #32257

    4. transferred this issue fromon Apr 10, 2022
    5. 9 remaining items

    6. added a commit that references this issue on Jul 5, 2022
      50208d4
    7. furkanonder commented on Nov 18, 2022

      @furkanonder
      Contributor

      @serhiy-storchaka The issue seems to be resolved in this PR.

    8. added a commit that references this issue on Jan 1, 2024
    9. serhiy-storchaka commented on Jan 1, 2024

      @serhiy-storchaka
      Member

      The pickletools CLI is especially treacherous. It leaks the files if you specify the -t argument. It leaks the output file if you only specify the output file argument. And of course it leaks the files in case of argument errors, for example specifying non-integer indent level or non-writeable output file.

      The only reliable way to handle this is to get rid of FileType and open and close files explicitly. PR #113618 does this.

    10. added a commit that references this issue on Jan 5, 2024
    11. added 2 commits that reference this issue on Jan 5, 2024
    12. added 2 commits that reference this issue on Jan 5, 2024
    13. added a commit that references this issue on Jan 22, 2024
    14. added a commit that references this issue on Feb 11, 2024
    15. added a commit that references this issue on Sep 2, 2024
    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

      3.10 (EOL)end of life3.8 (EOL)end of life3.9 (EOL)end of lifeeasystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions