Search code examples
pythonipythondemo

IPython demo mode


I'm trying to use the IPython demo mode. I created a file called test.py containing:

print 1
print 2
print 3

and then launched IPython and did the following:

In [1]: from IPython.demo import LineDemo

In [2]: d = LineDemo('test.py')

In [3]: d()
********************* <test.py> block # 0 (5 remaining) *********************
p

********************************** output: **********************************
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

/Users/tom/Library/Python/2.6/site-packages/ipython-0.10-py2.6.egg/IPython/demo.pyc in runlines(self, source)
    400         """Execute a string with one or more lines of code"""
    401 
--> 402         exec source in self.user_ns
    403 
    404     def __call__(self,index=None):

/Users/tom/tmp/<string> in <module>()
----> 1 
      2 
      3 
      4 
      5 

NameError: name 'p' is not defined

What is likely to be causing this error? Am I using LineDemo incorrectly?


Solution

  • There seems to be a bug in IPython. In demo.py in LineDemo.reload, the line that says:

    src_b           = [l for l in self.fobj.readline() if l.strip()]
    

    should say:

    src_b           = [l for l in self.fobj.readlines() if l.strip()]
    

    Currently it's trying to execute all the letters in the first line instead of all the lines in the file.

    Edit: Bug reported.