This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: exception text is being sourced from the wrong file
Type: behavior Stage:
Components: Versions: Python 3.8, Python 3.7, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jesse.farnham, rhubarbdog x, steven.daprano, xtreak
Priority: normal Keywords:

Created on 2019-03-11 01:51 by rhubarbdog x, last changed 2022-04-11 14:59 by admin.

Messages (4)
msg337642 - (view) Author: rhubarbdog x (rhubarbdog x) Date: 2019-03-11 01:51
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 <module>
    test 4
ZeroDivisionError: division by zero
```
msg337647 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-03-11 05:14
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 issue33139
msg339581 - (view) Author: Jesse Farnham (jesse.farnham) * Date: 2019-04-07 17:44
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.
msg339582 - (view) Author: Jesse Farnham (jesse.farnham) * Date: 2019-04-07 19:04
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.
History
Date User Action Args
2022-04-11 14:59:12adminsetgithub: 80440
2019-04-07 19:04:59jesse.farnhamsetmessages: + msg339582
2019-04-07 17:44:46jesse.farnhamsetmessages: + msg339581
2019-04-07 17:35:23jesse.farnhamsetnosy: + jesse.farnham
2019-03-11 08:28:15ronaldoussorensetversions: + Python 2.7, Python 3.7, Python 3.8
2019-03-11 05:14:12xtreaksetnosy: + xtreak
messages: + msg337647
2019-03-11 02:06:34steven.dapranosetnosy: + steven.daprano
2019-03-11 01:51:16rhubarbdog xcreate