~bzr/ubuntu/natty/python-testtools/bzr-ppa

« back to all changes in this revision

Viewing changes to testtools/compat.py

  • Committer: Robert Collins
  • Date: 2010-11-14 15:49:58 UTC
  • mfrom: (16.11.4 upstream)
  • Revision ID: robertc@robertcollins.net-20101114154958-lwb16rdhehq6q020
New snapshot for testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
202
202
                filename.decode(fs_enc, "replace"),
203
203
                lineno,
204
204
                name.decode("ascii", "replace"),
205
 
                line.decode(_get_source_encoding(filename), "replace")))
 
205
                line and line.decode(
 
206
                    _get_source_encoding(filename), "replace")))
206
207
        list.extend(traceback.format_list(extracted_list))
207
208
    else:
208
209
        list = []
209
210
    if evalue is None:
210
211
        # Is a (deprecated) string exception
211
 
        list.append(sclass.decode("ascii", "replace"))
212
 
    elif isinstance(evalue, SyntaxError) and len(evalue.args) > 1:
 
212
        list.append((eclass + "\n").decode("ascii", "replace"))
 
213
        return list
 
214
    if isinstance(evalue, SyntaxError):
213
215
        # Avoid duplicating the special formatting for SyntaxError here,
214
216
        # instead create a new instance with unicode filename and line
215
217
        # Potentially gives duff spacing, but that's a pre-existing issue
216
 
        filename, lineno, offset, line = evalue.args[1]
217
 
        if line:
 
218
        try:
 
219
            msg, (filename, lineno, offset, line) = evalue
 
220
        except (TypeError, ValueError):
 
221
            pass # Strange exception instance, fall through to generic code
 
222
        else:
218
223
            # Errors during parsing give the line from buffer encoded as
219
224
            # latin-1 or utf-8 or the encoding of the file depending on the
220
225
            # coding and whether the patch for issue #1031213 is applied, so
221
226
            # give up on trying to decode it and just read the file again
222
 
            bytestr = linecache.getline(filename, lineno)
223
 
            if bytestr:
224
 
                if lineno == 1 and bytestr.startswith("\xef\xbb\xbf"):
225
 
                    bytestr = bytestr[3:]
226
 
                line = bytestr.decode(_get_source_encoding(filename), "replace")
227
 
                del linecache.cache[filename]
228
 
            else:
229
 
                line = line.decode("ascii", "replace")
230
 
        if filename:
231
 
            filename = filename.decode(fs_enc, "replace")
232
 
        evalue = eclass(evalue.args[0], (filename, lineno, offset, line))
233
 
        list.extend(traceback.format_exception_only(eclass, evalue))
 
227
            if line:
 
228
                bytestr = linecache.getline(filename, lineno)
 
229
                if bytestr:
 
230
                    if lineno == 1 and bytestr.startswith("\xef\xbb\xbf"):
 
231
                        bytestr = bytestr[3:]
 
232
                    line = bytestr.decode(
 
233
                        _get_source_encoding(filename), "replace")
 
234
                    del linecache.cache[filename]
 
235
                else:
 
236
                    line = line.decode("ascii", "replace")
 
237
            if filename:
 
238
                filename = filename.decode(fs_enc, "replace")
 
239
            evalue = eclass(msg, (filename, lineno, offset, line))
 
240
            list.extend(traceback.format_exception_only(eclass, evalue))
 
241
            return list
 
242
    sclass = eclass.__name__
 
243
    svalue = _exception_to_text(evalue)
 
244
    if svalue:
 
245
        list.append("%s: %s\n" % (sclass, svalue))
 
246
    elif svalue is None:
 
247
        # GZ 2010-05-24: Not a great fallback message, but keep for the moment
 
248
        list.append("%s: <unprintable %s object>\n" % (sclass, sclass))
234
249
    else:
235
 
        sclass = eclass.__name__
236
 
        svalue = _exception_to_text(evalue)
237
 
        if svalue:
238
 
            list.append("%s: %s\n" % (sclass, svalue))
239
 
        elif svalue is None:
240
 
            # GZ 2010-05-24: Not a great fallback message, but keep for the
241
 
            #                the same for compatibility for the moment
242
 
            list.append("%s: <unprintable %s object>\n" % (sclass, sclass))
243
 
        else:
244
 
            list.append("%s\n" % sclass)
 
250
        list.append("%s\n" % sclass)
245
251
    return list