Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d57c5c8

Browse files
author
Victor Stinner
committed
libpython.py (gdb) now catchs IOError in py-list and py-bt commands
py-list displays the error. py-bt ignores the error (the filename and line number is already displayed).
1 parent 98b3722 commit d57c5c8

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

Tools/gdb/libpython.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,11 @@ def current_line(self):
905905
if self.is_optimized_out():
906906
return '(frame information optimized out)'
907907
filename = self.filename()
908-
with open(os_fsencode(filename), 'r') as f:
908+
try:
909+
f = open(os_fsencode(filename), 'r')
910+
except IOError:
911+
return None
912+
with f:
909913
all_lines = f.readlines()
910914
# Convert from 1-based current_line_num to 0-based list offset:
911915
return all_lines[self.current_line_num()-1]
@@ -1430,7 +1434,9 @@ def print_summary(self):
14301434
if pyop:
14311435
line = pyop.get_truncated_repr(MAX_OUTPUT_LEN)
14321436
write_unicode(sys.stdout, '#%i %s\n' % (self.get_index(), line))
1433-
sys.stdout.write(pyop.current_line())
1437+
line = pyop.current_line()
1438+
if line is not None:
1439+
sys.stdout.write(line)
14341440
else:
14351441
sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index())
14361442
else:
@@ -1441,7 +1447,9 @@ def print_traceback(self):
14411447
pyop = self.get_pyop()
14421448
if pyop:
14431449
pyop.print_traceback()
1444-
sys.stdout.write(' %s\n' % pyop.current_line().strip())
1450+
line = pyop.current_line()
1451+
if line is not None:
1452+
sys.stdout.write(' %s\n' % line.strip())
14451453
else:
14461454
sys.stdout.write(' (unable to read python frame information)\n')
14471455
else:
@@ -1501,7 +1509,13 @@ def invoke(self, args, from_tty):
15011509
if start<1:
15021510
start = 1
15031511

1504-
with open(os_fsencode(filename), 'r') as f:
1512+
try:
1513+
f = open(os_fsencode(filename), 'r')
1514+
except IOError as err:
1515+
sys.stdout.write('Unable to open %s: %s\n'
1516+
% (filename, err))
1517+
return
1518+
with f:
15051519
all_lines = f.readlines()
15061520
# start and end are 1-based, all_lines is 0-based;
15071521
# so [start-1:end] as a python slice gives us [start, end] as a

0 commit comments

Comments
 (0)