summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-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)