Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipdb should ask iPython 5 which debugger class to use #105

Comments

@JBKahn
Copy link
Contributor

JBKahn commented Jul 13, 2016

I've got this in two ways:

  1. Inside honcho, if I'm using an import ipdb; ipdb.sset_trace() in my Django app
  2. Same idea, but inside nose tests for the Django app

Although a breakpoint in the running app did happen, we had some weird behaviour with the autocompletion that used to be there in 4.2 but didn't seem to be present once we bumped ipython to 5, maybe that was known.

Here's the traceback for my test

    my_project.py line 77 in my_function
      import ipdb; ipdb.sset_trace()
    /data/virtualenv/my_project/lib/python2.7/site-packages/ipdb/stdout.py line 18 in sset_trace
      set_trace(frame, context)
    /data/virtualenv/my_project/lib/python2.7/site-packages/ipdb/__main__.py line 93 in set_trace
      p = _init_pdb(context).set_trace(frame)
    /data/virtualenv/my_project/lib/python2.7/site-packages/ipdb/__main__.py line 76 in _init_pdb
      p = Pdb(def_colors)
    /data/virtualenv/my_project/lib/python2.7/site-packages/IPython/terminal/debugger.py line 15 in __init__
      self.pt_init()
    /data/virtualenv/my_project/lib/python2.7/site-packages/IPython/terminal/debugger.py line 38 in pt_init
      self.pt_cli = CommandLineInterface(self._pt_app, eventloop=self.shell._eventloop)
   AttributeError: 'TerminalInteractiveShell' object has no attribute '_eventloop'
@JBKahn
Copy link
Contributor Author

JBKahn commented Jul 13, 2016

This was created to reference this issue I previously opened on ipython: ipython/ipython#9740

@JBKahn
Copy link
Contributor Author

JBKahn commented Jul 13, 2016

This patched it for me on version 5, would not be backwards compatible, but works seemigly for nose:

def _init_pdb(context=3):
    term = import_module(['IPython.terminal.interactiveshell'], "TerminalInteractiveShell")
    debugger_cls = term.TerminalInteractiveShell().debugger_cls
    if 'context' in getargspec(Pdb.__init__)[0]:
        p = debugger_cls(def_colors, context=context)
    else:
        p = debugger_cls(def_colors)
    p.rcLines += def_exec_lines
    return p

edit: I'm happy to contribute a PR but I'm not sure how you guys would want the conditional import_module and check.

@JBKahn JBKahn changed the title ipdb should be checking if ipyuthon v5 has decided it should be using a simple prompt before calling the ipython terminal debugger ipdb shoudl ask iPython 5 which debugger class to use Jul 13, 2016
@JBKahn JBKahn changed the title ipdb shoudl ask iPython 5 which debugger class to use ipdb should ask iPython 5 which debugger class to use Jul 13, 2016
@JBKahn
Copy link
Contributor Author

JBKahn commented Jul 24, 2016

@gotcha any opinions?

@llazzaro
Copy link

I had the same issue with ipython5 and ipdb

@GuyHoozdis
Copy link

I was seeing the same exception, but I don't think this is actually a bug. Try this:

py.test --capture=no path/to/test_module.py

nosetests has a similar option.

nosetests --nocapture

@JBKahn
Copy link
Contributor Author

JBKahn commented Aug 3, 2016

@GuyHoozdis that is a bug, the fix would detect the option and use the appropriate debugger class. i.e. the same one it uses now if you didn't use capture. Not all applications have an arg like that, to work around ipdb.

@GuyHoozdis
Copy link

GuyHoozdis commented Aug 4, 2016

I could be wrong, but my understanding of the issue is that packages like nose or pytest take over stdout in a way that causes problems for any debugger. That is why they offer an argument like --nocapture or --capture=no.

I suspect that when you added the line term = import_module(['IPython.terminal.interactiveshell'], "TerminalInteractiveShell") you have invoked some code that un-does or overwrites the stdout trickery that the application framework did and that is why your patch worked.

Not all applications have an arg like that

Are you referring to honcho? If honcho is fiddling with stdout and doesn't offer an argument to disable that behavior, then bug should be filed against their application - not ipdb.

https://github.com/gotcha/ipdb#issues-with-stdout

@JBKahn
Copy link
Contributor Author

JBKahn commented Aug 4, 2016

That is the issue but the breakage was that previously that call did this check, based on earlier versions of ipython and its API. It essentially did the same thing, but using a different path.

When they released version 5, they expect you to ask for the debugger class rather than use the specific class referenced by that line. It's not a new feature, the upgrade to version 5 accidentally missed that change. It simply lost some behaviour when it bumped ipython.

It's not that it overwrites or changes stdout. It's just that the code as of version 5 is calling the wrong debugger class because of an API change in version 5. There are simply limitations of what you can do when they override stdout and you cannot use the other debugger in those cases.

@JBKahn
Copy link
Contributor Author

JBKahn commented Aug 4, 2016

Also, I'm using sset_trace for that reason, in the code I posted above. It's a bug.

@Fak3
Copy link

Fak3 commented Aug 7, 2016

I got the same issue using kivy framework:

    import ipdb; ipdb.set_trace()
  File "/home/u1/.virtualenvs/monarchy/lib/python2.7/site-packages/ipdb/__main__.py", line 93, in set_trace
    p = _init_pdb(context).set_trace(frame)
  File "/home/u1/.virtualenvs/monarchy/lib/python2.7/site-packages/ipdb/__main__.py", line 76, in _init_pdb
    p = Pdb(def_colors)
  File "/home/u1/.virtualenvs/monarchy/lib/python2.7/site-packages/IPython/terminal/debugger.py", line 15, in __init__
    self.pt_init()
  File "/home/u1/.virtualenvs/monarchy/lib/python2.7/site-packages/IPython/terminal/debugger.py", line 38, in pt_init
    self.pt_cli = CommandLineInterface(self._pt_app, eventloop=self.shell._eventloop)
AttributeError: 'TerminalInteractiveShell' object has no attribute '_eventloop'

@Fak3
Copy link

Fak3 commented Aug 7, 2016

Downgrading to ipython 4 solved the issue for me.

@JBKahn
Copy link
Contributor Author

JBKahn commented Sep 8, 2016

fyi, this is the code I'm currently using in my app to monkey patch ipdb while #108 is unmerged/worked on.

# ==============================================================================
# Ipython 5 with ipdb monkey patch (https://github.com/gotcha/ipdb/pull/108/)
# ==============================================================================
from inspect import getargspec  # noqa

import ipdb  # noqa
from ipdb.__main__ import def_colors, def_exec_lines  # noqa
from IPython import version_info as ipython_version  # noqa
from IPython.terminal.interactiveshell import TerminalInteractiveShell  # noqa

def _get_debugger_cls():
    if ipython_version < (5, 0, 0):
        from IPython.core.debugger import Pdb
        return Pdb
    return TerminalInteractiveShell().debugger_cls

def _init_pdb(context=3):
    debugger_cls = _get_debugger_cls()
    if 'context' in getargspec(debugger_cls.__init__)[0]:
        p = debugger_cls(def_colors, context=context)
    else:
        p = debugger_cls(def_colors)
    p.rcLines += def_exec_lines
    return p

ipdb.__main__._init_pdb = _init_pdb

For use with nose/honcho for a month, have yet to have any specific issues with hitting exceptions at debuggers.

@ashb
Copy link

ashb commented Oct 11, 2016

I hit a similar issue (I think -- I got the same error anyway) from trying to debug flake8 to track down an incorrect line number report. Trying the fix proposed by JBKahn removed the exception for me.

@kingbuzzman
Copy link

kingbuzzman commented Oct 19, 2016

Update? I'm running into this now. Ps. @JBKahn code snippet works, just wondering if its going to be integrated anytime soon.

@chyld
Copy link

chyld commented Oct 21, 2016

I was experiencing the same error on an ubuntu box. I was sending a file via stdin as follows:
python main.py < input.txt. Once I stopped sending the file, i.e., python main.py the error went away.

@JBKahn
Copy link
Contributor Author

JBKahn commented Oct 21, 2016

Yup, this is related to what happens when stdout is redirected.

@kingbuzzman
Copy link

Irregardless of why is happening, shouldn't it be addressed?

@JBKahn
Copy link
Contributor Author

JBKahn commented Oct 22, 2016

When @gotcha works on ipdb again, hopefully it will be!

@JBKahn
Copy link
Contributor Author

JBKahn commented Nov 11, 2016

@gotcha just bumping this in case you're active again.

@bar
Copy link

bar commented Dec 1, 2016

hitting the same rock for a couple of months here too, any specific reason for not merging the fix / addressing the issue?

@gnebehay
Copy link
Collaborator

PR merged. Thx, @JBKahn !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment