Skip to content

Commit c9227df

Browse files
E-Paineambv
andauthored
bpo-42278: Use tempfile.TemporaryDirectory rather than tempfile.mktemp in pydoc (GH-23200)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent 2a8127c commit c9227df

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
@@ -1618,13 +1618,14 @@ def pipepager(text, cmd):
16181618
def tempfilepager(text, cmd):
16191619
"""Page through text by invoking a program on a temporary file."""
16201620
import tempfile
1621-
filename = tempfile.mktemp()
1622-
with open(filename, 'w', errors='backslashreplace') as file:
1623-
file.write(text)
1624-
try:
1621+
with tempfile.TemporaryDirectory() as tempdir:
1622+
filename = os.path.join(tempdir, 'pydoc.out')
1623+
with open(filename, 'w', errors='backslashreplace',
1624+
encoding=os.device_encoding(0) if
1625+
sys.platform == 'win32' else None
1626+
) as file:
1627+
file.write(text)
16251628
os.system(cmd + ' "' + filename + '"')
1626-
finally:
1627-
os.unlink(filename)
16281629

16291630
def _escape_stdout(text):
16301631
# 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)