[issue43657] shutil.rmtree fails on readonly files in Windows, onerror not called

2021-03-29 Thread Eryk Sun


Eryk Sun  added the comment:

> The doc on rmtree states 
> Exceptions raised by onerror will not be caught.
> Does this mean I can't use try/exept inside of onerro

rmtree() does not call onerror() in a try/except statement. An exception raised 
in onerror() will propagate to the scope that called rmtree().

The documentation has an example onerror() handler for Windows readonly files:

import os, stat
import shutil

def remove_readonly(func, path, _):
"Clear the readonly bit and reattempt the removal"
os.chmod(path, stat.S_IWRITE)
func(path)

shutil.rmtree(directory, onerror=remove_readonly)

I'd check whether the exception and function are expected values. For example:

import os, stat
import shutil

def remove_readonly(func, path, exc_info):
"Clear the readonly bit and reattempt the removal"
# ERROR_ACCESS_DENIED = 5
if func not in (os.unlink, os.rmdir) or exc_info[1].winerror != 5:
raise exc_info[1]
os.chmod(path, stat.S_IWRITE)
func(path)

shutil.rmtree(directory, onerror=remove_readonly)

--
nosy: +eryksun
type: crash -> behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43657] shutil.rmtree fails on readonly files in Windows, onerror not called

2021-03-29 Thread Walter White


Change by Walter White :


--
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43657] shutil.rmtree fails on readonly files in Windows, onerror not called

2021-03-29 Thread Walter White


Walter White  added the comment:

I tried to add a try/except that handles a PermissionError to
my onerror function. But that did not work.

The doc on rmtree states 

Exceptions raised by onerror will not be caught.

Does this mean I can't use try/exept inside of onerror?

--
status: closed -> open

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43657] shutil.rmtree fails on readonly files in Windows, onerror not called

2021-03-29 Thread Walter White


Change by Walter White :


--
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43657] shutil.rmtree fails on readonly files in Windows, onerror not called

2021-03-29 Thread Walter White


Walter White  added the comment:

Just saw:

class PermissionError(OSError)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43657] shutil.rmtree fails on readonly files in Windows, onerror not called

2021-03-29 Thread Walter White


New submission from Walter White :

shutil.rmtree fails on readonly files in Windows.

Usually people are using the onerror callback to handle file permissions and 
retry, but that is not possible in this case because it is not triggerd.

onerror is only triggered if a OSError is found. 
In my case the unlink throws a PermissionError

Code shutil.rmdir():

try:
os.unlink(fullname)
except OSError:
onerror(os.unlink, fullname, sys.exc_info())


Traceback:


Traceback (most recent call last):

  File "c:\Users\user\test.py", line 121, in 
shutil.rmtree(shutil.rmtree(working_dir),
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 740, in rmtree
return _rmtree_unsafe(path, onerror)
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 613, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 613, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 618, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 616, in _rmtree_unsafe
os.unlink(fullname)

PermissionError: [WinError 5] Access denied: 'C:\\Users\\user\\somefile.txt'

os.stat:

st_mode=33060
st_ino=34621422136837665
st_dev=3929268297
st_nlink=1
st_uid=0
st_gid=0

--
components: Library (Lib)
messages: 389697
nosy: homerun4711
priority: normal
severity: normal
status: open
title: shutil.rmtree fails on readonly files in Windows, onerror not called
type: crash
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com