Pdb and "Script (Python)" objects

Hello community,
for Plone 4 and for many addons it is common practice to use "Script (Python)" objects, living in the ZODB, to do little tasks which can be also be nicely edited TTW (at least, if you are accustomed with the ZMI).

World is perfectly fine to this point. The problem I have in particular, when it comes to the topic of debugging. As pdb is a common choice for Plone developers to debug, I use this tool as well for debugging (or rather say TRY debugging) the aforementioned Python Scripts. Unfortunately many of the commands of pdb are not working in the context of those objects as they are used to be (with for example file system Python files). For example the (l)isting command, which should return the current location, returns absolutely nothing. This also renders any logical successive commands like (n)ext or (s)tep useless - or at atleast with massive effort by manually tracking the source.

So my question is, if we can fix this issue to make pdb able to produce meaningful output? My guess is, that there are some properties fullfilled for file system Python scripts, that are not held by dynamic script objects, but are assumed by pdb.

best regards

Hi Paul,

Unfortunately, no dice. You'll be better off moving to browserviews if it is your own code, where this does work.

I normally just copy/paste the lines I'm interested in into my terminal (pbd session), or print whatever I need to know in the script and return printed (https://docs.zope.org/zope2/zope2book/BasicScripting.html#print-statement-support).

-Roel

@pgg if you're going to do @jaroel's approach, don't forget to check alternatives to pdb in Pdb;pdb.set_trace() alternative - there are some real gems there.

@pgg check https://pypi.python.org/pypi/Products.enablesettrace.

2 Likes

I just tried this product. Whereas this is helpful for scripts completely living in the ZODB. It does not help me however, when trying to list the sources.

What exactly are you trying to achieve, @pgg? From your first post I couldn't understand exactly what's your use case.

Why not just run "/bin/instance debug"? Then copy paste your script in the console to run it up to the point you want to investigate?

Do you mean use scripts to create content? If so, try ftw.inflator or use transmogrifier modules.

What exactly are you trying to achieve, @pgg? From your first post I couldn’t understand exactly what’s your use case.

I'm trying to use pdb with all its features in "Script (Python)" files. Currently listing the source is broken. See this example:

## Script (Python) "selectDefaultPage"
##title=Helper method to select a default page for a folder view
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind state=state
##bind subpath=traverse_subpath
##parameters=objectId=None

from Products.CMFPlone import PloneMessageFactory as _

if not objectId:
    context.plone_utils.addPortalMessage(_(u'Please select an item to use.'),
                                         'error')
    return state.set(status='missing')

import pdb; pdb.set_trace();  # <--- NOTE THIS LINE

if not objectId in context.objectIds():
    message = _(u'There is no object with short name ${name} in this folder.',
                mapping={u'name': objectId})

    context.plone_utils.addPortalMessage(message, 'error')
    return state.set(status='failure')

try:
    context.setDefaultPage(objectId)
    context.plone_utils.addPortalMessage(_(u'View changed.'))
except:
    pass
return state

When I find myself in the line with pdb.set_trace() the command l(ist) returns nothing. The console output is as follows:

/Plone/demo/pages/saveDefaultPage(10)saveDefaultPage()
pdb> l

pdb> 

I expect there, that the current line of code is printed. Otherwise I cannot say, where I am and what the next line will be.

Why not just run “/bin/instance debug”? Then copy paste your script in the console to run it up to the point you want to investigate?

Because it's cumbersome and different to the way of debugging I am used to.

Do you mean use scripts to create content?

No. The creation of "Python (Scripts)" files was just an example. These files could also live on the hard disk in some skin folder. The problem is there as well.

Forget persistent Python scripts inside the ZODB...they are a ghost of the last millennium.

Either use browser views for writing proper Python code that you can easily debug or use scripts that run against a ZEO server using bin/instance run script.py...everything else is not really state-of-the-art or recommendable.