Author Steven.Barker
Recipients Claudiu.Popa, Naddiseo, Steven.Barker, eric.araujo, ned.deily, nikitakit, zorceta
Date 2016-05-18.06:45:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1463553942.36.0.456366811333.issue12920@psf.upfronthosting.co.za>
In-reply-to
Content
The problem being discussed here just came up on Stack Overflow today: http://stackoverflow.com/questions/37288135/inspect-module-for-python/

The cause of the incorrect error message is pretty clear. The relevant code from `inspect.getfile` should do something better when the object has a `__module__` attribute, but the module named (when looked up in `sys.modules`) does not have a `__file__` attribute. Currently it says the module is a builtin class, which is total nonsense.

A very basic fix would be to have an extra error case:

    if isclass(object):
        if hasattr(object, '__module__'):
            object = sys.modules.get(object.__module__)
            if hasattr(object, '__file__'):
                return object.__file__
            raise TypeError()              # need a relevant message here!!!
        raise TypeError('{!r} is a built-in class'.format(object))

It might be easier to make a meaningful message if the code after the first `if` didn't overwrite `object` with the module.

But, really, it would be nice to figure out a better fix, which would make the relevant inspect functions actually work for classes defined interactively in the `__main__` module.
History
Date User Action Args
2016-05-18 06:45:42Steven.Barkersetrecipients: + Steven.Barker, ned.deily, eric.araujo, Claudiu.Popa, Naddiseo, zorceta, nikitakit
2016-05-18 06:45:42Steven.Barkersetmessageid: <1463553942.36.0.456366811333.issue12920@psf.upfronthosting.co.za>
2016-05-18 06:45:42Steven.Barkerlinkissue12920 messages
2016-05-18 06:45:42Steven.Barkercreate