[IPython-dev] [patch] IPython.set_trace() implemented

Ondrej Certik ondrej at certik.cz
Mon Jun 29 22:41:13 EDT 2009


On Mon, Jun 22, 2009 at 10:21 PM, Ondrej Certik<ondrej at certik.cz> wrote:
> Hi,
>
> with the attached patch you can now do:
>
> ----
> a = {3: 4}
>
> def something():
>    b = 45
>    import IPython
>    IPython.set_trace()
>
> something()
> ----
>
> and after you run it:
>
> -----------
> Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
> Type "copyright", "credits" or "license" for more information.
>
> IPython 0.10.bzr.r1163 -- An enhanced Interactive Python.
> ?         -> Introduction and overview of IPython's features.
> %quickref -> Quick reference.
> help      -> Python's own help system.
> object?   -> Details about 'object'. ?object also works, ?? prints more.
>
> In [1]: a
> Out[1]: {3: 4}
>
> In [2]: b
> Out[2]: 45
> -------
>
>
> e.g. you can access locals() and globals() immediately. I use this
> very often lately, as it is very handy to debug some broken place in
> the code. Another usage is that I need to implement some difficult
> operation with numpy arrays, dicts, etc. usually some oneliner, but I
> am not sure about the API, so I just call IPython.set_trace(), play
> with the things to get what I want, then just copy & paste the
> oneliner to the code.
>
> More info in the docstring:
>
> In [2]: IPython.set_trace?
> Type:           function
> Base Class:     <type 'function'>
> String Form:    <function set_trace at 0x7f31ca8b3140>
> Namespace:      Interactive
> File:           /home/ondrej/usr/lib/python/IPython/__init__.py
> Definition:     IPython.set_trace()
> Docstring:
>    Starts the IPython shell inplace.
>
>    Example:
>
>    >> import IPython
>    >> IPython.set_trace()
>
>    The user namespace is set to the union of globals() and locals(), e.g.
>    calling set_trace() is exactly equivalent to:
>
>        import IPython
>        IPython.Shell.IPShell(user_ns=dict(globals(), **locals())).mainloop()
>
>    but the actual implementation of set_trace() needs to use the inspect
>    module, because it needs to access the parents frame.
>
>
>
> Let me know if the implementation is ok, or if some more work is needed.


Any comments to that from ipython developers?

Ondrej



More information about the IPython-dev mailing list