Warning

This document hasn't been checked for compatibility with current versions of Plone. Use at your own risk.

Debugging .py and .cpy scripts

by Mikko Ohtamaa last modified Dec 30, 2008 03:02 PM
Knowing what goes wrong in .py and .cpy scripts

Introduction

Python scripts (.py) and (.cpy) are used in Plone for site layout and function glue logic. They are usually small code snippets which perform tasks that aren't assigned for any bigger functional module. Scripts are safe - they run in sandbox and don't have full code execution access. Also, there is some Plone specific logic in scripts. This is declared in ## comment headers.

 

Logical errors

If Python code raises exception or the state of logic flow inside the script doesn't feel right you can insert debug logging code inside the script. Note that Python print doesn't work.

Script logging is explained in this how to.

 

Syntax errors

If there is a syntax error with Python you get an error notice similar to the following one:

Site error

This site encountered an error trying to fulfill your request. The errors were:

Error Type
RuntimeError
Error Value
LSMSkin/skins/lsmskin/send_book_appointment.cpy has errors.
Request made at
2006/05/24 18:42:35.984 GMT+3

When you go to Zope management interface, then portal_skins and navigate to your Python script you should see errorneous line info have appeared in the script headers. Script compilar adds there lines when it tries to load script with bad Python syntax.

## Script (Python) "send_book_appointment"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind state=state
##bind subpath=traverse_subpath
##parameters=
##title=LSM website Book Appointment submission
##
## Errors:
##  invalid syntax (Script (Python), line 20)
##
REQUEST=context.REQUEST

Reported line numbers are misleading since they don't take account ## lines in the beginning of the script. Add number of ## lines from the reported number to get the correct error position.

Make fixes to your script. Then reload browser frame in ZMI to see if errors still exist.

Other errors

Python script compiler seems to picky with tab characters. If you get sporadic errors, try to replace tabs with spaces.

Binding errors in script header should raise an exception and tell which binding failed.

If ##parameters= line is incorrect script fails without error output. For example, if there are commas missing from the parameters line.


Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.