| Reply | « Previous Thread | Next Thread » |
|
Do you have any ideas to handle unexpected errors raised by functions which are called by users? Here is a simple example.
Code:
import sys
import e32
import appuifw
def raise_nameerror():
"""Raise NameError"""
# foo = u'bar'
foo
def main():
"""Main thread."""
script_lock = e32.Ao_lock()
appuifw.app.title = u'RaiseErrorTest'
appuifw.app.body = appuifw.Text()
appuifw.app.menu = [(u'Raise NameError', raise_nameerror),
(u'Exit', script_lock.signal)]
appuifw.app.exit_key_handler = script_lock.signal
script_lock.wait()
if __name__ == '__main__':
try:
main()
except:
appuifw.note(unicode(str(sys.exc_info()[0])), 'error')
Meanwhile, if the functions containing error are called by script itself (not by users), error handling works fine. Code:
import sys
import e32
import appuifw
def raise_nameerror():
"""Raise NameError"""
# foo = u'bar'
foo
def main():
"""Main thread."""
raise_nameerror()
if __name__ == '__main__':
try:
main()
except:
appuifw.note(unicode(str(sys.exc_info()[0])), 'error')
![]() I've referred to the article below, but this doesn't help me. So, if you have any good ideas to debug scripts quickly and efficiently, please let me know. Python debugging techniques [Forum Nokia Wiki] |
|
Exceptions from callbacks are not propagated to the "main thread" (in quotes because no real threading is going on in the background, only Symbian active objects).
You can wrap each callback in a function that collects the exception in a global list and signals the lock you're waiting. You can then handle exceptions collected in the list sequentially when the lock is signalled. Here's the concept in code. It's crude and I have not tested it myself. Code:
import traceback
# Exceptions from callbacks are collected here.
exceptions = []
# Decorator for callback functions
def collect_exception(func):
# Using Python (>v2.2) nested scopes.
def callit(*args, **kwds):
global script_lock
try:
return func(*args, **kwds)
except:
excstr = "\n".join(traceback.format_exception(*sys.exc_info()))
exceptions.append(excstr) # Store exception string in a list.
script_lock.signal() # Notify main function about exception.
raise # Re-raise exception, just in case.
return callit
...
def main():
global exceptions, script_lock
script_lock = e32.Ao_lock()
...
appuifw.app.menu = [(u'Raise NameError',
collect_exception(raise_nameerror)),
(u'Exit',
script_lock.signal)]
...
script_lock.wait()
# Report exceptions. Could be more than one, in theory.
for excstr in exceptions:
appuifw.note(unicode(excstr), 'error')
exceptions = [] # All exceptions accounted for, clear list.
Last edited by jethro.fn : 2008-08-21 at 07:37.
Reason: Syntax error
|
|
Quote:
Try this little module HERE and follow this method I hope you have success with it :-) BR Cyke64 pys60 1.4.5,1.9.7,pygame,PyS60 CE on E90 , N810 with Python 2.5.2 and ... last PyS60 1.9.7 with touch ui on 5800 ! pys60 extension modules on http://cyke64.googlepages.com/ |
|
Thank you for your quick replies.
I've integrated your two approaches into my sample script. (in order to show you a whole script) Code:
import sys
import e32
import appuifw
import linecache
# Dubugging Level (0: Release, 1: Show Traceback, 2: Trace All)
debuglevel = 1
# Exceptions from callbacks are stored here.
exceptions = []
class Trace:
"""Class for tracing script
This class is developed by cyke64.
Lisenced under GNU GPL.
"""
def __init__(self, runfile, f_all=u'E:\\Python\\traceit.txt',
f_main=u'E:\\Python\\traceitmain.txt'):
self.out_all=open(f_all, 'w')
self.out_main=open(f_main, 'w')
self.runfile = runfile
def go(self):
sys.settrace(self.traceit)
def stop(self):
sys.settrace(None)
self.out_all.close()
self.out_main.close()
def traceit(self, frame, event, arg):
lineno = frame.f_lineno
name = frame.f_globals['__name__']
if (frame.f_globals).has_key('__file__'):
file_trace=frame.f_globals['__file__']
line = linecache.getline(file_trace, lineno)
else:
file_trace = self.runfile
line = linecache.getline(file_trace, lineno)
self.out_main.write('%s*%s*\n*%s*\n' \
% (event, lineno, line.rstrip()))
self.out_all.write('%s*%s*of %s(%s)\n*%s*\n' \
%(event, lineno, name, file_trace, line.rstrip()))
e32.ao_sleep(0)
return self.traceit
def call_callback(func):
"""Catch exeption
This function is developed by jethro.fn.
"""
def call_func(*args, **kwds):
import traceback
global exceptions
global script_lock
try:
return func(*args, **kwds)
except:
# Collect Exceptions
exception = ''.join(traceback.format_exception(*sys.exc_info()))
exceptions.append(exception)
# Signal lock in main thread (immediate termination)
if debuglevel == 2: script_lock.signal()
return call_func
def raise_nameerror():
"""Raise NameError"""
# foo = u'bar'
foo
def main():
"""Main thread"""
global script_lock
global exceptions
# main UI
script_lock = e32.Ao_lock()
appuifw.app.title = u'RaiseErrorTest'
appuifw.app.body = appuifw.Text()
appuifw.app.menu = [(u'Raise NameError',
call_callback(raise_nameerror)),
(u'Exit', script_lock.signal)]
appuifw.app.exit_key_handler = script_lock.signal
script_lock.wait()
# Handling exception
if debuglevel and exceptions:
show_traceback(exceptions)
exceptions = []
def show_traceback(exceptions):
"""Show traceback"""
appuifw.app.title = u'Traceback'
traceback = '%d error(s) occurred.\n\n' % len(exceptions)
for exception in exceptions:
traceback += '%s\n' % exception
body = appuifw.Text(unicode(traceback))
body.set_pos(0)
appuifw.app.body = body
appuifw.app.menu = [(u'Exit', script_lock.signal)]
script_lock.wait()
if __name__ == '__main__':
try:
if debuglevel == 2:
trace = Trace('E:\\Python\\RaiseErrorTest.py')
trace.go()
main()
trace.stop()
else:
main()
except:
raise
and surely helps me develop my applications.Thank you again for your advices! Maybe I have to learn more and more about Python ![]() |
|
Wow. Seems pretty thorough. You wont miss a single exception with that thing...
|
|
Quote:
Now we have a complete debugging tool because of Hiisi ![]() You have a great blog on PyS60 Could you announce your most useful free applications(PyTask Server,PyBSLog and WAPlib) on DiBo ? You could also add them to wiki application I have added both debugging method + your very useful mixed method to the python debugging technics wiki page ! ![]() Happy debugging ![]() Cyke64 pys60 1.4.5,1.9.7,pygame,PyS60 CE on E90 , N810 with Python 2.5.2 and ... last PyS60 1.9.7 with touch ui on 5800 ! pys60 extension modules on http://cyke64.googlepages.com/
Last edited by cyke64 : 2008-08-22 at 11:53.
Reason: add three debugging methods to wiki !
|
|
Quote:
I don't know how to announce my applications on this discussion board. (I'm also new to this community.) Could you tell me how to do that? Thank you for your encouragement, cyke64 ![]() |
|
Join Date: Feb 2008
Posts: 2,543
Location: Bhavnagar, Gujarat, India
gaba88
Online
Forum Nokia Champion
|
|
|
Quote:
first of all congrats for your so nice applications .and its very easy to announce something in the discussion boards. Just start a new thread with the title of your new application and thats all. ![]() Enjoy Pythoning Gaba88 Gargi Das- http://gargidas.blogsot.com Forum Nokia Python Wiki Learn Python at http://mobapps.org/PyS60 |
|
Quote:
Code:
[announce] PyTwitterS60 0.0.2 : simplest Twitter client on PyS60 ![]() Happy pythoning Cyke64 pys60 1.4.5,1.9.7,pygame,PyS60 CE on E90 , N810 with Python 2.5.2 and ... last PyS60 1.9.7 with touch ui on 5800 ! pys60 extension modules on http://cyke64.googlepages.com/ |
|
Thank you, gaba88 and cyke64.
I will try it later ![]() |
|
Join Date: Feb 2008
Posts: 2,543
Location: Bhavnagar, Gujarat, India
gaba88
Online
Forum Nokia Champion
|
|
hi hiisi
we all will be waiting for your application Enjoy Pythoning Gaba88 Gargi Das- http://gargidas.blogsot.com Forum Nokia Python Wiki Learn Python at http://mobapps.org/PyS60 |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Rate This Thread | |
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| A problem with debugging Helloworldbasic on the mobilephone | lovemydoog | Carbide.c++ IDE and plug-ins | 23 | 2008-07-16 09:31 |
| debugging multi language sis problem | smui | Carbide.c++ IDE and plug-ins | 4 | 2008-05-31 00:06 |
| problem related to multi media file handling | deepakk | General Symbian C++ | 3 | 2008-05-20 13:00 |
| Carbide.ui v3.2 - problem with N95 theme (call handling screens) | liran2232 | Themes/Carbide.ui | 7 | 2008-05-13 19:45 |
| e32std errors huge problem | harish13_ks | General Symbian C++ | 1 | 2007-01-05 14:10 |