Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird IPython ipdb behaviour

I have some really weird behaviour that I just don't understand and therefore cannot explain, so I hope someone here can help me out. First thing I noticed was ipdb not letting me define variables any more:

ipdb> what=5
ipdb> what
*** NameError: name 'what' is not defined
whatelse=6

And a bit later I found ipdb returning this on my input (after running the code again):

dir()
ipdb> ['args', 'content_type', 'function', 'ipdb', 'item_code', 'kwargs', 'object_id', 'request', 'ud_dict', 'update_querydict', 'what', 'whatelse']
what=5
ipdb> what
5
ipdb> whatelse=7
ipdb> whatelse
ipdb> 6
whatelse
ipdb> 7
whatelse
ipdb> 6
whatelse
ipdb> 7

To me this looks as I have two interleaving debug sessions, which I have access to in some strange alternating pattern. How can I get rid of that?

edit: Killing all python processes and re-running the code did help. Everything is back to normal now. But as I just don't understand what was going on, I would be very interested in an answer to what happened, and how to reproduce the behaviour.

like image 311
marue Avatar asked May 23 '12 07:05

marue


1 Answers

If you have two processes each consuming stdin, they can cause symptoms very similar to what you've described.

>>> import subprocess
>>> subprocess.Popen('python', shell=True)
<subprocess.Popen object at 0x0000000001DEFEB8>
>>> Python 2.6.5 (r265:79096, Mar 19 2010, 18:02:59) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> a = 5
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> a
5
>>> a = 6
>>> a
5
>>> a
6
>>> a
5
>>> a
6
>>> a
5
>>> a
6

The first time I called a, it was undefined, even though I'd defined it in the line above. I call it again and it's 5. Then I set it to 6, and each subsequent call alternates between the two. This seems to be exactly what you're experiencing.

stdin is line-buffering, causing alternating lines of text to pass to alternating processes, in this case two instances of Python.

like image 154
mhlester Avatar answered Sep 19 '22 05:09

mhlester