-
-
Notifications
You must be signed in to change notification settings - Fork 684
Description
For parents and elements implemented in Cython, category lookup is emulated in __getattr__ using getattr_from_other_class. In this ticket, this is refactored a bit:
- We should not pick up special attributes from
type, there is no point in returning the type name of the category here (similarly for__dict__,__mro__, ...):
sage: ZZ(1).__name__
'JoinCategory.element_class'
sage: ZZ.__name__
'JoinCategory.parent_class'
This change causes a few failures which need to be fixed.
-
The implementation of
getattr_from_other_classis not very robust. For example, static methods are not supported. We fix this by using low-level Python functions to get the attribute and we manually call the descriptor__get__if needed. -
We shouldn't do anything special with double-underscore private attributes: in plain Python, this is implemented by the parser and not by
getattr(). So__getattr__would already receive the mangled private name. -
Some of the changes broke
_sage_src_lines_, so we also change that: currently,_sage_src_lines_()is used both to get the source lines of a class (e.g. dynamic classes) and an instance (e.g. cached functions). We change this to always mean the source lines of an instance, which makes things clearer. -
The lookup using
getattr_from_other_classis about 9 times slower than a normal method lookup::
sage: def foo(self): return
sage: Sets().element_class.foo = foo
sage: def g(x):
....: for i in range(1000):
....: x.foo()
sage: x = Semigroups().example().an_element()
sage: y = 1
sage: %timeit g(x)
10000 loops, best of 3: 115 µs per loop
sage: %timeit g(y)
1000 loops, best of 3: 1.18 ms per loop
We improve on this by roughly a factor 2 (but even then, it's still a lot slower than usual method lookup).
NOTE: This needs a trivial Cython patch, see #21030 for the Cython upgrade.
Depends on #21030
Depends on #21409
Upstream: Fixed upstream, in a later stable release.
Component: categories
Author: Jeroen Demeyer
Branch/Commit: 74041f3
Reviewer: Vincent Delecroix
Issue created by migration from https://trac.sagemath.org/ticket/20686