diff options
| author | gbrandl <gbrandl@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2009-04-01 20:00:21 +0000 |
|---|---|---|
| committer | gbrandl <gbrandl@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2009-04-01 20:00:21 +0000 |
| commit | 069193bfb868995efddf3d623d7128d2d63fda1c (patch) | |
| tree | ff6e815b7969aac2e2bffddc4ef6d99c9df43ca4 /docutils/test/DocutilsTestSupport.py | |
| parent | 7422fd625d008f8d73350ac03686927263b25c22 (diff) | |
| download | docutils-069193bfb868995efddf3d623d7128d2d63fda1c.tar.gz | |
Convert docutils to Python 3 (at least so that it runs the test suite.)
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@5889 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils/test/DocutilsTestSupport.py')
| -rw-r--r-- | docutils/test/DocutilsTestSupport.py | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/docutils/test/DocutilsTestSupport.py b/docutils/test/DocutilsTestSupport.py index 7d286b283..2638ea9a1 100644 --- a/docutils/test/DocutilsTestSupport.py +++ b/docutils/test/DocutilsTestSupport.py @@ -64,8 +64,8 @@ try: from docutils.parsers import rst from docutils.parsers.rst import states, tableparser, roles, languages from docutils.readers import standalone, pep - from docutils.readers.python import moduleparser from docutils.statemachine import StringList, string2lines + from docutils._compat import bytes except ImportError: # The importing module (usually __init__.py in one of the # subdirectories) may catch ImportErrors in order to detect the @@ -191,10 +191,18 @@ class CustomTestCase(StandardTestCase): """`input`, `output`, and `expected` should all be strings.""" if isinstance(input, unicode): input = input.encode('raw_unicode_escape') - if isinstance(output, unicode): - output = output.encode('raw_unicode_escape') - if isinstance(expected, unicode): - expected = expected.encode('raw_unicode_escape') + if sys.version_info > (3,): + # API difference: Python 3's node.__str__ doesn't escape + #assert expected is None or isinstance(expected, unicode) + if isinstance(expected, bytes): + expected = expected.decode('ascii') + if isinstance(output, bytes): + output = output.decode('ascii') + else: + if isinstance(expected, unicode): + expected = expected.encode('raw_unicode_escape') + if isinstance(output, unicode): + output = output.encode('raw_unicode_escape') try: self.assertEquals(output, expected) except AssertionError, error: @@ -611,6 +619,12 @@ class PythonModuleParserTestCase(CustomTestCase): def test_parser(self): if self.run_in_debugger: pdb.set_trace() + try: + import compiler + except ImportError: + # skip on Python 3 + return + from docutils.readers.python import moduleparser module = moduleparser.parse_module(self.input, 'test data').pformat() output = str(module) self.compare_output(self.input, output, self.expected) @@ -618,6 +632,12 @@ class PythonModuleParserTestCase(CustomTestCase): def test_token_parser_rhs(self): if self.run_in_debugger: pdb.set_trace() + try: + import compiler + except ImportError: + # skip on Python 3 + return + from docutils.readers.python import moduleparser tr = moduleparser.TokenParser(self.input) output = tr.rhs(1) self.compare_output(self.input, output, self.expected) @@ -806,19 +826,19 @@ class HtmlWriterPublishPartsTestCase(WriterPublishTestCase): output = [] for key in keys: output.append("%r: '''%s'''" - % (key, parts[key].encode('raw_unicode_escape'))) + % (key, parts[key])) if output[-1].endswith("\n'''"): output[-1] = output[-1][:-4] + "\\n'''" return '{' + ',\n '.join(output) + '}\n' -def exception_data(code): +def exception_data(func, *args, **kwds): """ - Execute `code` and return the resulting exception, the exception arguments, - and the formatted exception string. + Execute `func(*args, **kwds)` and return the resulting exception, the + exception arguments, and the formatted exception string. """ try: - exec(code) + func(*args, **kwds) except Exception, detail: return (detail, detail.args, '%s: %s' % (detail.__class__.__name__, detail)) @@ -849,13 +869,15 @@ def _format_str(*args): if ( (isinstance(i, str) or isinstance(i, unicode)) and '\n' in i): stripped = '' - if isinstance(i, unicode): - # stripped = 'u' or 'U' + if isinstance(i, unicode) and r.startswith('u'): + stripped = r[0] + r = r[1:] + elif isinstance(i, bytes) and r.startswith('b'): stripped = r[0] r = r[1:] # quote_char = "'" or '"' quote_char = r[0] - assert quote_char in ("'", '"') + assert quote_char in ("'", '"'), quote_char assert r[0] == r[-1] r = r[1:-1] r = (stripped + 3 * quote_char + '\\\n' + |
