Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 04:23, 2 May 2008.

Python debugging techniques

From Forum Nokia Wiki

This article aims to give developers ideas about how to better find the sources of errors in their PyS60 applications, as default error reports can be hard to understand and standalone applications don't even have error reports.

Method 1

A simple way of finding out what is wrong in a sequence of code is to show a confirmation message after an operation is successfully performed. The message can either be printed on the screen or spoken by the phone.

For example, let's say we want to alphabetize a list and append an element to it:

import appuifw, e32, audio
 
l=['alpha', 'beta', 'lambda', 'gamma']
n=len(l)
i=0
while(i<n-1):
    j=i+1
    while(j<n):
        if(l[i]>l[j]):
            a=l[i]
            l[i]=l[j]
            l[j]=a
        j+=1
    i+=1
 
#If all goes well to this point, we can print a message...
print "The list has been alphabetized"
 
#... or have the phone tell us
audio.say("The list has been alphabetized")
 
l.append('omega')
 
print "The element 'omega' has been added to the list"
 
applock=e32.Ao_lock()   
applock.wait()         #Tell the application not to terminate immediately

Method 2

Known as the "exception harness", this method is more useful for standalone applications. It mimics a stack trace of the Python Script Shell.

try:
    # Actual program is here.
    1 / 0
except:
    import sys
    import traceback
    import e32
    import appuifw
    appuifw.app.screen="normal"               # Restore screen to normal size.
    appuifw.app.focus=None                    # Disable focus callback.
    body=appuifw.Text()
    appuifw.app.body=body                     # Create and use a text control.
    applock=e32.Ao_lock()
    def quit():applock.signal()
    appuifw.app.exit_key_handler=quit         # Override softkey handler.
    appuifw.app.menu=[(u"Exit", quit)]        # Override application menu.
    body.set(unicode("\n".join(traceback.format_exception(*sys.exc_info()))))
    applock.wait()                            # Wait for exit key to be pressed.
    appuifw.app.set_exit()

Method 3

Here we record the error to a text log file.

def main():
    try:
        Your main code goes here.
    except:
        import sys
        import traceback
        import appuifw
        cla, exc, trbk = sys.exc_info()
        excName = cla.__name__
        try:
            excArgs = exc.__dict__["args"]
        except KeyError:
            excArgs = "<no args>"
        excTb = traceback.format_tb(trbk, 5)
        errorString = repr(excName) + '-' + repr(excArgs) + '-' + repr(excTb) + '\n'
        print errorString
        appuifw.note(u'Application errors, see log file for more information', "error")
        file = open(u'C:\\Log.txt','a')
        file.write(errorString)
        file.close()
        raise
 
if __name__ == "__main__":
    main()

Here the errors will be recorded in the Log file at C:\\Log.text

Related Discussions
Thread Thread Starter Forum Replies Last Post
Testing a TDesC8 descriptor rambler82 General Symbian C++ 10 2006-12-12 12:19
Debugging on device juanmahv Mobile Java Tools & SDKs 3 2008-01-10 17:45
Carbide.j on-device debugging SIGNED? pillar Mobile Java Tools & SDKs 4 2007-06-20 18:56
Why CodeWarrior kcome Symbian Tools & SDKs 3 2003-12-09 11:46
On Device Debugging Configuration the_mincer Mobile Java Tools & SDKs 0 2006-11-15 18:50
 
Powered by MediaWiki