You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python 3.10/3.8: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms #94692
Expected behavior: Passing None to shutil.rmtree's path argument should not yield an exception when ignore_errors=True.
Behavior on MacOS Python 3.10 (MacPorts) - Correct behavior:
>>> import shutil
>>> shutil.rmtree(None, ignore_errors=True)
>>> shutil.rmtree(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shutil.py", line 712, in rmtree
onerror(os.lstat, path, sys.exc_info())
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shutil.py", line 710, in rmtree
orig_st = os.lstat(path)
TypeError: lstat: path should be string, bytes or os.PathLike, not NoneType
The above occurs on RedHat Linux 8 Python 3.8 and Python 3.10 as well.
Behavior on Windows (Incorrect/differs from MacOS/Linux):
>>> import shutil
>>> shutil.rmtree(None, ignore_errors=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python310\lib\shutil.py", line 733, in rmtree
onerror(os.lstat, path, sys.exc_info())
File "C:\Python310\lib\shutil.py", line 577, in rmtree
orig_st = os.lstat(path)
TypeError: lstat: path should be string, bytes or os.PathLike, not NoneType
>>> shutil.rmtree(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python310\lib\shutil.py", line 733, in rmtree
onerror(os.lstat, path, sys.exc_info())
File "C:\Python310\lib\shutil.py", line 577, in rmtree
orig_st = os.lstat(path)
TypeError: lstat: path should be string, bytes or os.PathLike, not NoneType
Environment
CPython Versions Tested: 3.8.x, 3.10.x
Operating Systems: Windows 10 21H2, MacOS, RedHat Linux 8 (x86-64)
changed the title [-]shutil.rmtree(None) with ignore_errors behaves differently [/-][+]shutil.rmtree(None) with ignore_errors behaves differently between platforms[/+]on Jul 8, 2022
changed the title [-]shutil.rmtree(None) with ignore_errors behaves differently between platforms[/-][+]shutil.rmtree(None) with ignore_errors behaves differently between Windows and *nix platforms[/+]on Jul 8, 2022
changed the title [-]shutil.rmtree(None) with ignore_errors behaves differently between Windows and *nix platforms[/-][+]shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/+]on Jul 8, 2022
changed the title [-]shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/-][+]Python 3.10: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/+]on Jul 8, 2022
changed the title [-]Python 3.10: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/-][+]Python 3.10/3.8: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/+]on Jul 8, 2022
Yeah, it's unclear if maybe macOS raises some other non-OSError exception. It would take a lot of research, and more knowledge of macOS than I'll ever have. So I suspect the best we could do is document this and leave it be. Unless @RamseyK has some specific case where this is causing a real problem, I don't think we'll make a change here.
Functions in the os module such as os.lstat() and os.open() are supposed to raise OSError for errors due to system calls. The exact errno (or winerror) values may vary across platforms. A Python runtime error such as TypeError, ValueError (e.g. embedded null in a string), or ArithmeticError (e.g. fd value exceeds INT_MAX) is a script logic error, not a "failed removal" at the filesystem level.
Thanks for the clarification @ericvsmith@eryksun . I think documenting specifically that ignore_errors does not trap all exceptions as I (and others on my team) had naively assumed a different behavior of ignore_errors. Perhaps except Exception@hauntsaninja pointed out should be except OSError so the behavior is a bit more consistent across platforms.
Since this isn't a new regression, it is properly worked around by people who noticed, and by those who didn't, it's a behavioral change. Let's treat as feature.
Activity
[-]shutil.rmtree(None) with ignore_errors behaves differently [/-][+]shutil.rmtree(None) with ignore_errors behaves differently between platforms[/+][-]shutil.rmtree(None) with ignore_errors behaves differently between platforms[/-][+]shutil.rmtree(None) with ignore_errors behaves differently between Windows and *nix platforms[/+][-]shutil.rmtree(None) with ignore_errors behaves differently between Windows and *nix platforms[/-][+]shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/+][-]shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/-][+]Python 3.10: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/+][-]Python 3.10: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/-][+]Python 3.10/3.8: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/+]ericvsmith commentedon Jul 8, 2022
I disagree with this. The intent isn't to hide all errors, but rather filesystem errors.
The only change here might be to not ignore the TypeError on macOS.
hauntsaninja commentedon Jul 8, 2022
+1 to Eric. It's conceivable that these
except Exceptionshould be made intoexcept OSError:cpython/Lib/shutil.py
Lines 721 to 727 in 4bed0db
except Exceptionwas intentional.ericvsmith commentedon Jul 8, 2022
Yeah, it's unclear if maybe macOS raises some other non-
OSErrorexception. It would take a lot of research, and more knowledge of macOS than I'll ever have. So I suspect the best we could do is document this and leave it be. Unless @RamseyK has some specific case where this is causing a real problem, I don't think we'll make a change here.eryksun commentedon Jul 10, 2022
Functions in the
osmodule such asos.lstat()andos.open()are supposed to raiseOSErrorfor errors due to system calls. The exacterrno(orwinerror) values may vary across platforms. A Python runtime error such asTypeError,ValueError(e.g. embedded null in a string), orArithmeticError(e.g. fd value exceedsINT_MAX) is a script logic error, not a "failed removal" at the filesystem level.RamseyK commentedon Jul 10, 2022
Thanks for the clarification @ericvsmith @eryksun . I think documenting specifically that ignore_errors does not trap all exceptions as I (and others on my team) had naively assumed a different behavior of ignore_errors. Perhaps
except Exception@hauntsaninja pointed out should beexcept OSErrorso the behavior is a bit more consistent across platforms.pythongh-94692: Only catch OSError in shutil.rmtree()
serhiy-storchaka commentedon Dec 5, 2023
I am wondering whether this change should be considered a bug fix (and backported) or a new feature (and only applied to main).
ambv commentedon Dec 5, 2023
Since this isn't a new regression, it is properly worked around by people who noticed, and by those who didn't, it's a behavioral change. Let's treat as feature.
gh-94692: Only catch OSError in shutil.rmtree() (#112756)
serhiy-storchaka commentedon Dec 5, 2023
Then perhaps add
versionchangedand tweak the vague documentation?Add a versionchanged directive for pythongh-94692
Add a versionchanged directive for gh-94692 (GH-112846)
pythongh-94692: Only catch OSError in shutil.rmtree() (python#112756)
Add a versionchanged directive for pythongh-94692 (pythonGH-112846)
pythongh-94692: Only catch OSError in shutil.rmtree() (python#112756)
Add a versionchanged directive for pythongh-94692 (pythonGH-112846)