1

I get wrong line in the error message when I use Elpy in emacs (interpreter: ipython). This happens when I select part of my code and use ctrl-c-c to run it in elpy. The error message shows the wrong content. For example:

import numpy as np

a = np.array([1,2,3])

for i in range(20):
    a = b

I deliberately write a wrong code. If I run it as a whole, the error message is correct:

In [4]: ---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-31bb503ed55c> in <module>()
----> 1 import codecs, os;__pyfile = codecs.open('''/tmp/py14415AqV''', encoding='''utf-8''');__code = __pyfile.read().encode('''utf-8''');__pyfile.close();os.remove('''/tmp/py14415AqV''');exec(compile(__code, '''/home/yanza15/test.py''', 'exec'));

/home/yanza15/test.py in <module>()
      4 
      5 for i in range(20):
----> 6     a = b

NameError: name 'b' is not defined

But if I only select a part and use ctrl-c-c to run it:

for i in range(20):
    a = b

The error message will be

In [5]: ---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-7e398e1dee8c> in <module>()
----> 1 import codecs, os;__pyfile = codecs.open('''/tmp/py14415N0b''', encoding='''utf-8''');__code = __pyfile.read().encode('''utf-8''');__pyfile.close();os.remove('''/tmp/py14415N0b''');exec(compile(__code, '''/home/yanza15/test.py''', 'exec'));

/home/yanza15/test.py in <module>()
      1 import numpy as np
----> 2 
      3 a = np.array([1,2,3])
      4 
      5 for i in range(20):

NameError: name 'b' is not defined

The line number in the error message is 2, which means the second line I selected is wrong. But the content it shows is the line 2 in the whole code. That's not I want. How can I customize elpy so that the error message shows the correct position of the code where I'm wrong?

Thanks in advance!

  • I know nothing of `elpy`. However, this seems intrinsic to iPython; I don't know the correct terms to describe what happens, but if you don't restart the kernel, you get the correct error message; (in the copy/paste example, `b` _is_ on line 2) but the traceback shows some cached code from before when it reports the error. So you get a mismatch. This happens particularly if you change imported modules. – roganjosh Mar 22 '17 at 21:38
  • I'm on my phone and that became jumbled through edits, sorry. Basically, the line the error is reported on is correct, but the code it shows in the traceback might be historical code, so it doesn't make sense as a whole. I would like to know why exactly it behaves like this. – roganjosh Mar 22 '17 at 21:46

0 Answers0