selfboot / AnnotatedShadowSocks

Annotated shadowsocks(python version)
Other
3 stars 1 forks source link

Print stack traceback with traceback.print_exc() #22

Open selfboot opened 7 years ago

selfboot commented 7 years ago

Traceback module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter.

traceback.print_exc([limit[, file]])

This is a shorthand for print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file).

print_exception is here:

traceback.print_exception(etype, value, tb[, limit[, file]])

Print exception information and up to limit stack trace entries from the traceback tb to file. If limit is omitted or None, all entries are printed. If file is omitted or None, the output goes to sys.stderr.

Demo

This simple example implements a basic read-eval-print loop, similar to (but less useful than) the standard Python interactive interpreter loop.

import sys, traceback

def run_user_code(envdir):
    source = raw_input(">>> ")
    try:
        exec source in envdir
    except:
        print "Exception in user code:"
        print '-'*60
        traceback.print_exc(file=sys.stdout)
        print '-'*60

envdir = {}
while 1:
    run_user_code(envdir)

When we run it as follows:

$ python demo.py
>>> 1a
Exception in user code:
------------------------------------------------------------
Traceback (most recent call last):
  File "demo.py", line 8, in run_user_code
    exec source in envdir
  File "<string>", line 1
    1a
     ^
SyntaxError: invalid syntax
------------------------------------------------------------

We get the exception information just like as we use interactive interpreter.

>>> 1a
  File "<stdin>", line 1
    1a
     ^
SyntaxError: invalid syntax

exec

exec_stmt: "exec" expression ["in" expression ["," expression]]

This statement supports dynamic execution of Python code. The first expression should evaluate to either a string, an open file object, or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). If it is an open file, the file is parsed until EOF and executed. If it is a code object, it is simply executed.

In all cases, if the optional parts are omitted, the code is executed in the current scope.

>>> a = 2
>>> exec "a = 1"
>>> a
1

If only the first expression after in is specified, it should be a dictionary, which will be used for both the global and the local variables.

>>> a = 10
>>> b = 20
>>> g = {'a': 6, 'b': 8}
>>> exec "global a; print a, b" in g
6 8

If two expressions are given, both must be dictionaries and they are used for the global and local variables, respectively.

>>> a = 10
>>> b = 20
>>> c = 20
>>> g = {'a': 6, 'b': 8}
>>> l = {'b': 9, 'c':10}
>>> exec "global a; print a,b,c" in g, l
6 9 10

Ref
traceback — Print or retrieve a stack traceback
The exec statement
python exec and eval