traceback shows only one line of a multiline command - python

I have added a small debugging aid to my server. It logs a stack trace obtained from traceback.format_stack()
It contains few incomplete lines like this:
File "/home/...../base/loop.py", line 361, in run
self.outputs.fd_list, (), sleep)
which is not that much helpfull.
The source lines 360 and 361:
rlist, wlist, unused = select.select(self.inputs.fd_list,
self.outputs.fd_list, (), sleep)
If only one line can be part of the stack trace, I would say the line 360 with the function name (here select.select) is the right one, because the stack is created by calling functions.
Anyway, I would prefer the whole (logical) line to be printed. Or at least some context (e.g. 2 lines before). Is that possible? I mean with just an adequate effort, of course.
Tried to add a line continuation character \, but without success.
EPILOGUE:
Based on Jean-François Fabre's answer and his code I'm going to use this function:
def print_trace():
for fname, lnum, func, line in traceback.extract_stack()[:-1]:
print('File "{}", line {}, in {}'.format(fname, lnum, func))
try:
with open(fname) as f:
rl = f.readlines()
except OSError:
if line is not None:
print(" " + line + " <===")
continue
first = max(0, lnum-3)
# read 2 lines before and 2 lines after
for i, line in enumerate(rl[first:lnum+2]):
line = line.rstrip()
if i + first + 1 == lnum:
print(" " + line + " <===")
elif line:
print(" " + line)

"just with adequate effort" this can be done. But it's hack-like
check this example:
import traceback,re,os,sys
r = re.compile(r'File\s"(.*)",\sline\s(\d+)')
def print_trace():
# discard the 2 deepest entries since they're a call to print_trace()
lines = [str.split(x,"\n")[0] for x in traceback.format_stack()][:-2]
for l in lines:
m = r.search(l)
if m != None:
sys.stdout.write(l+"\n")
file = m.group(1)
line = int(m.group(2))-1
if os.path.exists(file):
with open(file,"r") as f:
rl = f.readlines()
tblines = rl[max(line-2,0):min(line+3,len(rl))]
# read 2 lines before and 2 lines after
for i,tl in enumerate(tblines):
tl = tl.rstrip()
if i==2:
sys.stdout.write(" "+tl+" <====\n")
elif tl:
sys.stdout.write(" "+tl+"\n")
def foo():
print_trace()
foo()
output:
File "C:\Users\dartypc\AppData\Roaming\PyScripter\remserver.py", line 63, in <module>
if __name__ == "__main__":
main() <====
File "C:\Users\dartypc\AppData\Roaming\PyScripter\remserver.py", line 60, in main
t = SimpleServer(ModSlaveService, port = port, auto_register = False)
t.start() <====
if __name__ == "__main__":
File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\utils\server.py", line 227, in start
File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\utils\server.py", line 139, in accept
File "C:\Users\dartypc\AppData\Roaming\PyScripter\remserver.py", line 14, in _accept_method
class SimpleServer(Server):
def _accept_method(self, sock):
self._serve_client(sock, None) <====
class ModSlaveService(SlaveService):
File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\utils\server.py", line 191, in _serve_client
File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py", line 391, in serve_all
File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py", line 382, in serve
File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py", line 350, in _dispatch
File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py", line 298, in _dispatch_request
File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py", line 528, in _handle_call
File "<string>", line 420, in run_nodebug
File "C:\DATA\jff\data\python\stackoverflow\traceback_test.py", line 31, in <module>
print_trace()
foo() <====
EDIT: VPfB suggested the use of extract_stack which is a little less "hacky", no need to parse a string, just get the quadruplet with traceback info (needs to rebuild the text message, but that's better)
import traceback,os,sys
def print_trace():
# discard the 2 deepest entries since they're a call to print_trace()
for file,line,w1,w2 in traceback.extract_stack()[:-2]:
sys.stdout.write(' File "{}", line {}, in {}\n'.format(file,line,w1))
if os.path.exists(file):
line -= 1
with open(file,"r") as f:
rl = f.readlines()
tblines = rl[max(line-2,0):min(line+3,len(rl))]
# read 2 lines before and 2 lines after
for i,tl in enumerate(tblines):
tl = tl.rstrip()
if i==2:
sys.stdout.write(" "+tl+" <====\n")
elif tl:
sys.stdout.write(" "+tl+"\n")
def foo():
print_trace()
foo()

The traceback.format_exception_only function format only one line, except in case of SyntaxError, so…

Related

Autokey: sporadic error on system.exec_command

I have a script that grabs the window title and writes out a debug statement based on whether the file title includes a certain string.
active_title = window.get_active_title()
counter = system.exec_command("date '+%s'")
newcounter = counter[-5:]
counter = newcounter
time.sleep(.4)
if '.php' in active_title:
output = "die('<pre>[" + str(counter) + "] ' . date('Y-m-d H:i:s'));"
time.sleep(.2)
clipboard.fill_clipboard(output)
time.sleep(.2)
keyboard.send_keys('<ctrl>+v')
if '.js' in active_title:
output = "console.log('[" + str(counter) + "] ' + Date.now())"
time.sleep(.2)
clipboard.fill_clipboard(output)
time.sleep(.2)
keyboard.send_keys('<ctrl>+v')
time.sleep(.2)
keyboard.send_keys("<ctrl>+s")
99% of the time it works without issue, but that 100th time it throws this error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/autokey/service.py", line 485, in execute
exec(script.code, scope)
File "<string>", line 2, in <module>
File "/usr/lib/python3/dist-packages/autokey/scripting.py", line 1104, in get_active_title
return self.mediator.interface.get_window_title()
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1158, in get_window_title
return self.get_window_info(window, traverse).wm_title
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1074, in get_window_info
return self._get_window_info(window, traverse)
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1081, in _get_window_info
new_wm_class = self._try_get_window_class(window)
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1151, in _try_get_window_class
wm_class = window.get_wm_class()
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/xobject/drawable.py", line 660, in get_wm_class
d = self.get_full_property(Xatom.WM_CLASS, Xatom.STRING)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/xobject/drawable.py", line 452, in get_full_property
prop = self.get_property(property, type, 0, sizehint)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/xobject/drawable.py", line 435, in get_property
r = request.GetProperty(display = self.display,
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/rq.py", line 1481, in __init__
self.reply()
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/rq.py", line 1493, in reply
self._display.send_and_recv(request = self._serial)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 556, in send_and_recv
gotreq = self.parse_response(request)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 643, in parse_response
gotreq = self.parse_request_response(request) or gotreq
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 720, in parse_request_response
req = self.get_waiting_replyrequest()
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 847, in get_waiting_replyrequest
raise RuntimeError("Request reply to unknown request. Can't happen!")
RuntimeError: Request reply to unknown request. Can't happen!
Closing and restarting Autokey helps, but I'd like to avoid the error if at all possible. I have several scripts that get the window title, and this is the only script that has this issue. Is there something wrong with using system.exec_command?
Since you are invoking the clipboard, it helps to add to the start and the end
os.system("sleep .1; xsel -cb")
to clear it.

how to fix error about BERT?just confuse me few days

Traceback (most recent call last):
File "D:/desk/bert-master333/bert-master/run_classifier.py", line 1024, in <module>
tf.app.run()
File "D:\anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "D:\anaconda\envs\tensorflow\lib\site-packages\absl\app.py", line 303, in run
_run_main(main, args)
File "D:\anaconda\envs\tensorflow\lib\site-packages\absl\app.py", line 251, in _run_main
sys.exit(main(argv))
File "D:/desk/bert-master333/bert-master/run_classifier.py", line 885, in main
train_examples = processor.get_train_examples(FLAGS.data_dir)
File "D:/desk/bert-master333/bert-master/run_classifier.py", line 380, in get_train_examples
self._read_tsv(os.path.join(data_dir, "train.csv")), "train")
File "D:/desk/bert-master333/bert-master/run_classifier.py", line 410, in _create_examples
label = tokenization.convert_to_unicode(line[1])
IndexError: list index out of range
class limengnanProcessor(DataProcessor):
def get_train_examples(self, data_dir):
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "train.csv")), "train")
def get_dev_examples(self, data_dir):
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "dev.csv")), "dev")
def get_test_examples(self, data_dir):
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "test.csv")), "test")
def get_labels(self):
return ['0','1']
def _read_tsv(cls, input_file, quotechar=None):
with tf.gfile.Open(input_file, "r") as f:
reader = csv.reader(f, delimiter="&", quotechar=quotechar)
lines = []
for line in reader:
lines.append(line)
return lines
def _create_examples(self, lines, set_type):
examples = []
for (i, line) in enumerate(lines):
guid = "%s-%s" % (set_type, i)
if set_type == "test":
text_a = tokenization.convert_to_unicode(line[0])
label = "0"
else:
text_a = tokenization.convert_to_unicode(line[0])
label = tokenization.convert_to_unicode(line[1])
if label not in ['0','1']:
continue
examples.append(
InputExample(guid=guid, text_a=text_a, text_b=None, label=label))
return examples

MemoryError when loading model

When I try to load the model (input, not meta-model), it returns a MemoryError about 30 seconds after executing.
Expected: List of tree: [{'type':'func', 'callee':'print', 'args':[['Hello']]}]
Actual: MemoryError
Output
Traceback (most recent call last):
File "C:/Users/kenxs/PycharmProjects/program/program/parser.py", line 103, in <module>
main()
File "C:/Users/kenxs/PycharmProjects/program/program/parser.py", line 97, in main
program.do_it(True, True, True)
File "C:/Users/kenxs/PycharmProjects/program/program/parser.py", line 80, in do_it
if cont and intp: cont, err = self.interpret()
File "C:/Users/kenxs/PycharmProjects/program/program/parser.py", line 67, in interpret
self.model = self.mm.model_from_file(os.path.abspath('program.program'))
File "C:\Program Files (x86)\Python38-32\lib\site-packages\textx\metamodel.py", line 574, in model_from_file
return self.internal_model_from_file(file_name, encoding, debug)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\textx\metamodel.py", line 613, in internal_model_from_file
model = self._parser_blueprint.clone().get_model_from_str(
File "C:\Program Files (x86)\Python38-32\lib\site-packages\textx\model.py", line 262, in get_model_from_str
self.parse(model_str, file_name=file_name)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 1493, in parse
self.parse_tree = self._parse()
File "C:\Program Files (x86)\Python38-32\lib\site-packages\textx\model.py", line 221, in _parse
return self.parser_model.parse(self)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 286, in parse
result = self._parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 365, in _parse
result = e.parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 286, in parse
result = self._parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 365, in _parse
result = e.parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 286, in parse
result = self._parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 481, in _parse
result = p(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 286, in parse
result = self._parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 404, in _parse
result = e.parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 286, in parse
result = self._parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 365, in _parse
result = e.parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 286, in parse
result = self._parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 365, in _parse
result = e.parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 286, in parse
result = self._parse(parser)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arpeggio\__init__.py", line 484, in _parse
append(result)
MemoryError
Grammar
Program:
commands*=Command
;
Command:
Statement | Function | Definition
;
Statement:
callee=ID '(' checker=Checker ')' '=' effect=Collection Ending
;
Checker:
a=Object sign=CheckerSign b=Object
;
CheckerSign:
'==' | '!='
;
Collection:
'[' objs*=PseudoObject ']'
;
PseudoObject:
Object Ending
;
Function:
callee=ID '(' args=Arguments ')' Ending
;
Arguments:
arg*=Argument
;
Argument:
NamedArgument | UnnamedArgument
;
NamedArgument:
a=Object '=' b=Object
;
UnnamedArgument:
a=Object
;
Definition:
a=Object '=' b=Object
;
Object:
a*=ObjectChild
;
ObjectChild:
ObjectChildChild ( '.' | '' )
;
ObjectChildChild:
String | ID | INT | STRICTFLOAT | BOOL | Collection | Function
;
String:
'"' ID '"'
;
Comment:
/#.*/ Ending
;
Ending:
'' *Newline
;
Newline:
( '\n' | ';' )
;
Program
import os
from textx import *
from textx.export import *
class Parser(object):
def __init__(self, meta_model_path='grammar.tx', model_str='print("Hello")'):
self.tree = []
self.meta_model_path = os.path.abspath(meta_model_path)
self.model_str = model_str
self.mm = None
self.model = None
def __str__(self):
return str(self.tree)
def _interpret_function(self, c):
result = {}
result['type'] = 'func'
result['callee'] = c.callee
result['args'] = []
for arg in c.args.arg:
if arg.__class__.__name__ == 'UnnamedArgument':
result['args'].append([arg.a.a])
elif arg.__class__.__name__ == 'NamedArgument':
result['args'].append([arg.a.a, arg.b.a])
return result
def _interpret_definition(self, c):
result = {}
result['type'] = 'defi'
result['a'] = c.a.a
result['b'] = c.b.a
return result
def _interpret_statement(self, c):
result = {}
result['type'] = 'stat'
result['callee'] = c.callee
result['checker_a'] = c.checker.a
result['checker_b'] = c.checker.b
result['checker_sign'] = c.checker.sign
result['effect'] = c.effect.objs
return result
def _interpret(self, model):
for c in model.commands:
if c.__class__.__name__ == 'Statement':
self.tree.append(self._interpret_statement(c))
elif c.__class__.__name__ == 'Function':
self.tree.append(self._interpret_function(c))
elif c.__class__.__name__ == 'Definition':
self.tree.append(self._interpret_definition(c))
def export_meta_model(self, mm):
metamodel_export(self.mm, os.path.abspath('grammar.dot'))
return [True, None]
def export_model(self, model):
model_export(self.model, os.path.abspath('program.dot'))
return [True, None]
def interpret(self):
print(-1)
self.mm = metamodel_from_file(self.meta_model_path, debug=False)
print(0)
try:
self.model = self.mm.model_from_str(self.model_str)
# self.model = self.mm.model_from_file(os.path.abspath('program.prg'))
except TextXSyntaxError as err:
print('Syntax Error # {}:{}'.format(err.line, err.col))
print('{}'.format(err.message))
return [False, err]
print(1)
self._interpret(model)
print(2)
return [True, None]
def do_it(self, exp_mm=False, exp_m=False, intp=True): # My naming skills :)
cont = True
err = None
if cont and intp: cont, err = self.interpret()
if cont and exp_mm: cont, err = self.export_meta_model()
if cont and exp_m: cont, err = self.export_model()
def main(debug=False):
print('Program')
program = Parser()
print('Inp Done')
program.do_it(True, True, True)
print('Done')
print(program)
if __name__ == "__main__":
main()
Rule Ending has zero or more empty string match ''* that is essentially an infinite loop building a parse tree node with an infinite number of empty match terminals. Eventually, the parse tree eats up all the memory and you get MemoryError.
In general, repetitions ('*', '+') over a parsing expression that could potentially be an empty match could lead to an infinite loop.
I suggest that you register an issue in the issue tracker for this as it should be fairly easy to at least detect it at runtime without to much overhead.

ignore empty lines and grep lines with match words while reading files

I am trying to write sample code which will read from a file but
a)Ignore empty line
b) will only show lines which starts with dm-
but its giving me error, not sure what to do ,
can any one please give me some light
def _find_dm_name():
with open (IOSTAT_OUTPUT,'r')as f:
for line in f:
lines = (line.rstrip() for line in f)
lines = list(line for line in lines if line)
if re.match("(dm-)", lines):
content=lines
return content
if __name__ == '__main__':
dm_name=_find_dm_name()
print dm_name
Traceback (most recent call last):
File "test.py", line 47, in <module>
dm_name=_find_dm_name()
File "test.py", line 41, in _find_dm_name
if re.match("(.*)", lines):
File "/usr/lib64/python2.6/re.py", line 137, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
Even though if i try this
def _find_dm_name():
with open (IOSTAT_OUTPUT,'r')as f:
for line in f:
if re.match("(dm-*)", line):
content=line
return content
it gives me only the last line
but how do I get all the line which match only dm- + ignore any empty lines

ValueError: I/O operation on closed file in a function of Python

I have my nested "final" function to save a text file with append mode. When i execute selection in console mode (with PyCharm) i get this message
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2721, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-62-9c259f95cff2>", line 1, in <module>
f.write(line)
ValueError: I/O operation on closed file
0
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2721, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-63-f015ac76386b>", line 12, in <module>
f.write(line)
ValueError: I/O operation on closed file
my function is the following.
def segmentation_accuracy(reference, segmented, output, method="ke", threshold=0.1, sep="space", header=True):
if not check_suffix(reference) or not check_suffix(segmented):
raise ValueError('the input files need to be ESRI shapefile (*.shp)')
ref_shp = ogr.Open(os.path.abspath(reference))
if ref_shp is None:
raise SystemExit('Unable to open %s' % reference)
seg_shp = ogr.Open(os.path.abspath(segmented))
if seg_shp is None:
raise SystemExit('Unable to open %s' % segmented)
if method == "ke" or method == "pu":
if threshold is None:
raise AccuracyException("threshold need to be set with a value between 0.0 and 1.0")
ref_layer = ref_shp.GetLayer()
ref_layer_num = ref_layer.GetFeatureCount()
seg_layer = seg_shp.GetLayer()
seg_layer_num = seg_layer.GetFeatureCount()
if ref_layer.GetGeomType() != 3:
raise ValueError("s% is not a single-part polygons" % (os.path.split(reference)[1]))
if ref_layer.GetGeomType() != 3:
raise ValueError("s% is not a single-part polygons" % (os.path.split(segmented)[1]))
# get shapely polygons
ref_list = polygon_list(ref_layer)
seg_list = polygon_list(seg_layer)
# create text file
ref_path = os.path.split(os.path.abspath(reference))[0]
filename = set_outputfile(output, path=ref_path)
with open(filename, "a") as f:
if header is True:
line = ["FID", "centroidX", "centroidY", "area", "perimeter", "segments", "ra_or", "ra_os", "over_seg", \
"under_seg", "over_merge", "under_merge", "sim_size", "sim_size_std", "AFI", "qr", "seg_error", \
"d_sr", "d_max", "rp_sr"]
line = sepType[sep].join([str(e) for e in line])+ "\n"
f.write(line)
for FID, ref in enumerate(ref_list):
print FID
seg_overlap = list()
for seg in seg_list:
if ref.intersects(seg):
if threshold_type[method](ref, seg, threshold):
seg_overlap.append(seg)
if len(seg_overlap) != 0:
accuracy = Accuracy(ref, seg_overlap)
line = [FID] + accuracy.data
line = sepType[sep].join([str(e) for e in line])+ "\n"
f.write(line)
else:
accuracy = Accuracy(ref)
line = [FID] + accuracy.data
line = sepType[sep].join([str(e) for e in line])+ "\n"
f.write(line)
The error is related with the line
line = ["FID", "centroidX", "centroidY", "area", "perimeter", "segments", "ra_or", "ra_os", "over_seg", \
"under_seg", "over_merge", "under_merge", "sim_size", "sim_size_std", "AFI", "qr", "seg_error", \
"d_sr", "d_max", "rp_sr"]
pycharm prefer the following form
line = (["FID", "centroidX", "centroidY", "area", "perimeter", "segments", "ra_or", "ra_os", "over_seg",
"under_seg", "over_merge", "under_merge", "sim_size", "sim_size_std", "AFI", "qr", "seg_error",
"d_sr", "d_max", "rp_sr"])

Categories

Resources