[capi-sig] Modify tp_getset or PyGetSetDef entries in run-time

Mateusz Loskot mateusz at loskot.net
Thu Jul 5 13:42:17 CEST 2012


Hi,

I have a type object implemented using plain Python 3 C API.
The type has number of attributes - tp_getset slot set with typical
table of geters and seters.
I'd like to find out best way to make certain attributes read-only at run-time.

The table I assign to tp_getset looks like this:

PyGetSetDef Foo_getseters[] =
{
    {"text", (getter)Foo_get_text, (setter)Foo_set_text,
PyDoc_STR("Foo text"), NULL },
    {NULL}
};

Now, at some point at run-time, I'd like to disallow assignment to
.text property
Obvious way to do that is to check desired condition in Foo_set_text()
and set AttributeError.

I wonder if it would be possible and sensible to modify the tp_getset slot and
simply set the Foo_set_text entry in Foo_getseters to NULL.

Is it possible?
Is there any better way to make an attribute read-only at run-time?

Best regards,
-- 
Mateusz Loskot, http://mateusz.loskot.net


More information about the capi-sig mailing list