Skip to content

Commit

Permalink
bpo-42278: Use tempfile.TemporaryDirectory rather than tempfile.mktem…
Browse files Browse the repository at this point in the history
…p 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>
  • Loading branch information
miss-islington and E-Paine committed Aug 29, 2021
1 parent 44dd2ec commit 193443b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,13 +1529,14 @@ def pipepager(text, cmd):
def tempfilepager(text, cmd):
"""Page through text by invoking a program on a temporary file."""
import tempfile
filename = tempfile.mktemp()
with open(filename, 'w', errors='backslashreplace') as file:
file.write(text)
try:
with tempfile.TemporaryDirectory() as tempdir:
filename = os.path.join(tempdir, 'pydoc.out')
with open(filename, 'w', errors='backslashreplace',
encoding=os.device_encoding(0) if
sys.platform == 'win32' else None
) as file:
file.write(text)
os.system(cmd + ' "' + filename + '"')
finally:
os.unlink(filename)

def _escape_stdout(text):
# Escape non-encodable characters to avoid encoding errors later
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Replaced usage of :func:`tempfile.mktemp` with
:class:`~tempfile.TemporaryDirectory` to avoid a potential race condition.

0 comments on commit 193443b

Please sign in to comment.