home contents changes options help subscribe edit (external edit)

This is an example of stepping through a Python Scripts. Zope pythonscripts are RestrictedCode, defined in the ZODB (Zope Object Database) rather than in modules on disk. Normally pdb reads the .py module file to show the actual lines of code go by. Since there is no such module file for a Python script, it cannot show the actual lines (although it can show the line numbers). You can step through it and you will at least see source for things it calls. :

 > /opt/Zope-2.7/lib/python/Shared/DC/Scripts/Bindings.py(232)_getTraverseSubpath()->[]
 -> return self.REQUEST.other.get('traverse_subpath', [])
 pdb> s
  > <string>(2)?()
 pdb> s
 > <string>(4)?()
 pdb> l
 [EOF]
 pdb> s
 > <string>(6)?()
 pdb> s
 > <string>(8)?()
 pdb> s
 --Return--
 > <string>(8)?()->None
 pdb> s
 > /opt/Zope-2.7/lib/python/Shared/DC/Scripts/Bindings.py(282)_bindAndExec()->None
 ...

But! See Ken's note on DebuggingZopeWithEmacs?. There is a way to do this, apparently. I copy a python script to an emacs python-mode buffer of the same name and do this:

 >>> Zope.debug('/devskin',d=1)
 * Type "s<cr>c<cr>" to jump to beginning of real publishing process.
 * Then type c<cr> to jump to the beginning of the URL traversal
   algorithm.
 * Then type c<cr> to jump to published object call.
 > <string>(1)?()
 pdb> b PythonScripts/[Python Scripts].py:318
 Breakpoint 43 at /usr/local/src/Zope-2.7.0/lib/python/Products/PythonScripts/[Python Scripts].py:318
 pdb> c
 > /usr/local/src/Zope-2.7.0/lib/python/Products/PythonScripts/[Python Scripts].py(318)_exec()
 -> result = f(*args, **kw)
 pdb> s
 --Call--
             <-- (hit enter a couple of times)                                                                                                                              

 > /home/simon/Script (Python)(1)devskin()
 pdb>

At this point the source buffer does appear, with a pdb pointer on one of the lines. We seem to be not really there though. Stepping either does nothing or brings up other code windows.


comments:

import pdb in Python Scripts? --LennartRegebro, Wed, 21 Apr 2004 04:15:07 -0700 reply
I remember seeing a text about doing "import pdb;pdb.set_trace()" in a python script. This can be done if Python Scripts are set up to allow import pdb. I haven't needed to test this yet, though, so I don't know if it's useful, but it's worth mentioning.

import pdb in Python Scripts? --simon, Fri, 23 Apr 2004 15:20:43 -0700 reply
The script at the end of ConversingWithZope? perhaps.

... -- Fri, 30 Apr 2004 13:57:26 -0700 reply
OK, tested it, it doesn't help, the debug output is still the useless output of above. --Lennart

page needs cleanup --simon, Tue, 13 Sep 2005 14:33:13 -0700 reply
Adding pdb to the allowed modules (Python Scripts), import pdb; pdb.set_trace() to your script, and starting with zopectl fg (or zopectl fg -Xdebug-mode=on with Zope <2.8.1) works. If you run in eg an emacs shell you'll get source for things the script calls.

try ZDB --simon, Tue, 14 Mar 2006 15:31:50 -0800 reply
Paul Winkler writes:

 I have also sometimes used Chris Withers' "zdb" product, 
http://www.simplistix.co.uk/software/zope/zdb,
which has some overlap in that it allows you to do this in restricted
code, e.g. cmf scripts:

 from Products.zdb import set_trace
set_trace()

 ... and, very usefully, once in the debugger you can use the "l" command
to see the script source and it gets line numbers right.  So Ross, maybe
you can borrow some of Chris' code  :-) 

 Unfortunately zdb doesn't appear to work with zope 2.9 / python 2.4.
I'll file a bug report with Chris.
It works great with zope 2.8 / python 2.3.



subject:
  ( 23 subscribers )