Python Examples

The following are 9 code examples of You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module traceback , or try the search function .
Example #1
Source File:    From stoq with Apache License 2.0 6 votes vote down vote up
def format_exc(exc: Exception, limit: int = -1, msg: Optional[str] = None):
    Format `Exceptions` for use with `Stoq` error handling

    # Inspired from
    tb = traceback.format_tb(exc.__traceback__, limit=limit)[0].split('\n')[0].strip()
    stype = type(exc).__qualname__
    smod = type(exc).__module__
    if smod not in ('__main__', 'builtins'):
        stype = f'{smod}.{stype}'
    exc_str = f'{tb} ; {stype}: {str(exc)}'
    if msg:
        return f'{msg}: {exc_str}'
        return exc_str 
Example #2
Source File:    From eliot with Apache License 2.0 6 votes vote down vote up
def _writeTracebackMessage(logger, typ, exception, traceback):
    Write a traceback to the log.

    @param typ: The class of the exception.

    @param exception: The L{Exception} instance.

    @param traceback: The traceback, a C{str}.
    msg = TRACEBACK_MESSAGE(reason=exception, traceback=traceback, exception=typ)
    msg = msg.bind(**_error_extraction.get_fields_for_exception(logger, exception))

# The default Python standard library formatting functions
# involving reading source from disk. This is a potential performance hit
# since disk I/O can block. We therefore format the tracebacks with in-memory
# information only.
# Unfortunately, the easiest way to do this is... exciting. 
Example #3
Source File:    From HPOlib with GNU General Public License v3.0 6 votes vote down vote up
def load_experiment_config_file():
    # Load the config file, this holds information about data, black box fn etc.
        cfg_filename = "config.cfg"
        config = SafeConfigParser(allow_no_value=True)
        if not config.has_option("HPOLIB", "is_not_original_config_file"):
            logger.critical("Config file in directory %s seems to be an"
                            " original config which was not created by "
                            "Are you sure that you are in the right directory?" %
        return config
    except IOError as e:
        logger.critical("Could not open config file in directory %s",
Example #4
Source File:    From Computable with MIT License 5 votes vote down vote up
def _fixed_getinnerframes(etb, context=1,tb_offset=0):

    records  = fix_frame_records_filenames(inspect.getinnerframes(etb, context))

    # If the error is at the console, don't build any context, since it would
    # otherwise produce 5 blank lines printed out (there is no file at the
    # console)
    rec_check = records[tb_offset:]
        rname = rec_check[0][1]
        if rname == '<ipython console>' or rname.endswith('<string>'):
            return rec_check
    except IndexError:

    aux = traceback.extract_tb(etb)
    assert len(records) == len(aux)
    for i, (file, lnum, _, _) in zip(range(len(records)), aux):
        maybeStart = lnum-1 - context//2
        start =  max(maybeStart, 0)
        end   = start + context
        lines = ulinecache.getlines(file)[start:end]
        buf = list(records[i])
        buf[LNUM_POS] = lnum
        buf[INDEX_POS] = lnum - 1 - start
        buf[LINES_POS] = lines
        records[i] = tuple(buf)
    return records[tb_offset:]

# Helper function -- largely belongs to VerboseTB, but we need the same
# functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
# can be recognized properly by ipython.el's py-traceback-line-re
# (SyntaxErrors have to be treated specially because they have no traceback) 
Example #5
Source File:    From Computable with MIT License 5 votes vote down vote up
def _format_list(self, extracted_list):
        """Format a list of traceback entry tuples for printing.

        Given a list of tuples as returned by extract_tb() or
        extract_stack(), return a list of strings ready for printing.
        Each string in the resulting list corresponds to the item with the
        same index in the argument list.  Each string ends in a newline;
        the strings may contain internal newlines as well, for those items
        whose source text line is not None.

        Lifted almost verbatim from

        Colors = self.Colors
        list = []
        for filename, lineno, name, line in extracted_list[:-1]:
            item = '  File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
                    (Colors.filename, filename, Colors.Normal,
                     Colors.lineno, lineno, Colors.Normal,
           , name, Colors.Normal)
            if line:
                item += '    %s\n' % line.strip()
        # Emphasize the last entry
        filename, lineno, name, line = extracted_list[-1]
        item = '%s  File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
                 Colors.filenameEm, filename, Colors.normalEm,
                 Colors.linenoEm, lineno, Colors.normalEm,
                 Colors.nameEm, name, Colors.normalEm,
        if line:
            item += '%s    %s%s\n' % (Colors.line, line.strip(),
        #from pprint import pformat; print 'LISTTB', pformat(list) # dbg
        return list 
Example #6
Source File:    From Computable with MIT License 5 votes vote down vote up
def _some_str(self, value):
        # Lifted from
            return str(value)
            return '<unprintable %s object>' % type(value).__name__

Example #7
Source File:    From better-exceptions with MIT License 5 votes vote down vote up
def format_traceback(self, tb=None):
        omit_last = False
        if not tb:
                raise Exception()
                omit_last = True
                _, _, tb = sys.exc_info()
                assert tb is not None

        frames = []
        final_source = ''
        while tb:
            if omit_last and not tb.tb_next:

            formatted, colored = self.format_traceback_frame(tb)

            # special case to ignore runcode() here.
            if not (os.path.basename(formatted[0]) == '' and formatted[2] == 'runcode'):
                final_source = colored

            tb = tb.tb_next

        lines = traceback.format_list(frames)

        return ''.join(lines), final_source 
Example #8
Source File:    From better-exceptions with MIT License 5 votes vote down vote up
def _format_exception(self, value, tb, seen=None):
        # Implemented from built-in traceback module:

        exc_type, exc_value, exc_traceback = type(value), value, tb

        if seen is None:
            seen = set()


        if exc_value and PY3:
            if exc_value.__cause__ is not None and id(exc_value.__cause__) not in seen:
                for text in self._format_exception(exc_value.__cause__,exc_value.__cause__.__traceback__, seen=seen):
                    yield text
                yield u"\nThe above exception was the direct cause of the following exception:\n\n"
            elif exc_value.__context__ is not None and id(exc_value.__context__) not in seen and not exc_value.__suppress_context__:
                for text in self._format_exception(exc_value.__context__, exc_value.__context__.__traceback__, seen=seen):
                    yield text
                yield u"\nDuring handling of the above exception, another exception occurred:\n\n"

        if exc_traceback is not None:
            yield u'Traceback (most recent call last):\n'

        formatted, colored_source = self.format_traceback(exc_traceback)

        yield formatted

        if not str(value) and exc_type is AssertionError:
            value.args = (colored_source,)
        title = traceback.format_exception_only(exc_type, value)

        yield u''.join(title).strip() + u'\n' 
Example #9
Source File:    From HPOlib with GNU General Public License v3.0 5 votes vote down vote up
def format_traceback(exc_info):
    traceback_template = '''Traceback (most recent call last):
    File "%(filename)s", line %(lineno)s, in %(name)s
    %(type)s: %(message)s\n'''# Skipping the "actual line" item

    # Also note: we don't walk all the way through the frame stack in this example
    # see
    # (Imagine if the 1/0, below, were replaced by a call to test() which did 1/0.)

    exc_type, exc_value, exc_traceback = exc_info  # most recent (if any) by default

    Reason this _can_ be bad: If an (unhandled) exception happens AFTER this,
    or if we do not delete the labels on (not much) older versions of Py, the
    reference we created can linger.

    traceback.format_exc/print_exc do this very thing, BUT note this creates a
    temp scope within the function.

    traceback_details = {
                         'filename': exc_traceback.tb_frame.f_code.co_filename,
                         'lineno'  : exc_traceback.tb_lineno,
                         'name'    : exc_traceback.tb_frame.f_code.co_name,
                         'type'    : exc_type.__name__,
                         'message' : exc_value.message, # or see traceback._some_str()

    del(exc_type, exc_value, exc_traceback) # So we don't leave our local labels/objects dangling
    # This still isn't "completely safe", though!
    # "Best (recommended) practice: replace all exc_type, exc_value, exc_traceback
    # with sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]

    return "\n" + traceback.format_exc() + "\n\n" + traceback_template % traceback_details + "\n\n"