Hi i have a directory containing a directory `bug` and the python program `bad.py`. The code for `bad.py` is ``` import os os.chdir('bug') print(7/0) ``` directory `bug` contains a file `bad.py` this file contents are ``` test 1 test 2 test 3 test 4 test 5 test 6 ``` when i run the python script i get the error text ``` Traceback (most recent call last): File "bad.py", line 4, in test 4 ZeroDivisionError: division by zero ```
I noticed a similar report in the past where using chdir causes pdb to use the file path relative to the directory where it chdir to . I am not sure if both are same but the issue I am talking about is
I did some digging into this, and the problem seems to be that _Py_FindSourceFile() in traceback.c searches through every directory in sys.path (of which the first entry is the working directory) to find a file with the passed filename. So if there's a file in the working directory with name matching the filename passed in from the traceback object, _Py_FindSourceFile will find that one, resulting in the wrong listing being printed. Does the traceback object contain any other information that would avoid the need to search sys.path to find the right file? If it can know the file name, maybe there's a way to find the file path as well? Apologies if this is not useful -- this is my first attempt to contribute to Python.
Upon further digging, the filename to search for ultimately comes from the PyCodeObject where the error occurred. So a possible solution could be to populate the PyCodeObject with the absolute path to the file when it’s first created in compilation. Then no searching is needed and the problem doesn’t happen. This seems like a potentially very invasive change, though.