Issue 30639: inspect.getfile(obj) calls object repr on failure (original) (raw)

Created on 2017-06-12 13:54 by takluyver, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 2132 merged takluyver,2017-06-12 15:42
Messages (9)
msg295782 - (view) Author: Thomas Kluyver (takluyver) * Date: 2017-06-12 13:54
This came up in IPython & Jedi: several functions in inspect end up calling getfile(). If the object is something for which it can't find the source file, it throws an error, the message for which contains the object's repr. This is problematic for us because for some objects the repr may be expensive to calculate (e.g. data tables where the repr forms primary output, not just debugging information). In some cases, it could also throw another error. I plan to make a PR for this, but I'm opening an issue so I've got a bpo number. https://github.com/ipython/ipython/issues/10493 https://github.com/davidhalter/jedi/issues/919
msg295792 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-06-12 14:50
What is your proposed solution? This doesn't at the moment sound like a bug in Python to me.
msg295796 - (view) Author: Thomas Kluyver (takluyver) * Date: 2017-06-12 15:36
Not exactly a bug, but I think that an improvement is possible. My plan is to delay computing the repr until the error is formatted, so that code which catches the TypeError can carry on safely. I think this is more relevant for inspect than for many other modules, because by design, we're often calling inspect functions on arbitrary objects which we can make few guarantees about.
msg295807 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-06-12 17:50
I think in that case it is enough to report just type name.
msg295808 - (view) Author: Thomas Kluyver (takluyver) * Date: 2017-06-12 17:54
Sure, I can do that if you want. The other thing I thought of was using object.__repr__, so the repr always shows like <foo object at 0x123abc...> .
msg295809 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-06-12 17:56
The address of the object is not informative. The possibility of getting a source file is determined by the type of the object.
msg295811 - (view) Author: Thomas Kluyver (takluyver) * Date: 2017-06-12 19:14
OK, I've updated the PR to do that.
msg304903 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-10-24 12:42
New changeset e968bc735794a7123f28f26d68fdf5dc8c845280 by Yury Selivanov (Thomas Kluyver) in branch 'master': bpo-30639: Lazily compute repr for error (#2132) https://github.com/python/cpython/commit/e968bc735794a7123f28f26d68fdf5dc8c845280
msg304905 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-10-24 12:43
Thank you, Thomas.
History
Date User Action Args
2022-04-11 14:58:47 admin set github: 74824
2017-10-24 12:43:35 yselivanov set status: open -> closedresolution: fixedmessages: + stage: resolved
2017-10-24 12:42:38 yselivanov set nosy: + yselivanovmessages: +
2017-06-12 19:14:17 takluyver set messages: +
2017-06-12 17:56:55 serhiy.storchaka set messages: +
2017-06-12 17:54:19 takluyver set messages: +
2017-06-12 17:51:40 serhiy.storchaka set keywords: + easy
2017-06-12 17:50:49 serhiy.storchaka set versions: + Python 3.7nosy: + serhiy.storchakamessages: + type: behavior -> enhancement
2017-06-12 15:42:31 takluyver set pull_requests: + <pull%5Frequest2186>
2017-06-12 15:36:05 takluyver set messages: +
2017-06-12 14:50:55 r.david.murray set nosy: + r.david.murraymessages: +
2017-06-12 13:54:06 takluyver create