summaryrefslogtreecommitdiffstats
path: root/Lib/doctest.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-08-29 03:53:53 (GMT)
committerGuido van Rossum <guido@python.org>2007-08-29 03:53:53 (GMT)
commit1b81e7bea562b6fc9df0a9e99f51b2b4c351ec34 (patch)
treee524e99df43050f49b5100d84e72979a811c070d /Lib/doctest.py
parenta1c42a92dbe147a1157e840d27adeb373c544044 (diff)
downloadcpython-1b81e7bea562b6fc9df0a9e99f51b2b4c351ec34.zip
cpython-1b81e7bea562b6fc9df0a9e99f51b2b4c351ec34.tar.gz
cpython-1b81e7bea562b6fc9df0a9e99f51b2b4c351ec34.tar.bz2
Change the way the encoding parameter is handled.
This fixes test_doctest with strict bytes/str.
Diffstat (limited to 'Lib/doctest.py')
-rw-r--r--Lib/doctest.py17
1 files changed, 6 insertions, 11 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index ec51657..3fd4ecd 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -203,14 +203,14 @@ def _normalize_module(module, depth=2):
else:
raise TypeError("Expected a module, string, or None")
-def _load_testfile(filename, package, module_relative):
+def _load_testfile(filename, package, module_relative, encoding):
if module_relative:
package = _normalize_module(package, 3)
filename = _module_relative_path(package, filename)
if hasattr(package, '__loader__'):
if hasattr(package.__loader__, 'get_data'):
return package.__loader__.get_data(filename).decode('utf-8'), filename
- return open(filename, encoding="utf-8").read(), filename
+ return open(filename, encoding=encoding).read(), filename
def _indent(s, indent=4):
"""
@@ -1890,7 +1890,8 @@ def testfile(filename, module_relative=True, name=None, package=None,
"relative paths.")
# Relativize the path
- text, filename = _load_testfile(filename, package, module_relative)
+ text, filename = _load_testfile(filename, package, module_relative,
+ encoding or "utf-8")
# If no name was given, then use the file's name.
if name is None:
@@ -1909,9 +1910,6 @@ def testfile(filename, module_relative=True, name=None, package=None,
else:
runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
- if encoding is not None:
- text = text.decode(encoding)
-
# Read the file, convert it to a test, and run it.
test = parser.get_doctest(text, globs, name, filename, 0)
runner.run(test)
@@ -2292,7 +2290,8 @@ def DocFileTest(path, module_relative=True, package=None,
"relative paths.")
# Relativize the path.
- doc, path = _load_testfile(path, package, module_relative)
+ doc, path = _load_testfile(path, package, module_relative,
+ encoding or "utf-8")
if "__file__" not in globs:
globs["__file__"] = path
@@ -2300,10 +2299,6 @@ def DocFileTest(path, module_relative=True, package=None,
# Find the file and read it.
name = os.path.basename(path)
- # If an encoding is specified, use it to convert the file to unicode
- if encoding is not None:
- doc = doc.decode(encoding)
-
# Convert it to a test, and wrap it in a DocFileCase.
test = parser.get_doctest(doc, globs, name, path, 0)
return DocFileCase(test, **options)