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

Skip to content

Commit 5e38b6f

Browse files
committed
handle class exceptions; added runeval; made runctx obsolete
1 parent 051ab12 commit 5e38b6f

6 files changed

Lines changed: 88 additions & 29 deletions

File tree

Lib/bdb.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,36 +270,59 @@ def format_stack_entry(self, frame_lineno, lprefix=': '):
270270
# The following two methods can be called by clients to use
271271
# a debugger to debug a statement, given as a string.
272272

273-
def run(self, cmd):
274-
import __main__
275-
dict = __main__.__dict__
276-
self.runctx(cmd, dict, dict)
273+
def run(self, cmd, globals=None, locals=None):
274+
if globals is None:
275+
import __main__
276+
globals = __main__.__dict__
277+
if locals is None:
278+
locals = globals
279+
self.reset()
280+
sys.settrace(self.trace_dispatch)
281+
try:
282+
try:
283+
exec cmd + '\n' in globals, locals
284+
except BdbQuit:
285+
pass
286+
finally:
287+
self.quitting = 1
288+
sys.settrace(None)
277289

278-
def runctx(self, cmd, globals, locals):
290+
def runeval(self, expr, globals=None, locals=None):
291+
if globals is None:
292+
import __main__
293+
globals = __main__.__dict__
294+
if locals is None:
295+
locals = globals
279296
self.reset()
280297
sys.settrace(self.trace_dispatch)
281298
try:
282299
try:
283-
exec(cmd + '\n', globals, locals)
300+
return eval(expr + '\n', globals, locals)
284301
except BdbQuit:
285302
pass
286303
finally:
287304
self.quitting = 1
288305
sys.settrace(None)
289306

307+
def runctx(self, cmd, globals, locals):
308+
# B/W compatibility
309+
self.run(cmd, globals, locals)
310+
290311
# This method is more useful to debug a single function call.
291312

292313
def runcall(self, func, *args):
293314
self.reset()
294315
sys.settrace(self.trace_dispatch)
316+
res = None
295317
try:
296318
try:
297-
apply(func, args)
319+
res = apply(func, args)
298320
except BdbQuit:
299321
pass
300322
finally:
301323
self.quitting = 1
302324
sys.settrace(None)
325+
return res
303326

304327

305328
def set_trace():

Lib/lib-stdwin/wdb.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
7575
# This function is called if an exception occurs,
7676
# but only if we are to stop at or just below this level
7777
frame.f_locals['__exception__'] = exc_type, exc_value
78-
self.settitle(exc_type + ': ' + repr.repr(exc_value))
78+
if type(exc_type) == type(''):
79+
exc_type_name = exc_type
80+
else: exc_type_name = exc_type.__name__
81+
self.settitle(exc_type_name + ': ' + repr.repr(exc_value))
7982
stdwin.fleep()
8083
self.interaction(frame, exc_traceback)
8184
if not self.closed:
@@ -271,19 +274,23 @@ def draw(self, detail):
271274

272275
# Simplified interface
273276

274-
def run(statement):
277+
def run(statement, globals=None, locals=None):
275278
x = Wdb()
276-
try: x.run(statement)
279+
try: x.run(statement, globals, locals)
277280
finally: x.close()
278281

279-
def runctx(statement, globals, locals):
282+
def runeval(expression, globals=None, locals=None):
280283
x = Wdb()
281-
try: x.runctx(statement, globals, locals)
284+
try: return x.runeval(expression, globals, locals)
282285
finally: x.close()
283286

287+
def runctx(statement, globals, locals):
288+
# B/W compatibility
289+
run(statement, globals, locals)
290+
284291
def runcall(*args):
285292
x = Wdb()
286-
try: apply(x.runcall, args)
293+
try: return apply(x.runcall, args)
287294
finally: x.close()
288295

289296
def set_trace():

Lib/lib-stdwin/wdbframewin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ def re_eval(self):
100100
value = eval(expr, globals, locals)
101101
output = repr.repr(value)
102102
except:
103-
output = sys.exc_type + ': ' + `sys.exc_value`
103+
if type(sys.exc_type) == type(''):
104+
exc_type_name = sys.exc_type
105+
else: exc_type_name = sys.exc_type.__name__
106+
output = exc_type_name + ': ' + `sys.exc_value`
104107
self.displaylist[1] = output
105108
lh = stdwin.lineheight()
106109
r = (-10, 0), (30000, 2*lh)

Lib/pdb.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
5555
# This function is called if an exception occurs,
5656
# but only if we are to stop at or just below this level
5757
frame.f_locals['__exception__'] = exc_type, exc_value
58-
print exc_type + ':', repr.repr(exc_value)
58+
if type(exc_type) == type(''):
59+
exc_type_name = exc_type
60+
else: exc_type_name = exc_type.__name__
61+
print exc_type_name + ':', repr.repr(exc_value)
5962
self.interaction(frame, exc_traceback)
6063

6164
# General interaction function
@@ -74,7 +77,10 @@ def default(self, line):
7477
try:
7578
exec(line + '\n', globals, locals)
7679
except:
77-
print '***', sys.exc_type + ':', sys.exc_value
80+
if type(sys.exc_type) == type(''):
81+
exc_type_name = sys.exc_type
82+
else: exc_type_name = sys.exc_type.__name__
83+
print '***', exc_type_name + ':', sys.exc_value
7884

7985
# Command definitions, called by cmdloop()
8086
# The argument is the remaining string on the command line
@@ -199,7 +205,10 @@ def do_p(self, arg):
199205
value = eval(arg, self.curframe.f_globals, \
200206
self.curframe.f_locals)
201207
except:
202-
print '***', sys.exc_type + ':', `sys.exc_value`
208+
if type(sys.exc_type) == type(''):
209+
exc_type_name = sys.exc_type
210+
else: exc_type_name = sys.exc_type.__name__
211+
print '***', exc_type_name + ':', `sys.exc_value`
203212
return
204213

205214
print `value`
@@ -254,7 +263,10 @@ def do_whatis(self, arg):
254263
value = eval(arg, self.curframe.f_globals, \
255264
self.curframe.f_locals)
256265
except:
257-
print '***', sys.exc_type + ':', `sys.exc_value`
266+
if type(sys.exc_type) == type(''):
267+
exc_type_name = sys.exc_type
268+
else: exc_type_name = sys.exc_type.__name__
269+
print '***', exc_type_name + ':', `sys.exc_value`
258270
return
259271
code = None
260272
# Is it a function?
@@ -429,14 +441,18 @@ def help_pdb(self):
429441

430442
# Simplified interface
431443

432-
def run(statement):
433-
Pdb().run(statement)
444+
def run(statement, globals=None, locals=None):
445+
Pdb().run(statement, globals, locals)
446+
447+
def runeval(expression, globals=None, locals=None):
448+
return Pdb().runeval(expression, globals, locals)
434449

435450
def runctx(statement, globals, locals):
436-
Pdb().runctx(statement, globals, locals)
451+
# B/W compatibility
452+
run(statement, globals, locals)
437453

438454
def runcall(*args):
439-
apply(Pdb().runcall, args)
455+
return apply(Pdb().runcall, args)
440456

441457
def set_trace():
442458
Pdb().set_trace()

Lib/stdwin/wdb.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
7575
# This function is called if an exception occurs,
7676
# but only if we are to stop at or just below this level
7777
frame.f_locals['__exception__'] = exc_type, exc_value
78-
self.settitle(exc_type + ': ' + repr.repr(exc_value))
78+
if type(exc_type) == type(''):
79+
exc_type_name = exc_type
80+
else: exc_type_name = exc_type.__name__
81+
self.settitle(exc_type_name + ': ' + repr.repr(exc_value))
7982
stdwin.fleep()
8083
self.interaction(frame, exc_traceback)
8184
if not self.closed:
@@ -271,19 +274,23 @@ def draw(self, detail):
271274

272275
# Simplified interface
273276

274-
def run(statement):
277+
def run(statement, globals=None, locals=None):
275278
x = Wdb()
276-
try: x.run(statement)
279+
try: x.run(statement, globals, locals)
277280
finally: x.close()
278281

279-
def runctx(statement, globals, locals):
282+
def runeval(expression, globals=None, locals=None):
280283
x = Wdb()
281-
try: x.runctx(statement, globals, locals)
284+
try: return x.runeval(expression, globals, locals)
282285
finally: x.close()
283286

287+
def runctx(statement, globals, locals):
288+
# B/W compatibility
289+
run(statement, globals, locals)
290+
284291
def runcall(*args):
285292
x = Wdb()
286-
try: apply(x.runcall, args)
293+
try: return apply(x.runcall, args)
287294
finally: x.close()
288295

289296
def set_trace():

Lib/stdwin/wdbframewin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ def re_eval(self):
100100
value = eval(expr, globals, locals)
101101
output = repr.repr(value)
102102
except:
103-
output = sys.exc_type + ': ' + `sys.exc_value`
103+
if type(sys.exc_type) == type(''):
104+
exc_type_name = sys.exc_type
105+
else: exc_type_name = sys.exc_type.__name__
106+
output = exc_type_name + ': ' + `sys.exc_value`
104107
self.displaylist[1] = output
105108
lh = stdwin.lineheight()
106109
r = (-10, 0), (30000, 2*lh)

0 commit comments

Comments
 (0)