mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-06 05:20:09 +03:00
secure tempdir creation and deletion, fixes race condition
This commit is contained in:
parent
84cfe379d3
commit
1b091e27a2
|
@ -1,4 +1,5 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
import tempfile
|
||||||
|
|
||||||
# require that deprecation warnings are triggered
|
# require that deprecation warnings are triggered
|
||||||
import warnings
|
import warnings
|
||||||
|
@ -18,6 +19,7 @@ py3 = (sys.version_info >= (3, 0))
|
||||||
|
|
||||||
_target = None
|
_target = None
|
||||||
_tempfiles = []
|
_tempfiles = []
|
||||||
|
_temproot = tempfile.mkdtemp(prefix='pillow-tests')
|
||||||
_logfile = None
|
_logfile = None
|
||||||
|
|
||||||
|
|
||||||
|
@ -230,19 +232,14 @@ def tempfile(template, *extra):
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
|
||||||
files = []
|
files = []
|
||||||
root = os.path.join(tempfile.gettempdir(), 'pillow-tests')
|
|
||||||
try:
|
|
||||||
os.mkdir(root)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
for temp in (template,) + extra:
|
for temp in (template,) + extra:
|
||||||
assert temp[:5] in ("temp.", "temp_")
|
assert temp[:5] in ("temp.", "temp_")
|
||||||
name = os.path.basename(sys.argv[0])
|
name = os.path.basename(sys.argv[0])
|
||||||
name = temp[:4] + os.path.splitext(name)[0][4:]
|
name = temp[:4] + os.path.splitext(name)[0][4:]
|
||||||
name = name + "_%d_%d" % (os.getpid(), len(_tempfiles)) + temp[4:]
|
name = name + "_%d_%d" % (os.getpid(), len(_tempfiles)) + temp[4:]
|
||||||
name = os.path.join(root, name)
|
name = os.path.join(_temproot, name)
|
||||||
files.append(name)
|
files.append(name)
|
||||||
_tempfiles.extend(files)
|
_tempfiles.extend(files)
|
||||||
return files[0]
|
return files[0]
|
||||||
|
@ -319,9 +316,8 @@ def _setup():
|
||||||
os.remove(file)
|
os.remove(file)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass # report?
|
pass # report?
|
||||||
temp_root = os.path.join(tempfile.gettempdir(), 'pillow-tests')
|
|
||||||
try:
|
try:
|
||||||
os.rmdir(temp_root)
|
os.rmdir(_temproot)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user