Python traceback.format_tb() Examples

The following are 30 code examples of traceback.format_tb(). 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: base.py    From jbox with MIT License 6 votes vote down vote up
def load_wsgi(self):
        try:
            self.wsgi = self.app.wsgi()
        except SyntaxError as e:
            if not self.cfg.reload:
                raise

            self.log.exception(e)

            # fix from PR #1228
            # storing the traceback into exc_tb will create a circular reference.
            # per https://docs.python.org/2/library/sys.html#sys.exc_info warning,
            # delete the traceback after use.
            try:
                exc_type, exc_val, exc_tb = sys.exc_info()
                self.reloader.add_extra_file(exc_val.filename)

                tb_string = traceback.format_tb(exc_tb)
                self.wsgi = util.make_fail_app(tb_string)
            finally:
                del exc_tb 
Example #2
Source File: test_traceback.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_traceback_format(self):
        from _testcapi import traceback_print
        try:
            raise KeyError('blah')
        except KeyError:
            type_, value, tb = sys.exc_info()
            traceback_fmt = 'Traceback (most recent call last):\n' + \
                            ''.join(traceback.format_tb(tb))
            file_ = StringIO()
            traceback_print(tb, file_)
            python_fmt  = file_.getvalue()
        else:
            raise Error("unable to create test traceback string")

        # Make sure that Python and the traceback module format the same thing
        self.assertEqual(traceback_fmt, python_fmt)

        # Make sure that the traceback is properly indented.
        tb_lines = python_fmt.splitlines()
        self.assertEqual(len(tb_lines), 3)
        banner, location, source_line = tb_lines
        self.assertTrue(banner.startswith('Traceback'))
        self.assertTrue(location.startswith('  File'))
        self.assertTrue(source_line.startswith('    raise')) 
Example #3
Source File: search_command.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _report_unexpected_error(self):

        error_type, error, tb = sys.exc_info()
        origin = tb

        while origin.tb_next is not None:
            origin = origin.tb_next

        filename = origin.tb_frame.f_code.co_filename
        lineno = origin.tb_lineno
        message = '{0} at "{1}", line {2:d} : {3}'.format(error_type.__name__, filename, lineno, error)

        environment.splunklib_logger.error(message + '\nTraceback:\n' + ''.join(traceback.format_tb(tb)))
        self.write_error(message)

    # endregion

    # region Types 
Example #4
Source File: test_app_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def test_apps_list(self):
        runner = CliRunner()
        result = runner.invoke(cli, ["get", "apps"])
        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
            pytest.fail("BP Get failed")
        LOG.info("Success") 
Example #5
Source File: telemetry.py    From azure-cli-extensions with MIT License 6 votes vote down vote up
def _get_stack_trace():
    def _get_root_path():
        dir_path = os.path.dirname(os.path.realpath(__file__))
        head, tail = os.path.split(dir_path)
        while tail and tail != 'azext_alias':
            head, tail = os.path.split(head)
        return head

    def _remove_root_paths(s):
        root = _get_root_path()
        frames = [p.replace(root, '') for p in s]
        return str(frames)

    _, _, ex_traceback = sys.exc_info()
    trace = traceback.format_tb(ex_traceback)
    return _remove_cmd_chars(_remove_symbols(_remove_root_paths(trace))) 
Example #6
Source File: exceptions.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def simplify(worker: "sy.workers.AbstractWorker", e):
        """
        Serialize information about an Exception which was raised to forward it
        """
        # Get information about the exception: type of error,  traceback
        tp = type(e)
        tb = e.__traceback__
        # Serialize the traceback
        traceback_str = "Traceback (most recent call last):\n" + "".join(traceback.format_tb(tb))
        # Include special attributes if relevant
        try:
            attributes = e.get_attributes()
        except AttributeError:
            attributes = {}
        return (
            sy.serde.msgpack.serde._simplify(worker, tp.__name__),
            sy.serde.msgpack.serde._simplify(worker, traceback_str),
            sy.serde.msgpack.serde._simplify(worker, attributes),
        ) 
Example #7
Source File: exceptions.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def simplify(worker: "sy.workers.AbstractWorker", e):
        """
        Serialize information about an Exception which was raised to forward it
        """
        # Get information about the exception: type of error,  traceback
        tp = type(e)
        tb = e.__traceback__
        # Serialize the traceback
        traceback_str = "Traceback (most recent call last):\n" + "".join(traceback.format_tb(tb))
        # Include special attributes if relevant
        try:
            attributes = e.get_attributes()
        except AttributeError:
            attributes = {}
        return (
            sy.serde.msgpack.serde._simplify(worker, tp.__name__),
            sy.serde.msgpack.serde._simplify(worker, traceback_str),
            sy.serde.msgpack.serde._simplify(worker, attributes),
        ) 
Example #8
Source File: search_command.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _report_unexpected_error(self):

        error_type, error, tb = sys.exc_info()
        origin = tb

        while origin.tb_next is not None:
            origin = origin.tb_next

        filename = origin.tb_frame.f_code.co_filename
        lineno = origin.tb_lineno
        message = '{0} at "{1}", line {2:d} : {3}'.format(error_type.__name__, filename, lineno, error)

        environment.splunklib_logger.error(message + '\nTraceback:\n' + ''.join(traceback.format_tb(tb)))
        self.write_error(message)

    # endregion

    # region Types 
Example #9
Source File: search_command.py    From SplunkForPCAP with MIT License 6 votes vote down vote up
def _report_unexpected_error(self):

        error_type, error, tb = sys.exc_info()
        origin = tb

        while origin.tb_next is not None:
            origin = origin.tb_next

        filename = origin.tb_frame.f_code.co_filename
        lineno = origin.tb_lineno
        message = '{0} at "{1}", line {2:d} : {3}'.format(error_type.__name__, filename, lineno, error)

        environment.splunklib_logger.error(message + '\nTraceback:\n' + ''.join(traceback.format_tb(tb)))
        self.write_error(message)

    # endregion

    # region Types 
Example #10
Source File: search_command.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _report_unexpected_error(self):

        error_type, error, tb = sys.exc_info()
        origin = tb

        while origin.tb_next is not None:
            origin = origin.tb_next

        filename = origin.tb_frame.f_code.co_filename
        lineno = origin.tb_lineno
        message = '{0} at "{1}", line {2:d} : {3}'.format(error_type.__name__, filename, lineno, error)

        environment.splunklib_logger.error(message + '\nTraceback:\n' + ''.join(traceback.format_tb(tb)))
        self.write_error(message)

    # endregion

    # region Types 
Example #11
Source File: search_command.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _report_unexpected_error(self):

        error_type, error, tb = sys.exc_info()
        origin = tb

        while origin.tb_next is not None:
            origin = origin.tb_next

        filename = origin.tb_frame.f_code.co_filename
        lineno = origin.tb_lineno
        message = '{0} at "{1}", line {2:d} : {3}'.format(error_type.__name__, filename, lineno, error)

        environment.splunklib_logger.error(message + '\nTraceback:\n' + ''.join(traceback.format_tb(tb)))
        self.write_error(message)

    # endregion

    # region Types 
Example #12
Source File: __init__.py    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 https://github.com/python/cpython/blob/3.7/Lib/traceback.py#L560-L563
    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}'
    else:
        return exc_str 
Example #13
Source File: search_command.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _report_unexpected_error(self):

        error_type, error, tb = sys.exc_info()
        origin = tb

        while origin.tb_next is not None:
            origin = origin.tb_next

        filename = origin.tb_frame.f_code.co_filename
        lineno = origin.tb_lineno
        message = '{0} at "{1}", line {2:d} : {3}'.format(error_type.__name__, filename, lineno, error)

        environment.splunklib_logger.error(message + '\nTraceback:\n' + ''.join(traceback.format_tb(tb)))
        self.write_error(message)

    # endregion

    # region Types 
Example #14
Source File: test_app_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def test_apps_list_with_name(self):
        runner = CliRunner()
        result = runner.invoke(cli, ["get", "apps", "--name=MSSQL"])
        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
        LOG.info("Success") 
Example #15
Source File: test_app_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def _test_describe_app(self):
        runner = CliRunner()
        LOG.info("Running 'calm describe app' command")
        result = runner.invoke(cli, ["describe", "app", self.created_app_name])
        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
        LOG.info("Success") 
Example #16
Source File: pyversion.py    From locality-sensitive-hashing with MIT License 6 votes vote down vote up
def format_exception(exc_info, encoding='UTF-8'):
    ec, ev, tb = exc_info

    # Our exception object may have been turned into a string, and Python 3's
    # traceback.format_exception() doesn't take kindly to that (it expects an
    # actual exception object).  So we work around it, by doing the work
    # ourselves if ev is not an exception object.
    if not is_base_exception(ev):
        tb_data = force_unicode(
                ''.join(traceback.format_tb(tb)),
                encoding)
        ev = exc_to_unicode(ev)
        return tb_data + ev
    else:
        return force_unicode(
                ''.join(traceback.format_exception(*exc_info)),
                encoding) 
Example #17
Source File: test_app_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def _test_app_delete(self):
        runner = CliRunner()
        self._wait_for_non_busy_state()
        LOG.info("Deleting App {} ".format(self.created_app_name))
        result = runner.invoke(cli, ["delete", "app", self.created_app_name])
        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
        LOG.info("Success") 
Example #18
Source File: test_app_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def _test_dsl_bp_delete(self):
        runner = CliRunner()
        LOG.info("Deleting Bp {} ".format(self.created_dsl_bp_name))
        result = runner.invoke(cli, ["delete", "bp", self.created_dsl_bp_name])
        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
        LOG.info("Success") 
Example #19
Source File: px.py    From px with MIT License 6 votes vote down vote up
def handle_exceptions(extype, value, tb):
    # Create traceback log
    lst = (traceback.format_tb(tb, None) +
        traceback.format_exception_only(extype, value))
    tracelog = '\nTraceback (most recent call last):\n' + "%-20s%s\n" % (
        "".join(lst[:-1]), lst[-1])

    if State.logger != None:
        pprint(tracelog)
    else:
        sys.stderr.write(tracelog)

        # Save to debug.log
        dbg = open(dfile(), 'w')
        dbg.write(tracelog)
        dbg.close()

###
# Install Px to startup 
Example #20
Source File: test_bp_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def test_bps_list_with_limit_offset(self):
        runner = CliRunner()
        result = runner.invoke(cli, ["get", "bps", "--limit=15", "--offset=5"])
        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
            pytest.fail("BP list with limit call failed")
        LOG.info("Success") 
Example #21
Source File: test_bp_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def test_bps_list_with_name(self):
        runner = CliRunner()
        result = runner.invoke(cli, ["get", "bps", "--name=MSSQL"])
        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
            pytest.fail("BP list with name call failed")
        LOG.info("Success") 
Example #22
Source File: test_bp_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def test_compile_bp(self):
        runner = CliRunner()
        LOG.info("Compiling Bp file at {}".format(DSL_BP_FILEPATH))
        result = runner.invoke(
            cli, ["compile", "bp", "--file={}".format(DSL_BP_FILEPATH)]
        )

        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
            pytest.fail("BP compile command failed")
        LOG.info("Success") 
Example #23
Source File: test_test_utils.py    From purerpc with Apache License 2.0 6 votes vote down vote up
def test_run_context_manager_generator_in_process_error_traceback():
    def gen():
        def inner_2():
            def inner_1():
                raise ValueError("42")
            inner_1()
        inner_2()

    try:
        with _run_context_manager_generator_in_process(gen):
            pass
    except ValueError:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        lines = traceback.format_tb(exc_traceback)
        expected_traceback = ("gen", "inner_2", "inner_1")
        for expected_fname, line in zip(expected_traceback[::-1], lines[::-1]):
            assert expected_fname in line 
Example #24
Source File: test_bp_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def _test_dsl_bp_delete(self):
        runner = CliRunner()
        LOG.info("Deleting DSL Bp {} ".format(self.created_dsl_bp_name))
        result = runner.invoke(cli, ["delete", "bp", self.created_dsl_bp_name])
        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
        LOG.info("Success") 
Example #25
Source File: test_bp_commands.py    From calm-dsl with Apache License 2.0 6 votes vote down vote up
def _test_json_bp_delete(self):
        runner = CliRunner()
        LOG.info("Deleting JSON Bp {} ".format(self.created_json_bp_name))
        result = runner.invoke(cli, ["delete", "bp", self.created_json_bp_name])
        if result.exit_code:
            cli_res_dict = {"Output": result.output, "Exception": str(result.exception)}
            LOG.debug(
                "Cli Response: {}".format(
                    json.dumps(cli_res_dict, indent=4, separators=(",", ": "))
                )
            )
            LOG.debug(
                "Traceback: \n{}".format(
                    "".join(traceback.format_tb(result.exc_info[2]))
                )
            )
        LOG.info("Success") 
Example #26
Source File: test_test_utils.py    From purerpc with Apache License 2.0 6 votes vote down vote up
def test_run_tests_in_workers_error_traceback():
    def target_fn():
        def inner_2():
            def inner_1():
                raise ValueError("42")
            inner_1()
        inner_2()

    try:
        run_tests_in_workers(target=target_fn, num_workers=1)
    except ValueError:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        lines = traceback.format_tb(exc_traceback)
        expected_traceback = ("target_fn", "inner_2", "inner_1")
        for expected_fname, line in zip(expected_traceback[::-1], lines[::-1]):
            assert expected_fname in line 
Example #27
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def run(self, manual=False):
        """ execute the code in this file """ 
        r = Run(report=self.report, name=self.file_location, manual=manual)
        r.save()
        logger = RunLineLogger(r)
        try:
            report_object = report_map(self.file_location, logger)
            report_object.run()
            for artifact in report_object.artifacts:
                artifact.convert_to_unicode()
                try:
                    result = Result(run=r, name=artifact.title, table=artifact.to_dict() )
                except AttributeError:
                    result = Result(run=r, table=artifact.to_dict() ) 
                result.save()
            r.success = True
            r.save()
        except Exception as e:
            logger.log("ERROR: " + str(e) )
            type_, value_, traceback_ = sys.exc_info()
            logger.log( ",".join(traceback.format_tb( traceback_ )) )
        return r 
Example #28
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def run(self, manual=False):
        r = Run(report=self.report, name=self.name, manual=manual)
        r.save()
        logger = RunLineLogger(r)
        try:
            DB2_Query.set_logger(logger) 
            DB2_Query.connect()
            q = DB2_Query()
            q.query = string.Template(self.query)
            artifact = q.result()
            artifact.convert_to_unicode()
            result = Result(run=r, name=self.name, table=artifact.to_dict() )
            result.save()
            r.success = True
            r.save()
        except Exception as e:
            logger.log("ERROR: " + str(e) )
            type_, value_, traceback_ = sys.exc_info()
            logger.log( ",".join(traceback.format_tb( traceback_ )) )
        return r 
Example #29
Source File: error_monitor.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def capture_exception(self, exc: Optional[BaseException] = None, user: Any = None,
                                context_env: Any = None,
                                severity: Optional[LogSeverity] = None):
        if exc:
            tb = exc.__traceback__
        else:
            _, exc, tb = sys.exc_info()
        exc_type: Any = type(exc)
        if severity is None:
            severity = LogSeverity.ERROR
        async with self.dbpool.acquire() as conn, conn.begin():
            query = error_logs.insert().values({
                'severity': severity,
                'source': 'manager',
                'user': user,
                'message': ''.join(traceback.format_exception_only(exc_type, exc)).strip(),
                'context_lang': 'python',
                'context_env': context_env,
                'traceback': ''.join(traceback.format_tb(tb)).strip()
            })
            await conn.execute(query)
        log.debug('Manager log collected: {}', str(exc)) 
Example #30
Source File: exceptions.py    From openshift-restclient-python with Apache License 2.0 6 votes vote down vote up
def api_exception(e):
    """
    Returns the proper Exception class for the given kubernetes.client.rest.ApiException object
    https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#success-codes
    """
    _, _, exc_traceback = sys.exc_info()
    tb = '\n'.join(traceback.format_tb(exc_traceback))
    return {
        400: BadRequestError,
        401: UnauthorizedError,
        403: ForbiddenError,
        404: NotFoundError,
        405: MethodNotAllowedError,
        409: ConflictError,
        410: GoneError,
        422: UnprocessibleEntityError,
        429: TooManyRequestsError,
        500: InternalServerError,
        503: ServiceUnavailableError,
        504: ServerTimeoutError,
    }.get(e.status, DynamicApiError)(e, tb)