Warning

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

Debug anywhere

by Martin Aspeli last modified Sep 12, 2012 10:51 AM
A simple way to get a PDB session on any location

First of all: Don't ever do this on a production site, or you risk stalling your Zope processes while they lie in debug mode. With that out of the way...

It's often useful to have a PDB debugger open to poke around your site. Let's say you are trying to debug a particular method on a content object and you want to run it in a few different ways, as you would with PDB. With an External Method, you can get a context-bound PDB session in no time.

  • Add a file Extensions/test-ex.py to your Zope instance.

It should contain something like:

  def test(self):
      import pdb; pdb.set_trace()
  • In your portal_skins/custom folder, add a new External Method, and point it at modle test-ex, method test, and give it an id test-ex.
  • Run Zope in debug mode (set debug-mode on in zope.conf) and in the foreground (start it with zopectl fg or runzope).

You can now traverse to some URL, e.g. http://localhost:8080/Plone/folder/obj/test-ex. In the terminal where Zope is running, you will get a PDB prompt.

To continue execution, remember to type c and then Enter!

Having an external method like this is useful in other circumstances too. You can change it while Zope is running and changes are reflected immediately so long as you're in debug mode. For example, if you wanted to test out a catalog query, you could replace the contents of text-ex.py with:

  from StringIO import StringIO
  from Products.CMFCore.utils import getToolByName

  def test(self):
      out = StringIO()
      print >>out, "Begin"

      catalog = getToolByName(self, 'portal_catalog')
      for brain in catalog(portal_type='Foo'):
          print >> out, brain.getURL()

      print >> out, "End"
      return out.getvalue()

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.