Commit 12b8d149 authored by Victor Stinner's avatar Victor Stinner

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).
parent e6c910e9
...@@ -92,10 +92,15 @@ __all__ = [ ...@@ -92,10 +92,15 @@ __all__ = [
] ]
import __future__ import __future__
import difflib
import sys, traceback, inspect, linecache, os, re import inspect
import unittest, difflib, pdb, tempfile import linecache
import warnings import os
import pdb
import re
import sys
import traceback
import unittest
from io import StringIO from io import StringIO
from collections import namedtuple from collections import namedtuple
...@@ -2509,15 +2514,6 @@ def debug_script(src, pm=False, globs=None): ...@@ -2509,15 +2514,6 @@ def debug_script(src, pm=False, globs=None):
"Debug a test script. `src` is the script, as a string." "Debug a test script. `src` is the script, as a string."
import pdb import pdb
# Note that tempfile.NameTemporaryFile() cannot be used. As the
# docs say, a file so created cannot be opened by name a second time
# on modern Windows boxes, and exec() needs to open and read it.
srcfilename = tempfile.mktemp(".py", "doctestdebug")
f = open(srcfilename, 'w')
f.write(src)
f.close()
try:
if globs: if globs:
globs = globs.copy() globs = globs.copy()
else: else:
...@@ -2525,23 +2521,14 @@ def debug_script(src, pm=False, globs=None): ...@@ -2525,23 +2521,14 @@ def debug_script(src, pm=False, globs=None):
if pm: if pm:
try: try:
with open(srcfilename) as f: exec(src, globs, globs)
exec(f.read(), globs, globs)
except: except:
print(sys.exc_info()[1]) print(sys.exc_info()[1])
p = pdb.Pdb(nosigint=True) p = pdb.Pdb(nosigint=True)
p.reset() p.reset()
p.interaction(None, sys.exc_info()[2]) p.interaction(None, sys.exc_info()[2])
else: else:
fp = open(srcfilename) pdb.Pdb(nosigint=True).run("exec(%r)" % src, globs, globs)
try:
script = fp.read()
finally:
fp.close()
pdb.Pdb(nosigint=True).run("exec(%r)" % script, globs, globs)
finally:
os.remove(srcfilename)
def debug(module, name, pm=False): def debug(module, name, pm=False):
"""Debug a single doctest docstring. """Debug a single doctest docstring.
......
...@@ -25,6 +25,9 @@ Core and Builtins ...@@ -25,6 +25,9 @@ Core and Builtins
Library Library
------- -------
- Issue #12451: doctest.debug_script() doesn't create a temporary file
anymore to avoid encoding issues.
- Issue #12451: pydoc.synopsis() now reads the encoding cookie if available, - Issue #12451: pydoc.synopsis() now reads the encoding cookie if available,
to read the Python script from the right encoding. to read the Python script from the right encoding.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment