How to tell the parent frame is a C module? (Lazy importing)

David Christian david.christian at gmail.com
Wed Feb 15 11:45:19 EST 2006


Hello all,
I've been working on a lazy import module to be found here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473888

The basic idea is that a module proxy for foo is returned from an
'import foo' statement.  When an attribute of foo is accessed, or
dir(foo) is called, or a from foo import * is done, the module is
actually loaded, and the module proxy is replaced in the calling
frame.  This relies on the proxy being able to intercept __getattr__,
etc from the module and behaving appropriately.

This seems to work very well except in the case where the importing
module is a C module.

When getting the attributes of a class in C, python does the following check:

if (!PyModule_Check(m)) {
      PyErr_BadInternalCall();
                    return NULL;
}
d = ((PyModuleObject *)m) -> md_dict;

Which does an end-run around all of my module-related evilness.

I'm hoping that there is some way to somehow detect when the import
statement is being called from a C module, and in that case just load
the module normally.  But I can't figure out any way to detect that
from python.  In fact, in sys.frame it looks like any C modules are
simply skipped over.  I could try to detect that an intermdiate frame
is missing, I suppose....

Any pointers?
Dave



More information about the Python-list mailing list