FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-41395: use context manager to close filetype objects by amiremohamadi · Pull Request #21702 · python/cpython · GitHub

/ cpython Public

bpo-41395: use context manager to close filetype objects - #21702

Closed
amiremohamadi wants to merge 8 commits into
python:mainfrom
amiremohamadi:bpo-41395-close-file
Closed

bpo-41395: use context manager to close filetype objects#21702
amiremohamadi wants to merge 8 commits into
python:mainfrom
amiremohamadi:bpo-41395-close-file

Conversation

amiremohamadi commented Aug 2, 2020
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Contributor

Use context manager to close filetype objects and avoid ResourceWarnings.

https://bugs.python.org/issue41395

amiremohamadi changed the title bpo 41395: use context manager to close filetype objects bpo-41395: use context manager to close filetype objects Aug 2, 2020
Comment thread Lib/pickle.py Outdated

kk456852 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Your code is impressive!

keep going!

Comment thread Lib/pickle.py Outdated
Comment thread Lib/pickletools.py Outdated

Copy link
Copy Markdown
Contributor Author

I think Azure Pipeline fail is not related to my changes. is it possible to run tests again?

Copy link
Copy Markdown
Member

Close and reopen to rerun tests.

gvanrossum closed this Aug 3, 2020
gvanrossum reopened this Aug 3, 2020
Comment thread Lib/pickletools.py
dis(f, args.output, memo, args.indentlevel, annotate)
with args.output:
for f in args.pickle_file:
with f:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think this could be improved as if there is an error while processing some files, the files that have not been already processed will not be closed. By using contextlib.ExitStack() here we could make sure that the files would always be closed even if some error occurs as we would register them all before starting processing them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Sounds good!
Just for making sure, you mean sth like this?

with contextlib.ExitStack() as stack:                            
    output = stack.enter_context(args.output)                    
    infiles = [stack.enter_context(f) for f in args.pickle_file] 
    for f in infiles:                                            
        preamble = args.preamble.format(name=f.name)
        output.write(preamble + '\n')
        dis(f, output, memo, args.indentlevel, annotate)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

What about just using try/finally? Looks simpler to me:

            try:
                for f in args.pickle_file:
                    preamble = args.preamble.format(name=f.name)
                    args.output.write(preamble + '\n')
                    dis(f, args.output, memo, args.indentlevel, annotate)
            finally:
                args.output.close()
                for f in args.pickle_file:
                    f.close()

Copy link
Copy Markdown
Contributor Author

@facundobatista ,@remilapeyre , @serhiy-storchaka

as you know, argparse module tries to open the FileType object and if any error occurs it raises an exception. here's the code from Lib/argparse.py:

try:                                                                 
    return open(string, self._mode, self._bufsize, self._encoding, self._errors)
except OSError as e:
    args = {'filename': string, 'error': e}
    message = _("can't open '%(filename)s': %(error)s")
    raise ArgumentTypeError(message % args)

So when we run pickletools like this:

python -Wall Lib/pickletools.py file1 chertfile file2 

If chertfile doesn't exist, it raises the exception and we get ResourceWarning because file1 opened before and never closed.

Any idea? Should it be handled in argparse module?

Copy link
Copy Markdown
Member

Oh, mmm... so there are multiple exit/crashing points after argparse opened the file. It's impossible/too hard to cover them all outside argparse.

Probably the best way to fix this is in argparse itself, maybe it's as easy as using atexit.register(just_opened_file.close) there (which will behave correctly even if the file is manually closed before).

That way, also, this issue will not be solved only for pickle, but for everybody using argparse.

+1 from me to fix this in argparse, then.

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It goes in the right direction, but it cannot overcome the main innate defect of FileType. It leaves the files open if Python quits or fails before it has a chance to handle the opened files. For example, when pass option -t in the pickletools CLI. Or pass only the output file argument without input file arguments.

#113618 manages files more explicitly and does not have such issues.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants


Back | FazBrowse Home | New Git URL