Skip to content

Commit 193443b

Browse files
bpo-42278: Use tempfile.TemporaryDirectory rather than tempfile.mktemp in pydoc (pythonGH-23200) (pythonGH-28026)
Co-authored-by: Łukasz Langa <lukasz@langa.pl> (cherry picked from commit c9227df) Co-authored-by: E-Paine <63801254+E-Paine@users.noreply.github.com>
1 parent 44dd2ec commit 193443b

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
@@ -1529,13 +1529,14 @@ def pipepager(text, cmd):
15291529
def tempfilepager(text, cmd):
15301530
"""Page through text by invoking a program on a temporary file."""
15311531
import tempfile
1532-
filename = tempfile.mktemp()
1533-
with open(filename, 'w', errors='backslashreplace') as file:
1534-
file.write(text)
1535-
try:
1532+
with tempfile.TemporaryDirectory() as tempdir:
1533+
filename = os.path.join(tempdir, 'pydoc.out')
1534+
with open(filename, 'w', errors='backslashreplace',
1535+
encoding=os.device_encoding(0) if
1536+
sys.platform == 'win32' else None
1537+
) as file:
1538+
file.write(text)
15361539
os.system(cmd + ' "' + filename + '"')
1537-
finally:
1538-
os.unlink(filename)
15391540

15401541
def _escape_stdout(text):
15411542
# 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)