Skip to content

gh-80490: Remove some usage of tempfile.mktemp() - #108515

Closed
Carreau wants to merge 1 commit into
python:mainfrom
Carreau:remove-mktmp
Closed

Carreau wants to merge 1 commit into
python:mainfrom
Carreau:remove-mktmp

Conversation

@Carreau

@Carreau Carreau commented Aug 26, 2023 •

Copy link
Copy Markdown
Contributor

See among other #80490, #87770

tempfile.mktemp is deprecated since 2.3 (security reason), but only in docs

As pointed out in above issues, and others there was at least one attempt to emit a deprecation warnings but it has been removed as too many places still use mktemp.

So trying to start by cleaning up the codebase of cpython from usage of mktemp, so that maybe we can reintroduce the deprecation warning later.

This tries to minimise the number of changes – I could have used a NamedTemporaryFile context manager in every tests, but it would have added a lot of noise.

@bedevere-bot bedevere-bot added tests Tests in the Lib/test dir awaiting review labels Aug 26, 2023
@Carreau Carreau changed the title Remove some usage of tempfile.mktemp() gh-80490: Remove some usage of tempfile.mktemp() Aug 26, 2023
@Carreau
Carreau force-pushed the remove-mktmp branch 2 times, most recently from eedf399 to 293d012 Compare August 26, 2023 19:41
See among other python#80490, python#87770

tempfile.mktemp is deprecated since 2.3 (security reason), but only in docs

As pointed out in above issues, and other there was at least one attempt
to emit a deprecation warnings but it has been removed as too many
places still use mktemp.

So trying to start by cleaning up the codebase of cpython from usage of
mktemp, so that maybe we can reintroduce the deprecation warning later.

This tries to minimise the number of changes – I could have used a
NamedTemporaryFile context manager in every tests, but it would have
added a lot of noise.
@cceneag

cceneag commented Jul 15, 2025

Copy link
Copy Markdown

This pull request refactors the Lib/test/test_shutil.py file to introduce a new mktemp helper method and applies it throughout the test suite to simplify temporary file creation. Additionally, it improves test class inheritance for consistency.

Refactoring for Temporary File Creation:

  • Added a new mktemp method to the BaseTest class, which simplifies the creation of temporary files by automatically handling directory management. This ensures that temporary files are cleaned up with their parent directories. ([Lib/test/test_shutil.pyR186-R195](https://github.com/python/cpython/pull/108515/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7R186-R195))
  • Replaced direct calls to tempfile.mktemp with the new mktemp method across multiple test cases, improving readability and consistency:
    • test_rmtree_errors_onerror ([Lib/test/test_shutil.pyL324-R334](https://github.com/python/cpython/pull/108515/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7L324-R334))
    • test_rmtree_errors_onexc ([Lib/test/test_shutil.pyL357-R367](https://github.com/python/cpython/pull/108515/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7L357-R367))
    • test_chown ([Lib/test/test_shutil.pyL1995-R2005](https://github.com/python/cpython/pull/108515/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7L1995-R2005))
    • test_move_dir ([Lib/test/test_shutil.pyL2443-R2453](https://github.com/python/cpython/pull/108515/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7L2443-R2453))
    • test_non_existent_src ([Lib/test/test_shutil.pyL2852-R2862](https://github.com/python/cpython/pull/108515/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7L2852-R2862))

Test Class Improvements:

  • Updated the _ZeroCopyFileTest class to inherit from BaseTest, ensuring it benefits from shared utilities like mktemp and aligning it with other test classes. ([Lib/test/test_shutil.pyL2796-R2806](https://github.com/python/cpython/pull/108515/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7L2796-R2806))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants