gotcha / ipdb

Integration of IPython pdb
BSD 3-Clause "New" or "Revised" License
1.85k stars 146 forks source link

`pyproject.toml` reading upon initialization raises KeyError exception #227

Closed anjos closed 3 years ago

anjos commented 3 years ago

Just started ipdb on a directory that happened to contain a pyproject.toml file inside to get a KeyError exception on tool.

To reproduce this problem, pip install the latest ipdb, create an empty pyproject.toml (n.b: it does not have to empty, it should just not contain any tool section) in the directory you are launching it, and then you will observe the crash (tested with ipdb 0.13.7):

$ pip install ipdb
$ mkdir tmp
$ cd tmp
$ touch pyproject.toml
$ touch test.py
$ python -m ipdb test.py
/Users/andre/conda/envs/bdt/lib/python3.8/runpy.py:127: RuntimeWarning: 'ipdb.__main__' found in sys.modules after import of package 'ipdb', but prior to execution of 'ipdb.__main__'; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))
Traceback (most recent call last):
  File "/Users/andre/conda/envs/bdt/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/andre/conda/envs/bdt/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/andre/conda/envs/bdt/lib/python3.8/site-packages/ipdb/__main__.py", line 323, in <module>
    main()
  File "/Users/andre/conda/envs/bdt/lib/python3.8/site-packages/ipdb/__main__.py", line 295, in main
    pdb = _init_pdb(commands=commands)
  File "/Users/andre/conda/envs/bdt/lib/python3.8/site-packages/ipdb/__main__.py", line 53, in _init_pdb
    context = os.getenv("IPDB_CONTEXT_SIZE", get_context_from_config())
  File "/Users/andre/conda/envs/bdt/lib/python3.8/site-packages/ipdb/__main__.py", line 84, in get_context_from_config
    parser = get_config()
  File "/Users/andre/conda/envs/bdt/lib/python3.8/site-packages/ipdb/__main__.py", line 181, in get_config
    if "ipdb" in toml_file["tool"]:
KeyError: 'tool'

Upon inspection, the code on line 181 could be better implemented and avoid this with something like:

if "ipdb" in toml_file.get("tool"):
    ....

I'll submit a PR in a bit.