Debugging tutorial
From OpenSource
Contents |
[edit] Testing and debugging Python applications
[edit] Testing plan
- Standalone platform neutral application logic modules can be tested on PC using [PyUnit] unit testing framework and normal Python interpreter
- Non-hardware related code (UI) can be tested in Series 60 emulator.
- Target testing. Get your code to the target device and run it there.
[edit] Unit testing
[edit] Emulation
The emulator might have not all device features available. Especially sockets (network connections) are missing in certain emulator versions and PyS60 1.4.X.
[edit] Target device testing
PUTools is a Python shell over Bluetooth with ability sync files between PC (development environment) and target device (phone). You can deploy and test new Python code in the target device with few clicks.
PYTOOLS IS 404'ed on this site.
[edit] Install PUTools on PC
Copy putools\phpush.py and putools\lib to your E:\Python folder on a memory card. You might need to edit phpush.py to boostrap PYTHONPATH resolving. Edit phpush.py and add the following:
import os
import sys
sys.path.append(os.path.join("e:", "Python", "lib")) # Now files from E:\Python\lib are importable in PUTools console
PYTOOLS DOES NOT CONTAIN THIS FILE.
[edit] Start phpush.py on phone
Enable incoming Bluetooth connections on PC. Start Python shell (PythonScriptShell_x_x_x.SIS) must be installed. Scan for your computer. Connect to your computer.
Windows XP makes a cling sound and displays a notification bubble when you have succesfully created serial connection over Bluetooth to your PC even if PUTools is not running on PC.
[edit] Start putools shell on PC
Run pcfiles\push on PC
cd putools\pcfiles set PYTHONPATH=..\lib c:\python25\python push
If there is an active Bluetooth serial connection to phpush.py, a console window is opened.
[edit] Debugging
To start your application type:
>>> import yourapplicationmainmodule
or
>>> import yourappliaction module >>> yourapplicationmodule.mytestfunction()
Then, when your application crashes, you'll see stack trace in the console window:
Traceback (most recent call last): File "<input>", line 1, in ? File "<input>", line 2, in x RuntimeError: whoopsie
[edit] Synchronizing files
After you have located a bug in your code, fixed it in files on your PC, it's time to retransfer modified files to the phone.
Edit putools\pcfiles\sync.config and add absolute PC file system paths which are copied to the phone. Then, in PUTools bluetooh shell, run command:
>>> sync
You should see output like:
sent file: ..\..\tests\test_print.py sent file: ..\..\tests\test_socket.py sent file: ..\..\tests\thread_import.py sync done.
If no files are copied, you have messed up paths in sync.config file.
[edit] Other aspects on target device
print, stdout, stderr & co. are not available in e32 threads. This is due to the limitation of sharing file descriptors between threads. You cannot print outside main thread. The simplest way to get logging information from other threads is to open a file in a thread and print there.
Most of native objects (sockets, databases) cannot be shared between e32 threads.
If exception falls through from e32 thread the whole application crashes.
You need to sign Python shell SIS file yourself to get access to some capabilities (GPS).
Memory card file system is prone for corruptions. If you start to get exceptions like
Traceback (most recent call last):
File "<input>", line 1, in ?
File "c:\resource\site.py", line 97, in platsec_import
return _original_import(name, globals, locals, fromlist)
SystemError: error return without exception set
it's time to reformat the memory card and recopy all files again. Test the application only with a memory card dedicated for testing or you might lose your personal files!.
