From c9227df5a9d8e958a2324cf0deba8524d1ded26a Mon Sep 17 00:00:00 2001 From: E-Paine <63801254+E-Paine@users.noreply.github.com> Date: Sun, 29 Aug 2021 12:07:51 +0100 Subject: bpo-42278: Use tempfile.TemporaryDirectory rather than tempfile.mktemp in pydoc (GH-23200) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ɓukasz Langa --- Lib/pydoc.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index b2915bfdfc..34a6087603 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 -- cgit v1.2.1