-
-
Notifications
You must be signed in to change notification settings - Fork 12.2k
array_tofile does not close file when PyArray_ToFile fails #17589
Description
When an open file is passed to numpy.save and the save fails, then file is then undeletable because numpy still has the file open.
Reproducing code example:
Firstly create a small partition F that has < 200Mb of space
import numpy as np
import os
a = np.zeros((100000000, 3), dtype=float)
filename = 'F:\\array.npy'
try:
with open(filename, 'wb') as f:
np.save(f, a)
except Exception as e:
if os.path.exists(filename):
os.remove(filename) # This failsError message:
Traceback (most recent call last):
File "S:/scripts/numpy_bug.py", line 9, in
np.save(f, a)
File "<array_function internals>", line 6, in save
File "S:\Leapfrog\components\py3_win64\lib\site-packages\numpy\lib\npyio.py", line 553, in save
pickle_kwargs=pickle_kwargs)
File "S:\Leapfrog\components\py3_win64\lib\site-packages\numpy\lib\format.py", line 687, in write_array
array.tofile(fp)
OSError: 300000000 requested and 0 written # We expect this
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\JetBrains\PyCharm 2020.1\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2020.1\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "S:/scripts/numpy_bug.py", line 12, in
os.remove(filename)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'F:\array.npy'
NumPy/Python version information:
1.18.1 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)]
1.19.2 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)]