c extension finding the module in object initialization
Marc-Andre Lemburg
mal at egenix.com
Mon Sep 27 04:38:16 EDT 2021
Hi Robin,
seeing that no one replied to your question, I'd suggest to ask this
on the Python C-API ML:
https://mail.python.org/mailman3/lists/capi-sig.python.org/
That's where the experts are, including the ones who implemented
the mutli-phase logic.
Cheers,
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Experts (#1, Sep 27 2021)
>>> Python Projects, Coaching and Support ... https://www.egenix.com/
>>> Python Product Development ... https://consulting.egenix.com/
________________________________________________________________________
::: We implement business ideas - efficiently in both time and costs :::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
https://www.egenix.com/company/contact/
https://www.malemburg.com/
On 21.09.2021 14:08, Robin Becker wrote:
> 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
>
More information about the Python-list
mailing list