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

Ondrej Certik ondrej@certik...
Tue Jun 23 00:18:56 CDT 2009


On Mon, Jun 22, 2009 at 11:08 PM, Gökhan SEVER<gokhansever@gmail.com> wrote:
> On Mon, Jun 22, 2009 at 11:21 PM, Ondrej Certik <ondrej@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.
>>
>> Thanks,
>> Ondrej
>>
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev@scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>
> Ondrej,
>
> I am testing the patch. See my comments below:
>
> [gsever@ccn ~]$ python
> Python 2.5.2 (r252:60911, Sep 30 2008, 15:41:38)
> [GCC 4.3.2 20080917 (Red Hat 4.3.2-4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> b=25
>>>> import numpy as np
>>>> arr = np.arange(10)
>>>> import IPython
>>>> IPython.set_trace()
>  Logging to /home/gsever/.ipython/2009-06-23.py
>
> Python 2.5.2 (r252:60911, Sep 30 2008, 15:41:38)
> Type "copyright", "credits" or "license" for more information.
>
> IPython 0.10.bzr.r1174 -- 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]: whos   ###<--- Nothing is listed :(
> Interactive namespace is empty.
>
> In [2]: b
> Out[2]: 25
>
> In [4]: arr
> Out[4]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>
> In [5]: exit()    ###<--- it exits but act as if it in the IPython session
> :(
> Do you really want to exit ([y]/n)? y
>>>> data[0]
>
> ### and can't completely exit, stays in a never-ending loop :(

I don't exactly understand what is wrong with your session? It seems
to me it does exactly what I want --- it starts an ipython session
inplace and allows you to easily manipulate your namespace. As to
exiting, this works for me flawlessly:

$ python
>>> a = 43
>>> import IPython
>>> IPython.set_trace()
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]: 43

In [2]:
>>>


Which python version and which system are you using? I am on ubuntu
jaunty, the latest ipython and the default python2.6.

>
> Other than these complaints, a very nice addition indeed :)

Thanks. :)

>
> Could you please tell whether IPython instantiation could be expanded to a
> pylab enabled session? (A previously asked question, but just wondering...)

Right --- I don't know. As I said, try to see how the --pylab command
is passed to ipython and then trigger it in the code.

Ondrej


More information about the IPython-dev mailing list