Skip to content

gh-79459 tempfile: Raise a ValueError if the prefix or suffix contains a directory component.#150477

Draft
mbyrnepr2 wants to merge 2 commits into
python:mainfrom
mbyrnepr2:gh-79459_tempfile
Draft

gh-79459 tempfile: Raise a ValueError if the prefix or suffix contains a directory component.#150477
mbyrnepr2 wants to merge 2 commits into
python:mainfrom
mbyrnepr2:gh-79459_tempfile

Conversation

@mbyrnepr2
Copy link
Copy Markdown
Contributor

tempfile: Raise a ValueError if the prefix or suffix contains a directory component.

gh-79459

Raise a ``ValueError`` if the ``prefix`` or ``suffix`` contains a
directory component.

pythongh-79459
@mbyrnepr2 mbyrnepr2 changed the title tempfile: Raise a ValueError if the prefix or suffix contains a directory component. gh-79459 tempfile: Raise a ValueError if the prefix or suffix contains a directory component. May 26, 2026
Comment thread Lib/test/test_tempfile.py Outdated


class TestPrefixAndSuffix(BaseTestCase):
def test_value_error_if_prefix_or_suffix_contains_directory(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_value_error_if_prefix_or_suffix_contains_directory(self):
def test_prefix_suffix_error(self):

Comment thread Lib/test/test_tempfile.py Outdated
((os.sep), None),
(os.fsencode(os.sep), tempfile.gettempdirb()),
)
else:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that it should be exclusive. I would suggest to always test with os.sep. And if os.altsep is defined (not None), test also os.altsep.

Instead of just passing sep/altsep, I would prefer 'dir{sep}name' relative path and '{sep}abs_name' absolute path.

Comment thread Lib/test/test_tempfile.py Outdated
with self.assertRaisesRegex(ValueError, MESSAGE):
os.rmdir(tempfile.mkdtemp(dir=directory, prefix=value))
with self.assertRaisesRegex(ValueError, MESSAGE):
tempfile.NamedTemporaryFile(dir=directory, prefix=value, delete=True)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that testing delete=True is relevant here.

You should test suffix as well.

Comment thread Lib/tempfile.py Outdated
if suffix is None:
suffix = output_type()
if _os.path.dirname(suffix):
raise ValueError("'prefix' or 'suffix' can't contain a directory component")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not being more specific and only mention suffix here? And only mention prefix in the error below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. I tried too hard to write as little code as possible.

Comment thread Lib/test/test_tempfile.py
((os.altsep), None),
(os.fsencode(os.altsep), tempfile.gettempdirb()),
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should test all functions taking prefix/suffix arguments:

  • mkstemp()
  • mkdtemp()
  • NamedTemporaryFile()
  • TemporaryFile()

Write one test for prefix and a second test for the suffix.

Comment thread Lib/test/test_tempfile.py Outdated
for value, directory in data:
with self.subTest((value, directory)):
with self.assertRaisesRegex(ValueError, MESSAGE):
tempfile.mkstemp(dir=directory, prefix=value)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that it's useful to set the dir parameter.

Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants