Closed mengyyy closed 5 years ago
Thanks! Looks like something that should work, I'll look into it.
Oh I should do like this
In [217]: try:
...: with stackprinter.TracePrinter(style='darkbg2'):
...: c = a- b
...: except Exception:
...: print(stackprinter.format())
...:
File <ipython-input-217-b3cd7d3a5ab2>, line 3, in <module>
1 try:
2 with stackprinter.TracePrinter(style='darkbg2'):
--> 3 c = a- b
4 except Exception:
..................................................
stackprinter.TracePrinter = <class 'stackprinter.tracing.TracePrinter'>
c = -1
a = 1
b = 2
..................................................
File c:\program files\python36\lib\site-packages\stackprinter\tracing.py, line 99, in __exit__
--> 99 def __exit__(self, etype, evalue, tb):
100 self.disable()
..................................................
self = <stackprinter.tracing.TracePrinter object at 0x0000019C6170A
940>
etype = None
evalue = None
tb = None
self.disable = <method 'TracePrinter.disable' of <stackprinter.tracing.Trac
ePrinter object at 0x0000019C6170A940> tracing.py:115>
..................................................
File c:\program files\python36\lib\site-packages\stackprinter\tracing.py, line 129, in trace
122 def trace(self, frame, event, arg):
(...)
125 return None
126
127 if 'call' in event:
128 callsite = frame.f_back
--> 129 self.show(callsite)
130 self.show(frame)
..................................................
self = <stackprinter.tracing.TracePrinter object at 0x0000019C6170A
940>
frame = <frame object at 0x0000019C5A00A498>
event = 'call'
arg = None
callsite = <frame object at 0x0000019C57E1EF48>
frame.f_back = <frame object at 0x0000019C57E1EF48>
self.show = <method 'TracePrinter.show' of <stackprinter.tracing.TracePr
inter object at 0x0000019C6170A940> tracing.py:144>
..................................................
File c:\program files\python36\lib\site-packages\stackprinter\tracing.py, line 149, in show
144 def show(self, frame, note=''):
145 if frame is None:
146 return
147
148 filepath = inspect.getsourcefile(frame) or inspect.getfile(frame)
--> 149 if match(filepath, __file__):
150 return
..................................................
self = <stackprinter.tracing.TracePrinter object at 0x0000019C6170A
940>
frame = <frame object at 0x0000019C57E1EF48>
note = ''
filepath = '<ipython-input-217-b3cd7d3a5ab2>'
inspect.getsourcefile = <function 'getsourcefile' inspect.py:680>
inspect.getfile = <function 'getfile' inspect.py:643>
__file__ = 'c:\\program files\\python36\\lib\\site-packages\\stackprint
er\\tracing.py'
..................................................
File c:\program files\python36\lib\site-packages\stackprinter\utils.py, line 14, in match
7 def match(string, patterns):
(...)
10 if isinstance(patterns, str):
11 patterns = [patterns]
12 elif patterns is None:
13 return False
--> 14 return any([bool(re.search(p, string)) for p in patterns])
..................................................
string = '<ipython-input-217-b3cd7d3a5ab2>'
patterns = ['c:\\program files\\python36\\lib\\site-packages\\stackprin
ter\\tracing.py']
re.search = <function 'search' re.py:179>
..................................................
File c:\program files\python36\lib\site-packages\stackprinter\utils.py, line 14, in <listcomp>
10 if isinstance(patterns, str):
11 patterns = [patterns]
12 elif patterns is None:
13 return False
--> 14 return any([bool(re.search(p, string)) for p in patterns])
15
..................................................
re.search = <function 'search' re.py:179>
p = 'c:\\program files\\python36\\lib\\site-packages\\stackprint
er\\tracing.py'
string = '<ipython-input-217-b3cd7d3a5ab2>'
..................................................
File c:\program files\python36\lib\re.py, line 182, in search
179 def search(pattern, string, flags=0):
180 """Scan through string looking for a match to the pattern, returning
181 a match object, or None if no match was found."""
--> 182 return _compile(pattern, flags).search(string)
..................................................
pattern = 'c:\\program files\\python36\\lib\\site-packages\\stackprint
er\\tracing.py'
string = '<ipython-input-217-b3cd7d3a5ab2>'
flags = 0
..................................................
File c:\program files\python36\lib\re.py, line 301, in _compile
286 def _compile(pattern, flags):
(...)
297 "cannot process flags argument with a compiled pattern")
298 return pattern
299 if not sre_compile.isstring(pattern):
300 raise TypeError("first argument must be string or compiled pattern")
--> 301 p = sre_compile.compile(pattern, flags)
302 if not (flags & DEBUG):
..................................................
pattern = 'c:\\program files\\python36\\lib\\site-packages\\stackprint
er\\tracing.py'
flags = 0
sre_compile.isstring = <function 'isstring' sre_compile.py:539>
sre_compile.compile = <function 'compile' sre_compile.py:557>
DEBUG = <RegexFlag.DEBUG: 128>
..................................................
File c:\program files\python36\lib\sre_compile.py, line 562, in compile
557 def compile(p, flags=0):
558 # internal: convert pattern list to internal format
559
560 if isstring(p):
561 pattern = p
--> 562 p = sre_parse.parse(p, flags)
563 else:
..................................................
p = 'c:\\program files\\python36\\lib\\site-packages\\stackprint
er\\tracing.py'
flags = 0
pattern = 'c:\\program files\\python36\\lib\\site-packages\\stackprint
er\\tracing.py'
sre_parse.parse = <function 'parse' sre_parse.py:844>
..................................................
File c:\program files\python36\lib\sre_parse.py, line 855, in parse
844 def parse(str, flags=0, pattern=None):
(...)
851 pattern.flags = flags
852 pattern.str = str
853
854 try:
--> 855 p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
856 except Verbose:
..................................................
str = 'c:\\program files\\python36\\lib\\site-packages\\stackprint
er\\tracing.py'
flags = 0
pattern = <sre_parse.Pattern object at 0x0000019C6170A7F0>
pattern.flags = 0
pattern.str = 'c:\\program files\\python36\\lib\\site-packages\\stackprint
er\\tracing.py'
source = <sre_parse.Tokenizer object at 0x0000019C6170A8D0>
SRE_FLAG_VERBOSE = 64
Verbose = <class 'sre_parse.Verbose'>
..................................................
File c:\program files\python36\lib\sre_parse.py, line 416, in _parse_sub
407 def _parse_sub(source, state, verbose, nested):
(...)
412 sourcematch = source.match
413 start = source.tell()
414 while True:
415 itemsappend(_parse(source, state, verbose, nested + 1,
--> 416 not nested and not items))
417 if not sourcematch("|"):
..................................................
source = <sre_parse.Tokenizer object at 0x0000019C6170A8D0>
state = <sre_parse.Pattern object at 0x0000019C6170A7F0>
verbose = 0
nested = 0
sourcematch = <method 'Tokenizer.match' of <sre_parse.Tokenizer object at
0x0000019C6170A8D0> sre_parse.py:248>
source.match = <method 'Tokenizer.match' of <sre_parse.Tokenizer object at
0x0000019C6170A8D0> sre_parse.py:248>
start = 0
source.tell = <method 'Tokenizer.tell' of <sre_parse.Tokenizer object at 0
x0000019C6170A8D0> sre_parse.py:285>
items = []
..................................................
File c:\program files\python36\lib\sre_parse.py, line 502, in _parse
470 def _parse(source, state, verbose, nested, first=False):
(...)
498 break
499 continue
500
501 if this[0] == "\\":
--> 502 code = _escape(source, this, state)
503 subpatternappend(code)
..................................................
source = <sre_parse.Tokenizer object at 0x0000019C6170A8D0>
state = <sre_parse.Pattern object at 0x0000019C6170A7F0>
verbose = 0
nested = 1
first = True
this = '\\p'
subpatternappend = <method 'SubPattern.append' of [(LITERAL, 99), (LITERAL, 58)
] sre_parse.py:171>
..................................................
File c:\program files\python36\lib\sre_parse.py, line 401, in _escape
342 def _escape(source, escape, state):
(...)
397 return GROUPREF, group
398 raise source.error("invalid group reference %d" % group, len(escape) - 1)
399 if len(escape) == 2:
400 if c in ASCIILETTERS:
--> 401 raise source.error("bad escape %s" % escape, len(escape))
402 return LITERAL, ord(escape[1])
..................................................
source = <sre_parse.Tokenizer object at 0x0000019C6170A8D0>
escape = '\\p'
state = <sre_parse.Pattern object at 0x0000019C6170A7F0>
GROUPREF = GROUPREF
source.error = <method 'Tokenizer.error' of <sre_parse.Tokenizer object at
0x0000019C6170A8D0> sre_parse.py:291>
c = 'p'
ASCIILETTERS = frozenset({'P', 'D', 'o', 'w', 'k', 'I', 'C', 'd', 'F', 'G',
'r', 'A', 'u', 'K', 'U', 'R', 'e', 'H', 'W', 'J', 'h', 'j',
'T', 'V', 'Y', 'n', 'b', 'Z', 'E', 'l', 't', 'S', 'f', 'v',
'z', 'm', 'N', 'g', 'i', 'O', 's', 'X', 'x', 'y', 'L', 'Q',
'p', 'M', 'B', 'c', 'a', 'q'})
LITERAL = LITERAL
..................................................
---- (full traceback above) ----
File <ipython-input-217-b3cd7d3a5ab2>, line 3, in <module>
c = a- b
File c:\program files\python36\lib\site-packages\stackprinter\tracing.py, line 99, in __exit__
def __exit__(self, etype, evalue, tb):
File c:\program files\python36\lib\site-packages\stackprinter\tracing.py, line 129, in trace
self.show(callsite)
File c:\program files\python36\lib\site-packages\stackprinter\tracing.py, line 149, in show
if match(filepath, __file__):
File c:\program files\python36\lib\site-packages\stackprinter\utils.py, line 14, in match
return any([bool(re.search(p, string)) for p in patterns])
File c:\program files\python36\lib\site-packages\stackprinter\utils.py, line 14, in <listcomp>
return any([bool(re.search(p, string)) for p in patterns])
File c:\program files\python36\lib\re.py, line 182, in search
return _compile(pattern, flags).search(string)
File c:\program files\python36\lib\re.py, line 301, in _compile
p = sre_compile.compile(pattern, flags)
File c:\program files\python36\lib\sre_compile.py, line 562, in compile
p = sre_parse.parse(p, flags)
File c:\program files\python36\lib\sre_parse.py, line 855, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File c:\program files\python36\lib\sre_parse.py, line 416, in _parse_sub
not nested and not items))
File c:\program files\python36\lib\sre_parse.py, line 502, in _parse
code = _escape(source, this, state)
File c:\program files\python36\lib\sre_parse.py, line 401, in _escape
raise source.error("bad escape %s" % escape, len(escape))
error: bad escape \p at position 2
Excellent, it's debugging itself :D Looks like the backslashes in windows file paths are causing some weirdness with escape characters - this should be easy to fix. I'll look into it in a few days (currently on the road)
This should be fixed in the new 0.2.3. Can you confirm?
Yes, it has been fixed. Thank you~
When i try this tracing-a-piece-of-code i get "error: bad escape \p at position 2" Here is my code