*** ../python-clean/dist/src/Lib/doctest.py 2005-09-13 22:32:45.094628288 +0300 --- dist/src/Lib/doctest.py 2005-09-13 22:39:40.476480648 +0300 *************** *** 1853,1859 **** def testfile(filename, module_relative=True, name=None, package=None, globs=None, verbose=None, report=True, optionflags=0, ! extraglobs=None, raise_on_error=False, parser=DocTestParser()): """ Test examples in the given file. Return (#failures, #tests). --- 1853,1860 ---- def testfile(filename, module_relative=True, name=None, package=None, globs=None, verbose=None, report=True, optionflags=0, ! extraglobs=None, raise_on_error=False, parser=DocTestParser(), ! encoding=None): """ Test examples in the given file. Return (#failures, #tests). *************** *** 1918,1923 **** --- 1919,1927 ---- Optional keyword arg "parser" specifies a DocTestParser (or subclass) that should be used to extract tests from the files. + Optional keyword arg "encoding" specifies an encoding that should + be used to convert the file to unicode. + Advanced tomfoolery: testmod runs methods of a local instance of class doctest.Tester, then merges the results into (or creates) global Tester instance doctest.master. Methods of doctest.master *************** *** 1956,1961 **** --- 1960,1967 ---- # Read the file, convert it to a test, and run it. s = open(filename).read() + if encoding: + s = s.decode(encoding) test = parser.get_doctest(s, globs, name, filename, 0) runner.run(test) *************** *** 2325,2331 **** ) def DocFileTest(path, module_relative=True, package=None, ! globs=None, parser=DocTestParser(), **options): if globs is None: globs = {} else: --- 2331,2338 ---- ) def DocFileTest(path, module_relative=True, package=None, ! globs=None, parser=DocTestParser(), ! encoding=None, **options): if globs is None: globs = {} else: *************** *** 2346,2351 **** --- 2353,2362 ---- name = os.path.basename(path) doc = open(path).read() + # If an encoding is specified, use it to convert the file to unicode + if encoding: + 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) *************** *** 2402,2407 **** --- 2413,2421 ---- parser A DocTestParser (or subclass) that should be used to extract tests from the files. + + encoding + An encoding that will be used to convert the files to unicode. """ suite = unittest.TestSuite() *** ../python-clean/dist/src/Lib/test/test_doctest4.txt 1970-01-01 03:00:00.000000000 +0300 --- dist/src/Lib/test/test_doctest4.txt 2005-09-13 22:35:11.320398592 +0300 *************** *** 0 **** --- 1,17 ---- + This is a sample doctest in a text file that contains non-ASCII characters. + This file is encoded using UTF-8. + + In order to get this test to pass, we have to manually specify the + encoding. + + >>> u'föö' + u'f\xf6\xf6' + + >>> u'bąr' + u'b\u0105r' + + >>> 'föö' + 'f\xc3\xb6\xc3\xb6' + + >>> 'bąr' + 'b\xc4\x85r' *** ../python-clean/dist/src/Lib/test/test_doctest.py 2005-09-13 22:32:50.702775720 +0300 --- dist/src/Lib/test/test_doctest.py 2005-09-13 22:35:11.331396920 +0300 *************** *** 1898,1906 **** >>> import unittest >>> suite = doctest.DocFileSuite('test_doctest.txt', ! ... 'test_doctest2.txt') >>> suite.run(unittest.TestResult()) ! The test files are looked for in the directory containing the calling module. A package keyword argument can be provided to --- 1898,1907 ---- >>> import unittest >>> suite = doctest.DocFileSuite('test_doctest.txt', ! ... 'test_doctest2.txt', ! ... 'test_doctest4.txt') >>> suite.run(unittest.TestResult()) ! The test files are looked for in the directory containing the calling module. A package keyword argument can be provided to *************** *** 1909,1917 **** >>> import unittest >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... package='test') >>> suite.run(unittest.TestResult()) ! '/' should be used as a path separator. It will be converted to a native separator at run time: --- 1910,1919 ---- >>> import unittest >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', + ... 'test_doctest4.txt', ... package='test') >>> suite.run(unittest.TestResult()) ! '/' should be used as a path separator. It will be converted to a native separator at run time: *************** *** 1956,1974 **** >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... globs={'favorite_color': 'blue'}) >>> suite.run(unittest.TestResult()) ! In this case, we supplied a missing favorite color. You can provide doctest options: >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... optionflags=doctest.DONT_ACCEPT_BLANKLINE, ... globs={'favorite_color': 'blue'}) >>> suite.run(unittest.TestResult()) ! And, you can provide setUp and tearDown functions: --- 1958,1978 ---- >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', + ... 'test_doctest4.txt', ... globs={'favorite_color': 'blue'}) >>> suite.run(unittest.TestResult()) ! In this case, we supplied a missing favorite color. You can provide doctest options: >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', + ... 'test_doctest4.txt', ... optionflags=doctest.DONT_ACCEPT_BLANKLINE, ... globs={'favorite_color': 'blue'}) >>> suite.run(unittest.TestResult()) ! And, you can provide setUp and tearDown functions: *************** *** 1986,1994 **** >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... setUp=setUp, tearDown=tearDown) >>> suite.run(unittest.TestResult()) ! But the tearDown restores sanity: --- 1990,1999 ---- >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', + ... 'test_doctest4.txt', ... setUp=setUp, tearDown=tearDown) >>> suite.run(unittest.TestResult()) ! But the tearDown restores sanity: *************** *** 2021,2026 **** --- 2026,2042 ---- >>> suite.run(unittest.TestResult()) + If the tests contain non-ASCII characters, we have to specify which + encoding the file is encoded with. We do so by using the `encoding` + parameter: + + >>> suite = doctest.DocFileSuite('test_doctest.txt', + ... 'test_doctest2.txt', + ... 'test_doctest4.txt', + ... encoding='utf-8') + >>> suite.run(unittest.TestResult()) + + """ def test_trailing_space_in_test(): *************** *** 2227,2232 **** --- 2243,2274 ---- Traceback (most recent call last): UnexpectedException: ... >>> doctest.master = None # Reset master. + + If the tests contain non-ASCII characters, the tests might fail, since + it's unknown which encoding is used. The encoding can be specified + using the optional keyword argument `encoding`: + + >>> doctest.testfile('test_doctest4.txt') # doctest: +ELLIPSIS + ********************************************************************** + File "...", line 7, in test_doctest4.txt + Failed example: + u'...' + Expected: + u'f\xf6\xf6' + Got: + u'f\xc3\xb6\xc3\xb6' + ********************************************************************** + ... + ********************************************************************** + 1 items had failures: + 2 of 4 in test_doctest4.txt + ***Test Failed*** 2 failures. + (2, 4) + >>> doctest.master = None # Reset master. + + >>> doctest.testfile('test_doctest4.txt', encoding='utf-8') + (0, 4) + >>> doctest.master = None # Reset master. """ # old_test1, ... used to live in doctest.py, but cluttered it. Note