-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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 seems to treat a file as a directory when processing an exception in the onerror() #87319
Comments
>>> import sys, tempfile, os
>>> sys.version
'3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]'
>>> folder = tempfile.TemporaryDirectory()
>>> file = open(os.path.join(folder.name, "example.txt"), 'wb')
>>> folder.cleanup()
Traceback (most recent call last):
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 616, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 32] Процесс не может получить доступ к файлу, так как этот файл занят другим процессом: 'C:\\Users\\user0\\AppData\\Local\\Temp\\tmpin8xeb9b\\example.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\tempfile.py", line 801, in onerror
_os.unlink(path)
PermissionError: [WinError 32] Процесс не может получить доступ к файлу, так как этот файл занят другим процессом: 'C:\\Users\\user0\\AppData\\Local\\Temp\\tmpin8xeb9b\\example.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\tempfile.py", line 830, in cleanup
self._rmtree(self.name)
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\tempfile.py", line 812, in _rmtree
_shutil.rmtree(name, onerror=onerror)
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 740, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 618, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\tempfile.py", line 804, in onerror
cls._rmtree(path)
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\tempfile.py", line 812, in _rmtree
_shutil.rmtree(name, onerror=onerror)
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 740, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 599, in _rmtree_unsafe
onerror(os.scandir, path, sys.exc_info())
File "C:\Users\user0\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 596, in _rmtree_unsafe
with os.scandir(path) as scandir_it:
NotADirectoryError: [WinError 267] Неверно задано имя папки: 'C:\\Users\\user0\\AppData\\Local\\Temp\\tmpin8xeb9b\\example.txt' |
This is a duplicate of https://bugs.python.org/issue43219 . |
My last comment was wrong, the issue I linked is unrelated. I think the actual issue here is the code here: Line 826 in 4b97d97
If This can be confusing when debugging and can lead to a bug if user's code tries to catch a PermissionError it expects but instead gets NotADirectory error. A solution is probably to check if path is a file and error is PermissionError and if ignore_errors=False, re-raise it instead of calling rmtree(). If ignore_errors=True, return instead of calling rmtree(). I don't have windows so can't test the OP code with this approach. Serhiy: adding you since you wrote |
I agree with Andrei. I'll test this approach on Windows. |
Hi Team, Do you have any news about an eventual patch for this? Was it solved in later Python versions? I am running Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)], on a Windows 10 64-bit machine, with 8 GB of RAM and an SSD drive. No filesystem compression. I am handling large data objects, coming from a scientific package. I am simply trying to serialize them on disk, for future use. Smaller files serialize correctly using pickle, marshal, joblib and dill libraries. However larger files (from approx. 200 MB up, in terms of their size on the disk) cause one and the same Error all the time, as listed below. I will try also HDF (hdf5) and serpy serialization, but at this point I am sure it is simply related to large file size. The error is identical to the one observed by You 1-2 years ago here. IMHO system tries to use temporary data in local Temp folder and something goes wrong. E.g. it attempts to unlink a file, it checks folder - yet he thinks the filename is a folder name. Or something similar. It is quite important for me, related to my day job... Is there something I could do myself, or test? I plan to dig in the <shutil.py> library, but maybe You already made some tests...
|
I just ran in to working a separate |
Is there any workaround for this issue? |
Fixed in 3.11-3.13. |
…unction() (pythonGH-112791) (cherry picked from commit ba18893) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
PermissionError
withNotADirectoryError
during tempdirectory cleanup #29940Note: 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:
Linked PRs
The text was updated successfully, but these errors were encountered: