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

Ondrej Certik ondrej@certik...
Mon Jun 22 23:21:40 CDT 2009


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.

Thanks,
Ondrej
-------------- next part --------------
A non-text attachment was scrubbed...
Name: set_trace.patch
Type: text/x-diff
Size: 1025 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/ipython-dev/attachments/20090622/acf9a7ac/attachment.bin 


More information about the IPython-dev mailing list