Skip to content

Commit 802f145

Browse files
mbyrnepr2vstinnerblurb-it[bot]
authored
gh-79459 tempfile: Raise a ValueError if the prefix or suffix contains a directory (#150477)
Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 7226870 commit 802f145

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎Lib/tempfile.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,15 @@ def _sanitize_params(prefix, suffix, dir):
119119
output_type = _infer_return_type(prefix, suffix, dir)
120120
if suffix is None:
121121
suffix = output_type()
122+
if _os.path.dirname(suffix):
123+
raise ValueError("suffix can't contain a directory component")
122124
if prefix is None:
123125
if output_type is str:
124126
prefix = template
125127
else:
126128
prefix = _os.fsencode(template)
129+
if _os.path.dirname(prefix):
130+
raise ValueError("prefix can't contain a directory component")
127131
if dir is None:
128132
if output_type is str:
129133
dir = gettempdir()

‎Lib/test/test_tempfile.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,5 +2154,46 @@ def test_cleanup_safe(self):
21542154
# platforms, but don't forget to update the docs.
21552155
self.assertTrue(tempfile._rmtree_use_dir_fd)
21562156

2157+
2158+
class TestMisc(BaseTestCase):
2159+
def test_prefix_suffix_error(self):
2160+
tests = [
2161+
f"dir{os.sep}name",
2162+
f"{os.sep}abs_name",
2163+
]
2164+
if os.altsep is not None:
2165+
tests.extend((
2166+
f"dir{os.altsep}name",
2167+
f"{os.altsep}abs_name",
2168+
))
2169+
if support.MS_WINDOWS:
2170+
tests.append('C:name')
2171+
tests.extend(tuple(os.fsencode(path) for path in tests))
2172+
2173+
PREFIX_ERR = "prefix can't contain a directory component"
2174+
SUFFIX_ERR = "suffix can't contain a directory component"
2175+
for value in tests:
2176+
with self.subTest(value):
2177+
# test prefix
2178+
with self.assertRaisesRegex(ValueError, PREFIX_ERR):
2179+
tempfile.mkstemp(prefix=value)
2180+
with self.assertRaisesRegex(ValueError, PREFIX_ERR):
2181+
tempfile.mkdtemp(prefix=value)
2182+
with self.assertRaisesRegex(ValueError, PREFIX_ERR):
2183+
tempfile.TemporaryFile(prefix=value)
2184+
with self.assertRaisesRegex(ValueError, PREFIX_ERR):
2185+
tempfile.NamedTemporaryFile(prefix=value)
2186+
2187+
# test suffix
2188+
with self.assertRaisesRegex(ValueError, SUFFIX_ERR):
2189+
tempfile.mkstemp(suffix=value)
2190+
with self.assertRaisesRegex(ValueError, SUFFIX_ERR):
2191+
tempfile.mkdtemp(suffix=value)
2192+
with self.assertRaisesRegex(ValueError, SUFFIX_ERR):
2193+
tempfile.TemporaryFile(suffix=value)
2194+
with self.assertRaisesRegex(ValueError, SUFFIX_ERR):
2195+
tempfile.NamedTemporaryFile(suffix=value)
2196+
2197+
21572198
if __name__ == "__main__":
21582199
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`tempfile` functions that take a ``prefix`` or ``suffix`` argument now raise a :exc:`ValueError` if they contain a directory component:
2+
:func:`tempfile.mkstemp`, :func:`tempfile.mkdtemp`, :func:`tempfile.TemporaryFile`, :func:`tempfile.NamedTemporaryFile`.

0 commit comments

Comments
 (0)