pudb icon indicating copy to clipboard operation
pudb copied to clipboard

"Debugger instance already exists" when used in pytest

Open hydrargyrum opened this issue 1 year ago • 5 comments

Describe the bug

When 2 breakpoints are encountered in pytest with pudb set up, this error pops: Debugger instance already exists

To Reproduce

Steps to reproduce the behavior:

  1. install pudb and pytest in venv
  2. create this test.py
def test_foo():
    breakpoint()
    breakpoint()
  1. pytest --pdbcls pudb.debugger:Debugger --pdb --capture=no test.py (following https://documen.tician.de/pudb/starting.html#usage-with-pytest)
  2. First breakpoint is encountered in pudb, press c to continue to second one
  3. See error

Expected behavior

The debugger should be called on second breakpoint

Versions

pudb 2022.1.3

hydrargyrum avatar Jun 19 '23 13:06 hydrargyrum

Why not use pytest-pudb? (installable via pip install)

inducer avatar Jun 19 '23 13:06 inducer

I thought from the documentation the new way was just adding a few options instead of having to install it.

hydrargyrum avatar Jun 19 '23 15:06 hydrargyrum

Actually, it's not working very well either. After installing it and running pytest --pudb test.py --pdbcls pudb.debugger:Debugger, I get the same error, but within pudb this time

It works fine with pytest-pudb installed (else it fails) and then running PYTHONBREAKPOINT="pudb.set_trace" pytest test.py, this is all very confusing and not matching documentation

hydrargyrum avatar Jun 19 '23 15:06 hydrargyrum

Try without the --pdbcls.

inducer avatar Jun 19 '23 17:06 inducer

I can't reproduce with 2 breakpoint(), but with this instead:

def test_foo():
    breakpoint()
    raise Exception

And running with the same command: pytest --pdbcls pudb.debugger:Debugger --pdb --capture=no test.py It fails with:

  File "[...]/.venv/lib/python3.11/site-packages/pudb/debugger.py", line 196, in __init__
    raise ValueError("a Debugger instance already exists")
ValueError: a Debugger instance already exists

I expect to see this instead (here I pressed already e): image

The issue can be fixed by commenting these two lines here:

        if Debugger._current_debugger:
           raise ValueError("a Debugger instance already exists")

But I assume that this fix is too dirty to be committed... yet I don't understand why this check is needed, for me it works fine (better) without it.

souliane avatar Aug 30 '23 15:08 souliane