Name mangling in Cython 3

117 views
Skip to first unread message

Nils Bruin

unread,
Dec 25, 2023, 2:49:24 PM12/25/23
to sage-devel
I think a lot of you may have run into similar issues with transitioning from Cython 0.* to 3.0, so I'm asking here as well. As a reference, I've also asked it on cython-users:


but perhaps a sage-specific perspective from here may help.

In the RiemannTheta package the instruction

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

used to work just fine but now gets the error:

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

(where RR is, as you can see, a sage RealField). I suspect there are two things in the way of this functioning:

 * the name now gets mangled by prefixing "_RiemannTheta"
 * the actual name of the attribute may get mangled as well; presumably to "_RealField_class__prec".

This must have come up in other libraries as well. How did you deal with that? Can we turn off mangling?

It seems to me cython might be a little too eager to mangle here. I don't think double-underscore prefixed names get mangled in python, do they?

Matthias Koeppe

unread,
Dec 25, 2023, 3:31:01 PM12/25/23
to sage-devel
On Monday, December 25, 2023 at 11:49:24 AM UTC-8 Nils Bruin wrote:
I don't think double-underscore prefixed names get mangled in python, do they?

Nils Bruin

unread,
Dec 25, 2023, 3:45:49 PM12/25/23
to sage-devel
Ah OK. Thanks. That explains why the attribute has been renamed to _prec . That's unfortunate for compatibility across versions of sage.

The "prec()" function is not cdef'd by the looks of it, so on cython level one should really use the attribute rather than the accessor function, to avoid the huge penalty of a python function call.
Reply all
Reply to author
Forward
0 new messages