summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-08-29 12:57:22 (GMT)
committerGitHub <noreply@github.com>2021-08-29 12:57:22 (GMT)
commit532ebba6c8697d214a0d94514ad0b2464a59cb7c (patch)
treeb52fbb0defc08bd64988bb4409086bb2ef278d07
parenta49398b643b3a2e6f9cc39729c528eb1e4385b1a (diff)
downloadcpython-532ebba6c8697d214a0d94514ad0b2464a59cb7c.zip
cpython-532ebba6c8697d214a0d94514ad0b2464a59cb7c.tar.gz
cpython-532ebba6c8697d214a0d94514ad0b2464a59cb7c.tar.bz2
bpo-42278: Use tempfile.TemporaryDirectory rather than tempfile.mktemp in pydoc (GH-23200)
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl> (cherry picked from commit c9227df5a9d8e958a2324cf0deba8524d1ded26a) Co-authored-by: E-Paine <63801254+E-Paine@users.noreply.github.com>
-rwxr-xr-xLib/pydoc.py13
-rw-r--r--Misc/NEWS.d/next/Security/2021-08-29-12-39-44.bpo-42278.jvmQz_.rst2
2 files changed, 9 insertions, 6 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 9f0acd0..4a8c10a 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1618,13 +1618,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
diff --git a/Misc/NEWS.d/next/Security/2021-08-29-12-39-44.bpo-42278.jvmQz_.rst b/Misc/NEWS.d/next/Security/2021-08-29-12-39-44.bpo-42278.jvmQz_.rst
new file mode 100644
index 0000000..db880cd
--- /dev/null
+++ b/Misc/NEWS.d/next/Security/2021-08-29-12-39-44.bpo-42278.jvmQz_.rst
@@ -0,0 +1,2 @@
+Replaced usage of :func:`tempfile.mktemp` with
+:class:`~tempfile.TemporaryDirectory` to avoid a potential race condition.