-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
Ensure file is closed promptly in case of error #35587
Conversation
copied the milestone from #35567. feel free to amend as appropriate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a whatsnew note in 1.2, bug fix section for I/O
Added the What's New note. |
pandas/io/sas/sas_xport.py
Outdated
self._read_header() | ||
try: | ||
self._read_header() | ||
except: # noqa: E722 OK because exception re-raised |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never use bare exceptions, use
try:
....
except Exception:
# close on error reading header...
....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can do that, but what about errors that don't inherit from Exception?
I note that the PEP says:
A good rule of thumb is to limit use of bare 'except' clauses to two cases:
If the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred.
If the code needs to do some cleanup work, but then lets the exception propagate upwards with raise. try...finally can be a better way to handle this case.
(try...finally obviously isn't applicable here since we only want to close on error)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simply use Exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I note that exceptions that don't inherit from Exception are supposed to be system-exiting, so we can suppose some higher-level process will clean up for us in this case.
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
I'm fine with this implementation, but curious: wouldnt the idiomatic way to do this be with a contextmanager? |
reader.close() | ||
return data | ||
try: | ||
return reader.read() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this needed that you are catching error (and closing) at the lower level? maybe only catch here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The close at the lower level (sas7bdat.py, sas_xport.py) is to match with the get_filepath_or_buffer call 10 lines earlier, and also with various calls to close scatter around those files.
It is possible (if both 'lower level' file are only ever used via sasreader.py) that all close calls are unnecessary lower down, but I don't think they do any harm so I would prefer to leave the code symmetrical for the moment (open/close) and potentially get rid of all the close calls in a separate PR.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok fair enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @rxxg happy to take a followup to see if you can remove the lower level try/excepts
reader.close() | ||
return data | ||
try: | ||
return reader.read() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok fair enough
Fixes #35566. Replaces #35567.