(merge 3.2) 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).
This commit is contained in:
Victor Stinner 2011-06-30 17:39:17 +02:00
commit a0b12a1ca7
2 changed files with 26 additions and 36 deletions

View file

@ -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
@ -2511,39 +2516,21 @@ 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 if globs:
# docs say, a file so created cannot be opened by name a second time globs = globs.copy()
# on modern Windows boxes, and exec() needs to open and read it. else:
srcfilename = tempfile.mktemp(".py", "doctestdebug") globs = {}
f = open(srcfilename, 'w')
f.write(src)
f.close()
try: if pm:
if globs: try:
globs = globs.copy() exec(src, globs, globs)
else: except:
globs = {} print(sys.exc_info()[1])
p = pdb.Pdb(nosigint=True)
if pm: p.reset()
try: p.interaction(None, sys.exc_info()[2])
with open(srcfilename) as f: else:
exec(f.read(), globs, globs) pdb.Pdb(nosigint=True).run("exec(%r)" % src, globs, globs)
except:
print(sys.exc_info()[1])
p = pdb.Pdb(nosigint=True)
p.reset()
p.interaction(None, sys.exc_info()[2])
else:
fp = open(srcfilename)
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.

View file

@ -200,6 +200,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.