-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
tempfile.TemporaryDirectory() context manager can fail to propagate exceptions generated within its context #85034
Comments
When an exception occurs within a tempfile.TemporaryDirectory() context ---------------------------------------------------------------------------- import tempfile
import os
import sys try: except ArithmeticError as exc:
print('An arithmetic error occurred: {}'.format(exc))
print('Continuing...') is effective at catching any ArithmeticError which occurs in the For example: import tempfile
import os
import sys try: except ArithmeticError as exc:
print('An arithmetic error occurred: {}'.format(exc))
print('Continuing...') produces this: /tmp/tmp_r2sxqgb but this: import tempfile
import os
import sys try: except ArithmeticError as exc:
print('An arithmetic error occurred: {}'.format(exc))
print('Continuing...') produces this: /tmp/tmp_yz6zyfs
Traceback (most recent call last):
File "tempfilebug.py", line 9, in <module>
n = 1 / 0
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tempfilebug.py", line 9, in <module>
n = 1 / 0
File "/usr/lib/python3.6/tempfile.py", line 948, in __exit__
self.cleanup()
File "/usr/lib/python3.6/tempfile.py", line 952, in cleanup
_rmtree(self.name)
File "/usr/lib/python3.6/shutil.py", line 477, in rmtree
onerror(os.lstat, path, sys.exc_info())
File "/usr/lib/python3.6/shutil.py", line 475, in rmtree
orig_st = os.lstat(path)
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmp_yz6zyfs' and the program exits with the top-level code having no chance to catch My view is that if an exception happens within a TemporaryDirectory ---------------------------------------------------------------------------- $ python3 --version
Python 3.6.9
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic |
Too late for 3.7 now, closing. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: