I have a c extension which is intended to implement a module the looks 
structurally like this


############
a = 1
b = 2

class T:
    def __init__(self):
        self.a = a
        self.b = b
############

so when an object of type T is instantiated it can set up defaults based on the 
current module values of a and b.

In the past using old style single phase module creation the init function for the type could look up the module by using the PyState_FindModule function. In the new world where we can implement c extensions which might work with multiple interpreters (multi-phase creation). The docs say PyState_FindModule won't work for those and indeed it returns NULL so is useless.

Is there a way I can set the current module onto the type definition during the module's exec function? I thought it would be easy to add at the point in the exec where I'm doing this to the module, m,

TType.tp_base = &PyBaseObject_Type;
if(PyType_Ready(&TType)<0) goto fail;
if(PyModule_AddObject(m,"T", (PyObject *)&TType)<0) goto fail;

but I don't see the place in the type where I can add these sorts of dynamic attributes. The basic_size is for the created objects.

The created type does have a __dict__ which is a mappingproxy. I wondered when 
that actually gets instantiated.

I can see that the type has a __module__ attribute after the module is imported and I suppose if I can find the 'current' interpreter I might use the __module__ to locate the module object via PyImport_GetModuleDict.

Any expertise or advice gratefully received.
--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to