Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c extension finding the module in object initialization

8 views
Skip to first unread message

Robin Becker

unread,
Sep 21, 2021, 9:53:01 AM9/21/21
to
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

Marc-Andre Lemburg

unread,
Sep 27, 2021, 4:38:55 AM9/27/21
to
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/

Robin Becker

unread,
Sep 27, 2021, 5:16:26 AM9/27/21
to
Hi Marc,

Thanks for the suggestion,


On 27/09/2021 09:38, Marc-Andre Lemburg wrote:
> 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,
>

I think I have this working using ob=PyImport_GetModuleDict() followed by PyDict_GetItemString(ob,"modulename"), but I
will ask there to see if there's a more direct route.

In Python >=3.7 there's PyImport_GetModule, but that seems more complex than is actually required for this simple case
and has to wait until 3.6 dies for me :(
--
Robin Becker



0 new messages