官术网_书友最值得收藏!

Taking advantage of an enhanced interactive Python debugger with ipdb

As a Python developer, you should already be familiar with pdb. So think of ipdb as a Python debugger with many of the advantages IPython provides. ipdb, for example, is much easier to use thanks to the IPython autocomplete feature.

Note

You can find the official documentation for pdb for Python 2.4 at http://www.python.org/doc/2.4.4/lib/module-pdb.html.

ipdb has another very useful and important feature which is the ability to get an IPython shell in our instance console whenever we want by means of adding the /ipdb suffix to any URL.

Note

ipdb is only available when Zope is running in the foreground mode.

How to do it…

  1. Add the iw.debug line in eggs parameter of [buildout] part:
    [buildout]
    
    ...
    
    # Add additional eggs here 
    eggs = 
     iw.debug 
    
  2. Insert an iw.debug line in the zcml parameter of [instance] part:
    [instance]
    
    ...
    
    # If you want to register ZCML slugs for any packages, 
    # list them here. 
    # e.g. zcml = my.package my.other.package 
    zcml = 
     iw.debug 
    
  3. Rebuild your instance. We need to build our Zope instance again to let it know that there were some changes:
    ./bin/buildout
    
  4. Run your instance. Then run the instance in foreground mode to get a handle of its console:
    ./bin/instance fg 
    

How it works…

Given that we haven't written any line of code yet, we won't call ipdb from inside the (non-existing) code. What we need to know is that by adding these lines anywhere in the code, we will get the prompt from the improved debugger:

import ipdb; ipdb.set_trace()

Note

If you leave any ipdb (or pdb) call in your code when running Zope in any but foreground mode, then you'll get a BdbQuit exception.

But we do have a Plone site running, so let's try to get an IPython shell on demand. Access http://localhost:8080/plone/ipdb and go to your foreground console.

There will be an ipdb prompt waiting for you. Use the ll command (double "L" in lowercase) to get a list of local variables. Then test some other things: context, is the object we called ipdb from, in our example, the Plone site. In this particular case, it also happens to be the portal variable.

Note

Don't forget to use Tab key to let ipdb auto-complete.

How it works…

Note

More information about ipdb can be found at: http://pypi.python.org/pypi/iw.debug.

主站蜘蛛池模板: 哈巴河县| 兴化市| 类乌齐县| 沾益县| 南雄市| 鹤峰县| 星子县| 喀喇沁旗| 同仁县| 蒙山县| 玛纳斯县| 竹山县| 平陆县| 永新县| 兴仁县| 林口县| 建始县| 定边县| 丹阳市| 天水市| 东阳市| 汕尾市| 雷山县| 华池县| 子长县| 通城县| 建始县| 阳新县| 通城县| 河间市| 涟水县| 北安市| 鸡泽县| 丹凤县| 高安市| 蚌埠市| 利津县| 白沙| 平凉市| 黑河市| 龙山县|