(Python デバッグ (pdb))

Description

Using Python command-line debugger (pdb) to debug Plone and Python applications.

はじめに

Python debugger (pdb) is interactive command-line debugger.

It is very limited in functionality, but it will work in every environment and console. Plone also has through-the-web-browser PBD debugging add-on products.

ノート

PDB is different from Python interactive shell. PDB let’s you to step through the code, whilst Python shell allows you just to inspect and manipulate objects.

If you wish to play around with Zope in interactive Python shell or run scripts instead of debugging (exceptions), please read Command line documentation.

See also

pdb を使う

警告

In following text word set_trace has been replaced with xxx_trace due to commit problems.

# Go to your code and insert statement import pdb ; pdb.xxx_trace. When the code is run next time
the execution will stop there and you can examine the current context variables from Python command prompt
# After you have put import pdb ; pdb.xxx_trace() to your code, stop Zope and start it
on foreground using bin/instance fg command.

# TextMate support for pdb can be found at http://pypi.python.org/pypi/PdbTextMateSupport/0.3.

# mr.freeze allows traces to be added without restarting: http://pypi.python.org/pypi/mr.freeze.

Example:

class AREditForm(crud.EditForm):
    """ Present edit     le containing rows per each item added and delete controls """
    editsubform_factory = AREditSubForm

    template = viewpagetemplatefile.ViewPageTemplateFile('ar-crud-    le.pt')

    @property
    def fields(self):

        #
        # Execution will stop here and interactive Python prompt is opened
        #

        import pdb ; xxx.set_trace()
        constructor = ARFormConstructor(self.context, self.context.context, self.request)
        return constructor.getFields()

Pretty printing objects

Example:

>>> from pprint import PrettyPrinter
>>> PrettyPrinter().pprint(folder.__dict__)
{
  '_Access_contents_information_Permission': ['Anonymous',
                                              'Manager',
                                              'Reviewer'],
  '_List_folder_contents_Permission': ('Manager', 'Owner', 'Member'),
  '_Modify_portal_content_Permission': ('Manager', 'Owner'),
  '_View_Permission': ['Anonymous', 'Manager', 'Reviewer'],
  '__ac_local_roles__': {'gregweb': ['Owner']},
  '_objects': ({'meta_type': 'Document', 'id': 'doc1'},
               {'meta_type': 'Document', 'id': 'doc2'}),
  'contributors': (),
  'creation_date': DateTime('2005/02/14 20:03:37.171 GMT+1'),
  'description': 'Dies ist der Mitglieder-Ordner.',
  'doc1': <Document at doc1>,
  'doc2': <Document at doc2>,
  'effective_date': None,
  'expiration_date': None,
  'format': 'text/html',
  'id': 'folder',
  'language': '',
  'modification_date': DateTime('2005/02/14 20:03:37.203 GMT+1'),
  'portal_type': 'Folder',
  'rights': '',
  'subject': (),
  'title': "Documents",
  'workflow_history': {'folder_workflow': ({'action': None,
    'review_state': 'visible', 'comments': '', 'actor': 'gregweb',
    'time': DateTime('2005/02/14 20:03:37.187 GMT+1')},)}
}

Fore more information:

Useful pdb commands

Just type the letter and hit enter

  • s = step into, go into the function in the cursor
  • n = step over, execute the function under the cursor without stepping into it
  • c = continue, resume program
  • bt = print stack trace
  • up = go to the scope of the caller function

Useful pdb snippets

Output object’s class:

print obj.__class__

Output object attributes and methods:

for i in dir(obj): print i

Print local variables in the current function:

print locals()

Dumping incoming HTTP GET or HTTP POST:

print "Got request:"
for i in self.request.form.items():
    print i

Automatically start debugger when exceptions in raised (browser)

You can start interactive through-the-browser Python debugger when your site throws an exception.

Automatically start debugger when exception is raised (command line)

ノート

This cannot be directly applied to a web server, but works with command line scripts

ノート

This does not work with Zope web server launch as it forks a process.

Example:

python -m pdb myscript.py

Hit c and enter to start the application. It keeps running, until uncaught exception is risen when it falls back to pdb debug prompt.

For more information see