name mangling in cython 3.0

50 views
Skip to first unread message

Nils Bruin

unread,
Dec 25, 2023, 2:39:57 PM12/25/23
to cython-users
I am encountering a problem with cython 0.* -> cython 3 transition:

More particularly, the instruction

        mpfr_init2(self.r1, self.RR.__prec)

which used to work just fine but now gets the error:

AttributeError: 'sage.rings.real_mpfr.RealField_class' object has no attribute '_RiemannTheta__prec'

so it seems cython now mangles the __prec attribute that was previously unmangled. It mangles it to the name the attribute will certainly not have. I guess the name on RealField_class might be mangled now as well ... to `_RealField_class__prec` then I guess? How can I ensure the attribute is accessible again? Is there a pragma to turn mangling off?

Stefan Behnel

unread,
Dec 25, 2023, 3:38:44 PM12/25/23
to cython...@googlegroups.com
Nils Bruin schrieb am 25.12.23 um 20:39:
This was fixed in Cython 3.0 to match the normal Python behaviour. There is
no option to turn this off.

https://docs.python.org/3/tutorial/classes.html#private-variables

A double underscore prefix represents a private name in Python that is not
meant to be accessed from the outside or even from subclasses. Thus the
mangling. Your best options are to a) not use the private attribute from
outside the class or b) rename the attribute to allow external usage. For
other options, including the chance that Cython is doing something
different from Python here, I'd need more info about the types involved here.

Stefan

Nils Bruin

unread,
Dec 25, 2023, 4:12:48 PM12/25/23
to cython-users
Thanks. This must have been encountered with the transition in sage to cython 3.0. The attribute has been renamed to "_prec" (single underscore), which does not get mangled.
Reply all
Reply to author
Forward
0 new messages