mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
bpo-42278: Use tempfile.TemporaryDirectory rather than tempfile.mktemp in pydoc (GH-23200) (GH-28025)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
(cherry picked from commit c9227df5a9
)
Co-authored-by: E-Paine <63801254+E-Paine@users.noreply.github.com>
This commit is contained in:
parent
330aabbbbe
commit
45409518c1
2 changed files with 9 additions and 6 deletions
11
Lib/pydoc.py
11
Lib/pydoc.py
|
@ -1617,13 +1617,14 @@ def pipepager(text, cmd):
|
||||||
def tempfilepager(text, cmd):
|
def tempfilepager(text, cmd):
|
||||||
"""Page through text by invoking a program on a temporary file."""
|
"""Page through text by invoking a program on a temporary file."""
|
||||||
import tempfile
|
import tempfile
|
||||||
filename = tempfile.mktemp()
|
with tempfile.TemporaryDirectory() as tempdir:
|
||||||
with open(filename, 'w', errors='backslashreplace') as file:
|
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)
|
file.write(text)
|
||||||
try:
|
|
||||||
os.system(cmd + ' "' + filename + '"')
|
os.system(cmd + ' "' + filename + '"')
|
||||||
finally:
|
|
||||||
os.unlink(filename)
|
|
||||||
|
|
||||||
def _escape_stdout(text):
|
def _escape_stdout(text):
|
||||||
# Escape non-encodable characters to avoid encoding errors later
|
# Escape non-encodable characters to avoid encoding errors later
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Replaced usage of :func:`tempfile.mktemp` with
|
||||||
|
:class:`~tempfile.TemporaryDirectory` to avoid a potential race condition.
|
Loading…
Add table
Add a link
Reference in a new issue