Python embedding question: how to expose a new "builtin"?

Alex Martelli aleax at aleax.it
Tue Nov 19 11:19:18 EST 2002


Rodrigo B. de Oliveira wrote:

> I've built an ISAPI module that allows .py files to be executed by IIS,
> everything is working fine and the performance is great. Now I want to
> expose an object in such a way that every imported module could see it, I
> tried the following code but it didn't quite work:
> 
> <snip language="C++">
> 
> PyObject* __builtin__ = PyImport_ImportModule("__builtin__");
> PyObject_SetAttrString(__builtin__, "application",
> (PyObject*)_pyapplication); Py_DECREF(__builtin__);
> 
> </snip>

This looks to me like the correct way to do what you ask.


> Notes:
>     * _pyapplication is an instance of a C new style class

What IS "a C new style class"...?


> The problem is: the modules can see the name "application" but
> dir(application) returns [] and if I try to access any of  application's
> attributes I get an AttributeError.
> 
> Thoughts?

Looks to me like the problem is with the _pyapplication object
rather than with the exposing of it in the built-ins module.  What happens 
if you architect another way to let Python code get at that same object?  
Does "is" show the two ways give the same object, and what are the
symptoms on e.g. dir for each?  I think you should get the same
results, confirming the problem is with the object and not with
the way you're choosing to expose it.


Alex




More information about the Python-list mailing list