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

Skip to content

Commit 8cd0def

Browse files
committed
M PyShell.py
M rpc.py SF Bug 676398 Doesn't handle non-built-in exceptions 1. Move exception formatting to the subprocess; allows subclassing of exceptions, including subclasses created in the shell without introducing excessive complexity in the RPC mechanism. 2. Provide access to linecache from subprocess to support this.
1 parent d174068 commit 8cd0def

2 files changed

Lines changed: 42 additions & 84 deletions

File tree

Lib/idlelib/PyShell.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ def start_subprocess(self):
353353
self.rpcclt.register("stdout", self.tkconsole.stdout)
354354
self.rpcclt.register("stderr", self.tkconsole.stderr)
355355
self.rpcclt.register("flist", self.tkconsole.flist)
356+
self.rpcclt.register("linecache", linecache)
356357
self.transfer_path()
357358
self.poll_subprocess()
358359

@@ -404,23 +405,6 @@ def poll_subprocess(self):
404405
if what is not None:
405406
print >>console, `what`
406407
elif how == "EXCEPTION":
407-
mod, name, args, tb = what
408-
print >>console, 'Traceback (most recent call last):'
409-
exclude = ("run.py", "rpc.py", "RemoteDebugger.py", "bdb.py")
410-
self.cleanup_traceback(tb, exclude, console)
411-
traceback.print_list(tb, file=console)
412-
# try to reinstantiate the exception, stuff in the args:
413-
try:
414-
etype = eval(mod + '.' + name)
415-
val = etype()
416-
val.args = args
417-
except TypeError: # string exception!
418-
etype = name
419-
val = args
420-
lines = traceback.format_exception_only(etype, val)
421-
for line in lines[:-1]:
422-
traceback._print(console, line, '')
423-
traceback._print(console, lines[-1], '')
424408
if self.tkconsole.getvar("<<toggle-jit-stack-viewer>>"):
425409
self.remote_stack_viewer()
426410
elif how == "ERROR":
@@ -430,36 +414,6 @@ def poll_subprocess(self):
430414
# we received a response to the currently active seq number:
431415
self.tkconsole.endexecuting()
432416

433-
def cleanup_traceback(self, tb, exclude, console):
434-
"Remove excluded traces from beginning/end of tb; get cached lines"
435-
orig_tb = tb[:]
436-
while tb:
437-
for rpcfile in exclude:
438-
if tb[0][0].count(rpcfile):
439-
break # found an exclude, break for: and delete tb[0]
440-
else:
441-
break # no excludes, have left RPC code, break while:
442-
del tb[0]
443-
while tb:
444-
for rpcfile in exclude:
445-
if tb[-1][0].count(rpcfile):
446-
break
447-
else:
448-
break
449-
del tb[-1]
450-
if len(tb) == 0:
451-
# error was in IDLE internals, don't prune!
452-
tb[:] = orig_tb[:]
453-
print>>sys.__stderr__, "** IDLE Internal Error: ", tb
454-
print>>console, "** IDLE Internal Error **"
455-
for i in range(len(tb)):
456-
fn, ln, nm, line = tb[i]
457-
if nm == '?':
458-
nm = "-toplevel-"
459-
if not line and fn.startswith("<pyshell#"):
460-
line = linecache.getline(fn, ln)
461-
tb[i] = fn, ln, nm, line
462-
463417
def kill_subprocess(self):
464418
clt = self.rpcclt
465419
self.rpcclt = None

Lib/idlelib/rpc.py

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,49 @@ def localcall(self, request):
154154
ret = remoteref(ret)
155155
return ("OK", ret)
156156
except:
157-
##traceback.print_exc(file=sys.__stderr__)
157+
self.debug("localcall:EXCEPTION")
158+
efile = sys.stderr
158159
typ, val, tb = info = sys.exc_info()
159160
sys.last_type, sys.last_value, sys.last_traceback = info
160-
if isinstance(typ, type(Exception)):
161-
# Class exception
162-
mod = typ.__module__
163-
name = typ.__name__
164-
if issubclass(typ, Exception):
165-
args = val.args
166-
else:
167-
args = (str(val),)
161+
tbe = traceback.extract_tb(tb)
162+
print >>efile, 'Traceback (most recent call last):'
163+
exclude = ("run.py", "rpc.py", "RemoteDebugger.py", "bdb.py")
164+
self.cleanup_traceback(tbe, exclude)
165+
traceback.print_list(tbe, file=efile)
166+
lines = traceback.format_exception_only(typ, val)
167+
for line in lines:
168+
print>>efile, line,
169+
return ("EXCEPTION", None)
170+
171+
def cleanup_traceback(self, tb, exclude):
172+
"Remove excluded traces from beginning/end of tb; get cached lines"
173+
orig_tb = tb[:]
174+
while tb:
175+
for rpcfile in exclude:
176+
if tb[0][0].count(rpcfile):
177+
break # found an exclude, break for: and delete tb[0]
168178
else:
169-
# User string exception
170-
mod = None
171-
name = typ
172-
if val is None: val = ''
173-
args = str(val)
174-
tb = traceback.extract_tb(tb)
175-
self.debug("localcall:EXCEPTION: ", mod, name, args, tb)
176-
return ("EXCEPTION", (mod, name, args, tb))
179+
break # no excludes, have left RPC code, break while:
180+
del tb[0]
181+
while tb:
182+
for rpcfile in exclude:
183+
if tb[-1][0].count(rpcfile):
184+
break
185+
else:
186+
break
187+
del tb[-1]
188+
if len(tb) == 0:
189+
# error was in RPC internals, don't prune!
190+
tb[:] = orig_tb[:]
191+
print>>sys.stderr, "** RPC Internal Error: ", tb
192+
for i in range(len(tb)):
193+
fn, ln, nm, line = tb[i]
194+
if nm == '?':
195+
nm = "-toplevel-"
196+
if not line and fn.startswith("<pyshell#"):
197+
line = self.remotecall('linecache', 'getline',
198+
(fn, ln), {})
199+
tb[i] = fn, ln, nm, line
177200

178201
def remotecall(self, oid, methodname, args, kwargs):
179202
self.debug("calling asynccall via remotecall")
@@ -198,26 +221,7 @@ def decoderesponse(self, response):
198221
if how == "OK":
199222
return what
200223
if how == "EXCEPTION":
201-
self.debug("decoderesponse: EXCEPTION:", what)
202-
mod, name, args, tb = what
203-
self.traceback = tb
204-
if mod: # not string exception
205-
try:
206-
__import__(mod)
207-
module = sys.modules[mod]
208-
except ImportError:
209-
pass
210-
else:
211-
try:
212-
cls = getattr(module, name)
213-
except AttributeError:
214-
pass
215-
else:
216-
# instantiate a built-in exception object and raise it
217-
raise getattr(__import__(mod), name)(*args)
218-
name = mod + "." + name
219-
# do the best we can:
220-
raise name, args
224+
raise Exception, "RPC SocketIO.decoderesponse exception"
221225
if how == "ERROR":
222226
self.debug("decoderesponse: Internal ERROR:", what)
223227
raise RuntimeError, what

0 commit comments

Comments
 (0)