i've had some difficulty with code that attempts to locate a type<br>by its __module__ and __name__, something like:<br> getattr(sys.modules[t.__module__], t.__name__)<br><br>the trouble is, all builtin types claim to belong to the __builtin__ module.
<br>for example:<br> >>> types.FunctionType<br> <type 'function'><br> >>> types.FunctionType.__name__<br> 'funcrtion'<br>
>>> types.FunctionType.__module__<br> '__builtin__'<br><br>but --<br> >>> __builtin__.function<br> Traceback (most recent call last):<br> File "<stdin>", line 1, in <module>
<br> AttributeError: 'module' object has no attribute 'function'<br><br>most, but not all, of the types are exposed in __builtin__... this required<br>me to create an artificial mapping in which "__builtin__.function" is mapped
<br>to types.FunctionType, and then use this mapping instead of sys.modules,<br>which adds more special cases on my part.<br><br>on the other hand, the exceptions module works differently. all builtin<br>exceptions are defined in the exceptions module, but are exposed
<br>through __builtin__:<br> >>> EOFError.__module__<br> 'exceptions'<br> >>> exceptions.EOFError<br> <type 'exceptions.EOFError'><br> >>> __builtin__.EOFError
<br>
<type 'exceptions.EOFError'><br>
<br>so i thought why not do the same with all builtin types? currently the<br>types module (types.py) exposes some type objects (not all), and uses<br>witchcraft to obtain them:<br> try:<br> raise TypeError<br>
except TypeError:<br> tb = sys.exc_info()[2]<br> TracebackType = type(tb)<br> FrameType = type(tb.tb_frame)<br><br>instead, let's make it a builtin module, in which all types will be defined;<br>
the useful types (int, str, ...) would be exposed into __builtin__ (just as <br>the exceptions module does), while the less useful will be kept unexposed.<br><br>this would make FunctionType.__module__ == "types", rather than "__builtin__",
<br>which would allow me to fetch it by name from sys.modules.<br><br><br>-tomer<br>