sys.last_type

Here are the examples of the python api sys.last_type taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

15 Examples 7

Example 1

Project: pymo Source File: run.py
Function: stack_viewer
    def stackviewer(self, flist_oid=None):
        if self.usr_exc_info:
            typ, val, tb = self.usr_exc_info
        else:
            return None
        flist = None
        if flist_oid is not None:
            flist = self.rpchandler.get_remote_proxy(flist_oid)
        while tb and tb.tb_frame.f_globals["__name__"] in ["rpc", "run"]:
            tb = tb.tb_next
        sys.last_type = typ
        sys.last_value = val
        item = StackViewer.StackTreeItem(flist, tb)
        return RemoteObjectBrowser.remote_object_tree_item(item)

Example 2

Project: pymo Source File: StackViewer.py
Function: get_exception
    def get_exception(self):
        type = sys.last_type
        value = sys.last_value
        if hasattr(type, "__name__"):
            type = type.__name__
        s = str(type)
        if value is not None:
            s = s + ": " + str(value)
        return s

Example 3

Project: pymo Source File: StackViewer.py
def _test():
    try:
        import testcode
        reload(testcode)
    except:
        sys.last_type, sys.last_value, sys.last_traceback = sys.exc_info()
    from Tkinter import Tk
    root = Tk()
    StackBrowser(None, top=root)
    root.mainloop()

Example 4

Project: pytest Source File: test_runner.py
def test_store_except_info_on_eror():
    """ Test that upon test failure, the exception info is stored on
    sys.last_traceback and friends.
    """
    # Simulate item that raises a specific exception
    class ItemThatRaises:
        def runtest(self):
            raise IndexError('TEST')
    try:
        runner.pytest_runtest_call(ItemThatRaises())
    except IndexError:
        pass
    # Check that exception info is stored on sys
    assert sys.last_type is IndexError
    assert sys.last_value.args[0] == 'TEST'
    assert sys.last_traceback

Example 5

Project: DidYouMean-Python Source File: didyoumean_api.py
def didyoumean_postmortem():
    """Post postem function to add suggestions to last exception thrown.

    Add suggestions to last exception thrown (in interactive mode) and
    return it (which should print it).
    """
    if hasattr(sys, 'last_type'):
        typ, val, trace = sys.last_type, sys.last_value, sys.last_traceback
        add_suggestions_to_exception(typ, val, trace)
        return val
    return None

Example 6

Project: DidYouMean-Python Source File: didyoumean_api_tests.py
Function: run_with_api
    def run_with_api(self, code):
        """Run code with didyoumean post mortem."""
        # A bit of an ugly way to proceed, in real life scenario
        # the sys.last_<something> members are set automatically.
        for a in ('last_type', 'last_value', 'last_traceback'):
            if hasattr(sys, a):
                delattr(sys, a)
        try:
            no_exception(code)
        except:
            sys.last_type, sys.last_value, sys.last_traceback = sys.exc_info()
        ret = didyoumean_postmortem()
        if ret is not None:
            raise ret

Example 7

Project: pywikibot-core Source File: http.py
def _flush():
    session.close()
    message = 'Closing network session.'
    if hasattr(sys, 'last_type'):
        # we quit because of an exception
        print(sys.last_type)  # flake8: disable=T003 (print)
        critical(message)
    else:
        log(message)

    log('Network session closed.')

Example 8

Project: cgstudiomap Source File: __init__.py
Function: print_last
def print_last(limit=None, file=None, chain=True):
    """This is a shorthand for 'print_exception(sys.last_type,
    sys.last_value, sys.last_traceback, limit, file)'."""
    if not hasattr(sys, "last_type"):
        raise ValueError("no last exception")
    print_exception(sys.last_type, sys.last_value, sys.last_traceback,
                    limit, file, chain)

Example 9

Project: tracestack Source File: debugger.py
def pm(*args, **kwargs):
    """Post-mortem function that searches the last exception."""

    if hasattr(sys, "last_type"):
        einfo = (sys.last_type, sys.last_value, sys.last_traceback)
        handler = ExceptionHandler(*args, **kwargs)
        try:
            ipython_shell = get_ipython()
            ipython_shell.showtraceback()
            handler.handle_error(*einfo)
        except NameError:
            handler(*einfo)
    else:
        raise ValueError("no last exception")

Example 10

Project: databus Source File: Boxing.py
Function: dump_exception
def dump_exception(typ, val, tb):
    """dumps the given exception using pickle (since not all exceptions are picklable)"""
    tbtext = "".join(traceback.format_exception(typ, val, tb))
    sys.last_type, sys.last_value, sys.last_traceback = typ, val, tb
    try:
        pickled_exc = pickle.dumps((typ, val, tbtext), protocol = PICKLE_PROTOCOL)
    except pickle.PicklingError, ex:
        newval = NestedException("pickling error %s\nexception type: %r\nexception object: %s" % (ex, typ, val))
        pickled_exc = pickle.dumps((NestedException, newval, tbtext), protocol = PICKLE_PROTOCOL)
    return pickled_exc

Example 11

Project: pudb Source File: __init__.py
Function: pm
def pm():
    import sys
    try:
        e_type = sys.last_type
        e_value = sys.last_value
        tb = sys.last_traceback
    except AttributeError:
        ## No exception on record. Do nothing.
        return
    post_mortem(tb, e_type, e_value)

Example 12

Project: PokemonGo-Bot-Desktop Source File: test_extensions.py
    def test_widget_destroy(self):
        # automatically created variable
        x = ttk.LabeledScale(self.root)
        var = x._variable._name
        x.destroy()
        self.assertRaises(tkinter.TclError, x.tk.globalgetvar, var)

        # manually created variable
        myvar = tkinter.DoubleVar(self.root)
        name = myvar._name
        x = ttk.LabeledScale(self.root, variable=myvar)
        x.destroy()
        if self.wantobjects:
            self.assertEqual(x.tk.globalgetvar(name), myvar.get())
        else:
            self.assertEqual(float(x.tk.globalgetvar(name)), myvar.get())
        del myvar
        self.assertRaises(tkinter.TclError, x.tk.globalgetvar, name)

        # checking that the tracing callback is properly removed
        myvar = tkinter.IntVar(self.root)
        # LabeledScale will start tracing myvar
        x = ttk.LabeledScale(self.root, variable=myvar)
        x.destroy()
        # Unless the tracing callback was removed, creating a new
        # LabeledScale with the same var will cause an error now. This
        # happens because the variable will be set to (possibly) a new
        # value which causes the tracing callback to be called and then
        # it tries calling instance attributes not yet defined.
        ttk.LabeledScale(self.root, variable=myvar)
        if hasattr(sys, 'last_type'):
            self.assertNotEqual(sys.last_type, tkinter.TclError)

Example 13

Project: pymo Source File: test_extensions.py
    def test_widget_destroy(self):
        # automatically created variable
        x = ttk.LabeledScale()
        var = x._variable._name
        x.destroy()
        self.assertRaises(Tkinter.TclError, x.tk.globalgetvar, var)

        # manually created variable
        myvar = Tkinter.DoubleVar()
        name = myvar._name
        x = ttk.LabeledScale(variable=myvar)
        x.destroy()
        self.assertEqual(x.tk.globalgetvar(name), myvar.get())
        del myvar
        self.assertRaises(Tkinter.TclError, x.tk.globalgetvar, name)

        # checking that the tracing callback is properly removed
        myvar = Tkinter.IntVar()
        # LabeledScale will start tracing myvar
        x = ttk.LabeledScale(variable=myvar)
        x.destroy()
        # Unless the tracing callback was removed, creating a new
        # LabeledScale with the same var will cause an error now. This
        # happens because the variable will be set to (possibly) a new
        # value which causes the tracing callback to be called and then
        # it tries calling instance attributes not yet defined.
        ttk.LabeledScale(variable=myvar)
        if hasattr(sys, 'last_type'):
            self.assertFalse(sys.last_type == Tkinter.TclError)

Example 14

Project: TrustRouter Source File: test_extensions.py
    def test_widget_destroy(self):
        # automatically created variable
        x = ttk.LabeledScale()
        var = x._variable._name
        x.destroy()
        self.assertRaises(tkinter.TclError, x.tk.globalgetvar, var)

        # manually created variable
        myvar = tkinter.DoubleVar()
        name = myvar._name
        x = ttk.LabeledScale(variable=myvar)
        x.destroy()
        self.assertEqual(x.tk.globalgetvar(name), myvar.get())
        del myvar
        self.assertRaises(tkinter.TclError, x.tk.globalgetvar, name)

        # checking that the tracing callback is properly removed
        myvar = tkinter.IntVar()
        # LabeledScale will start tracing myvar
        x = ttk.LabeledScale(variable=myvar)
        x.destroy()
        # Unless the tracing callback was removed, creating a new
        # LabeledScale with the same var will cause an error now. This
        # happens because the variable will be set to (possibly) a new
        # value which causes the tracing callback to be called and then
        # it tries calling instance attributes not yet defined.
        ttk.LabeledScale(variable=myvar)
        if hasattr(sys, 'last_type'):
            self.assertFalse(sys.last_type == tkinter.TclError)

Example 15

Project: iot-utilities Source File: code.py
Function: show_traceback
    def showtraceback(self):
        """Display the exception that just occurred.

        We remove the first stack item because it is our own code.

        The output is written by self.write(), below.

        """
        sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
        sys.last_traceback = last_tb
        try:
            lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
            if sys.excepthook is sys.__excepthook__:
                self.write(''.join(lines))
            else:
                # If someone has set sys.excepthook, we let that take precedence
                # over self.write
                sys.excepthook(ei[0], ei[1], last_tb)
        finally:
            last_tb = ei = None