Skip to content

Commit 4540951

Browse files
bpo-42278: Use tempfile.TemporaryDirectory rather than tempfile.mktemp in pydoc (GH-23200) (GH-28025)
Co-authored-by: Łukasz Langa <[email protected]> (cherry picked from commit c9227df) Co-authored-by: E-Paine <[email protected]>
1 parent 330aabb commit 4540951

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

Lib/pydoc.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,13 +1617,14 @@ def pipepager(text, cmd):
16171617
def tempfilepager(text, cmd):
16181618
"""Page through text by invoking a program on a temporary file."""
16191619
import tempfile
1620-
filename = tempfile.mktemp()
1621-
with open(filename, 'w', errors='backslashreplace') as file:
1622-
file.write(text)
1623-
try:
1620+
with tempfile.TemporaryDirectory() as tempdir:
1621+
filename = os.path.join(tempdir, 'pydoc.out')
1622+
with open(filename, 'w', errors='backslashreplace',
1623+
encoding=os.device_encoding(0) if
1624+
sys.platform == 'win32' else None
1625+
) as file:
1626+
file.write(text)
16241627
os.system(cmd + ' "' + filename + '"')
1625-
finally:
1626-
os.unlink(filename)
16271628

16281629
def _escape_stdout(text):
16291630
# Escape non-encodable characters to avoid encoding errors later
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Replaced usage of :func:`tempfile.mktemp` with
2+
:class:`~tempfile.TemporaryDirectory` to avoid a potential race condition.

0 commit comments

Comments
 (0)