-
-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trac #20686: Refactor getattr_from_other_class() for lookup of method…
…s in categories 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: 1. 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. 2. The implementation of `getattr_from_other_class` is 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. 3. 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. 4. 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. 5. The lookup using `getattr_from_other_class` is 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. URL: https://trac.sagemath.org/20686 Reported by: nthiery Ticket author(s): Jeroen Demeyer Reviewer(s): Vincent Delecroix
- Loading branch information
Showing
18 changed files
with
384 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.