Steven Barker added the comment:

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.

----------
nosy: +Steven.Barker

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue12920>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to