Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12b8d14

Browse files
author
Victor Stinner
committed
Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
avoid encoding issues (it used the locale encoding, whereas UTF-8 should be). Remove also an unused import (warnings).
1 parent e6c910e commit 12b8d14

2 files changed

Lines changed: 26 additions & 36 deletions

File tree

Lib/doctest.py

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,15 @@ def _test():
9292
]
9393

9494
import __future__
95-
96-
import sys, traceback, inspect, linecache, os, re
97-
import unittest, difflib, pdb, tempfile
98-
import warnings
95+
import difflib
96+
import inspect
97+
import linecache
98+
import os
99+
import pdb
100+
import re
101+
import sys
102+
import traceback
103+
import unittest
99104
from io import StringIO
100105
from collections import namedtuple
101106

@@ -2509,39 +2514,21 @@ def debug_script(src, pm=False, globs=None):
25092514
"Debug a test script. `src` is the script, as a string."
25102515
import pdb
25112516

2512-
# Note that tempfile.NameTemporaryFile() cannot be used. As the
2513-
# docs say, a file so created cannot be opened by name a second time
2514-
# on modern Windows boxes, and exec() needs to open and read it.
2515-
srcfilename = tempfile.mktemp(".py", "doctestdebug")
2516-
f = open(srcfilename, 'w')
2517-
f.write(src)
2518-
f.close()
2519-
2520-
try:
2521-
if globs:
2522-
globs = globs.copy()
2523-
else:
2524-
globs = {}
2525-
2526-
if pm:
2527-
try:
2528-
with open(srcfilename) as f:
2529-
exec(f.read(), globs, globs)
2530-
except:
2531-
print(sys.exc_info()[1])
2532-
p = pdb.Pdb(nosigint=True)
2533-
p.reset()
2534-
p.interaction(None, sys.exc_info()[2])
2535-
else:
2536-
fp = open(srcfilename)
2537-
try:
2538-
script = fp.read()
2539-
finally:
2540-
fp.close()
2541-
pdb.Pdb(nosigint=True).run("exec(%r)" % script, globs, globs)
2517+
if globs:
2518+
globs = globs.copy()
2519+
else:
2520+
globs = {}
25422521

2543-
finally:
2544-
os.remove(srcfilename)
2522+
if pm:
2523+
try:
2524+
exec(src, globs, globs)
2525+
except:
2526+
print(sys.exc_info()[1])
2527+
p = pdb.Pdb(nosigint=True)
2528+
p.reset()
2529+
p.interaction(None, sys.exc_info()[2])
2530+
else:
2531+
pdb.Pdb(nosigint=True).run("exec(%r)" % src, globs, globs)
25452532

25462533
def debug(module, name, pm=False):
25472534
"""Debug a single doctest docstring.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Core and Builtins
2525
Library
2626
-------
2727

28+
- Issue #12451: doctest.debug_script() doesn't create a temporary file
29+
anymore to avoid encoding issues.
30+
2831
- Issue #12451: pydoc.synopsis() now reads the encoding cookie if available,
2932
to read the Python script from the right encoding.
3033

0 commit comments

Comments
 (0)