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

D

David Christian

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top