README.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. IPython `pdb`
  2. =============
  3. .. image:: https://travis-ci.org/gotcha/ipdb.png?branch=master
  4. :target: https://travis-ci.org/gotcha/ipdb
  5. .. image:: https://codecov.io/gh/gotcha/ipdb/branch/master/graphs/badge.svg?style=flat
  6. :target: https://codecov.io/gh/gotcha/ipdb?branch=master
  7. Use
  8. ---
  9. ipdb exports functions to access the IPython_ debugger, which features
  10. tab completion, syntax highlighting, better tracebacks, better introspection
  11. with the same interface as the `pdb` module.
  12. Example usage:
  13. .. code-block:: python
  14. import ipdb
  15. ipdb.set_trace()
  16. ipdb.set_trace(context=5) # will show five lines of code
  17. # instead of the default three lines
  18. # or you can set it via IPDB_CONTEXT_SIZE env variable
  19. # or setup.cfg file
  20. ipdb.pm()
  21. ipdb.run('x[0] = 3')
  22. result = ipdb.runcall(function, arg0, arg1, kwarg='foo')
  23. result = ipdb.runeval('f(1,2) - 3')
  24. Arguments for `set_trace`
  25. +++++++++++++++++++++++++
  26. The `set_trace` function accepts `context` which will show as many lines of code as defined,
  27. and `cond`, which accepts boolean values (such as `abc == 17`) and will start ipdb's
  28. interface whenever `cond` equals to `True`.
  29. Using configuration file
  30. ++++++++++++++++++++++++
  31. It's possible to set up context using a `.ipdb` file on your home folder, `setup.cfg`
  32. or `pyproject.toml` on your project folder. You can also set your file location via
  33. env var `$IPDB_CONFIG`. Your environment variable has priority over the home
  34. configuration file, which in turn has priority over the setup config file.
  35. Currently, only context setting is available.
  36. A valid setup.cfg is as follows
  37. ::
  38. [ipdb]
  39. context=5
  40. A valid .ipdb is as follows
  41. ::
  42. context=5
  43. A valid pyproject.toml is as follows
  44. ::
  45. [tool.ipdb]
  46. context=5
  47. The post-mortem function, ``ipdb.pm()``, is equivalent to the magic function
  48. ``%debug``.
  49. .. _IPython: http://ipython.org
  50. If you install ``ipdb`` with a tool which supports ``setuptools`` entry points,
  51. an ``ipdb`` script is made for you. You can use it to debug your python 2 scripts like
  52. ::
  53. $ bin/ipdb mymodule.py
  54. And for python 3
  55. ::
  56. $ bin/ipdb3 mymodule.py
  57. Alternatively with Python 2.7 only, you can also use
  58. ::
  59. $ python -m ipdb mymodule.py
  60. You can also enclose code with the ``with`` statement to launch ipdb if an exception is raised:
  61. .. code-block:: python
  62. from ipdb import launch_ipdb_on_exception
  63. with launch_ipdb_on_exception():
  64. [...]
  65. .. warning::
  66. Context managers were introduced in Python 2.5.
  67. Adding a context manager implies dropping Python 2.4 support.
  68. Use ``ipdb==0.6`` with 2.4.
  69. Or you can use ``iex`` as a function decorator to launch ipdb if an exception is raised:
  70. .. code-block:: python
  71. from ipdb import iex
  72. @iex
  73. def main():
  74. [...]
  75. .. warning::
  76. Using ``from future import print_function`` for Python 3 compat implies dropping Python 2.5 support.
  77. Use ``ipdb<=0.8`` with 2.5.
  78. Issues with ``stdout``
  79. ----------------------
  80. Some tools, like ``nose`` fiddle with ``stdout``.
  81. Until ``ipdb==0.9.4``, we tried to guess when we should also
  82. fiddle with ``stdout`` to support those tools.
  83. However, all strategies tried until 0.9.4 have proven brittle.
  84. If you use ``nose`` or another tool that fiddles with ``stdout``, you should
  85. explicitly ask for ``stdout`` fiddling by using ``ipdb`` like this
  86. .. code-block:: python
  87. import ipdb
  88. ipdb.sset_trace()
  89. ipdb.spm()
  90. from ipdb import slaunch_ipdb_on_exception
  91. with slaunch_ipdb_on_exception():
  92. [...]
  93. Development
  94. -----------
  95. ``ipdb`` source code and tracker are at https://github.com/gotcha/ipdb.
  96. Pull requests should take care of updating the changelog ``HISTORY.txt``.
  97. Under the unreleased section, add your changes and your username.
  98. Manual testing
  99. ++++++++++++++
  100. To test your changes, make use of ``manual_test.py``. Create a virtual environment,
  101. install IPython and run ``python manual_test.py`` and check if your changes are in effect.
  102. If possible, create automated tests for better behaviour control.
  103. Automated testing
  104. +++++++++++++++++
  105. To run automated tests locally, create a virtual environment, install `coverage`
  106. and run `coverage run setup.py test`.
  107. Third-party support
  108. -------------------
  109. Products.PDBDebugMode
  110. +++++++++++++++++++++
  111. Zope2 Products.PDBDebugMode_ uses ``ipdb``, if available, in place of ``pdb``.
  112. .. _Products.PDBDebugMode: http://pypi.python.org/pypi/Products.PDBDebugMode
  113. iw.debug
  114. ++++++++
  115. iw.debug_ allows you to trigger an ``ipdb`` debugger on any published object
  116. of a Zope2 application.
  117. .. _iw.debug: http://pypi.python.org/pypi/iw.debug
  118. ipdbplugin
  119. ++++++++++
  120. ipdbplugin_ is a nose_ test runner plugin that also uses the IPython debugger
  121. instead of ``pdb``. (It does not depend on ``ipdb`` anymore).
  122. .. _ipdbplugin: http://pypi.python.org/pypi/ipdbplugin
  123. .. _nose: http://readthedocs.org/docs/nose