]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42278: Use tempfile.TemporaryDirectory rather than tempfile.mktemp in pydoc ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 29 Aug 2021 12:56:45 +0000 (05:56 -0700)
committerGitHub <noreply@github.com>
Sun, 29 Aug 2021 12:56:45 +0000 (14:56 +0200)
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>
Lib/pydoc.py
Misc/NEWS.d/next/Security/2021-08-29-12-39-44.bpo-42278.jvmQz_.rst [new file with mode: 0644]

index bdc0849f6af7a45a26d41d0ea4027a30bf1f6740..4f9d227ff4603a32add7f79d5992c4a8bb73c4a3 100755 (executable)
@@ -1617,13 +1617,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 (file)
index 0000000..db880cd
--- /dev/null
@@ -0,0 +1,2 @@
+Replaced usage of :func:`tempfile.mktemp` with
+:class:`~tempfile.TemporaryDirectory` to avoid a potential race condition.