classification
Title: inspect.getfile(obj) calls object repr on failure
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, serhiy.storchaka, takluyver
Priority: normal Keywords: easy

Created on 2017-06-12 13:54 by takluyver, last changed 2017-06-12 19:14 by takluyver.

Pull Requests
URL Status Linked Edit
PR 2132 open takluyver, 2017-06-12 15:42
Messages (7)
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.
History
Date User Action Args
2017-06-12 19:14:17takluyversetmessages: + msg295811
2017-06-12 17:56:55serhiy.storchakasetmessages: + msg295809
2017-06-12 17:54:19takluyversetmessages: + msg295808
2017-06-12 17:51:40serhiy.storchakasetkeywords: + easy
2017-06-12 17:50:49serhiy.storchakasetversions: + Python 3.7
nosy: + serhiy.storchaka

messages: + msg295807

type: behavior -> enhancement
2017-06-12 15:42:31takluyversetpull_requests: + pull_request2186
2017-06-12 15:36:05takluyversetmessages: + msg295796
2017-06-12 14:50:55r.david.murraysetnosy: + r.david.murray
messages: + msg295792
2017-06-12 13:54:06takluyvercreate