diff options
| author | Martin <gzlist@googlemail.com> | 2010-06-22 23:18:11 +0100 |
|---|---|---|
| committer | Martin <gzlist@googlemail.com> | 2010-06-22 23:18:11 +0100 |
| commit | 047746da26baa994f7d491254a19a29fab41c26f (patch) | |
| tree | feebf7fa225f0e91721fe19f8d374c6917d53a55 /testtools/compat.py | |
| parent | cbedaeeef3aaf21fa61a2aa842cf04af705bb899 (diff) | |
| download | testtools-047746da26baa994f7d491254a19a29fab41c26f.tar.gz | |
Avoid problem introduced with fix to Python bug #1031213 by ignoring the SyntaxError line and rereading the file instead
Diffstat (limited to 'testtools/compat.py')
| -rw-r--r-- | testtools/compat.py | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/testtools/compat.py b/testtools/compat.py index 32b8d18..5b57509 100644 --- a/testtools/compat.py +++ b/testtools/compat.py @@ -193,16 +193,21 @@ def _format_exc_info(eclass, evalue, tb, limit=None): # instead create a new instance with unicode filename and line # Potentially gives duff spacing, but that's a pre-existing issue filename, lineno, offset, line = evalue.args[1] - if filename: - filename = filename.decode(fs_enc, "replace") if line: # Errors during parsing give the line from buffer encoded as - # latin-1 if the given coding or utf-8 for all other codings - # Can't know which was used, so just try utf-8 first - try: - line = line.decode("utf-8") - except UnicodeDecodeError: - line = line.decode("latin-1") + # latin-1 or utf-8 or the encoding of the file depending on the + # coding and whether the patch for issue #1031213 is applied, so + # give up on trying to decode it and just read the file again + bytes = linecache.getline(filename, lineno) + if bytes: + if lineno == 1 and bytes.startswith("\xef\xbb\xbf"): + bytes = bytes[3:] + line = bytes.decode(_get_source_encoding(filename), "replace") + del linecache.cache[filename] + else: + line = line.decode("ascii", "replace") + if filename: + filename = filename.decode(fs_enc, "replace") evalue = eclass(evalue.args[0], (filename, lineno, offset, line)) list.extend(traceback.format_exception_only(eclass, evalue)) else: |
