Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getattr_from_other_class() for lookup of methods in categories #20686

Closed
nthiery opened this issue May 26, 2016 · 98 comments
Closed

Refactor getattr_from_other_class() for lookup of methods in categories #20686

nthiery opened this issue May 26, 2016 · 98 comments

Comments

@nthiery
Copy link
Contributor

nthiery commented May 26, 2016

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

Depends on #21030
Depends on #21409

Upstream: Fixed upstream, in a later stable release.

CC: @jdemeyer @tscrim

Component: categories

Author: Jeroen Demeyer

Branch/Commit: 74041f3

Reviewer: Vincent Delecroix

Issue created by migration from https://trac.sagemath.org/ticket/20686

@nthiery nthiery added this to the sage-7.3 milestone May 26, 2016
@jdemeyer
Copy link

comment:2

I might have some ideas... discuss in Meudon?

@nthiery
Copy link
Contributor Author

nthiery commented May 26, 2016

comment:3

+1

@jdemeyer

This comment has been minimized.

@jdemeyer
Copy link

Author: Jeroen Demeyer

@jdemeyer
Copy link

@jdemeyer
Copy link

Dependencies: #20825

@jdemeyer
Copy link

New commits:

a143a32EvaluationMethods should be a new-style class
bbd0b05Improve getattr_from_other_class

@jdemeyer
Copy link

Commit: bbd0b05

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 14, 2016

Changed commit from bbd0b05 to 3cd449f

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 14, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

44e80b3EvaluationMethods should be a new-style class
3cd449fImprove getattr_from_other_class

@jdemeyer
Copy link

Upstream: Reported upstream. No feedback yet.

@jdemeyer

This comment has been minimized.

@jdemeyer

This comment has been minimized.

@jdemeyer
Copy link

Changed upstream from Reported upstream. No feedback yet. to Fixed upstream, but not in a stable release.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 14, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

e906b7eImprove getattr_from_other_class

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 14, 2016

Changed commit from 3cd449f to e906b7e

@jdemeyer

This comment has been minimized.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 14, 2016

Changed commit from e906b7e to 92f4520

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 14, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

92f4520Improve getattr_from_other_class

@jdemeyer

This comment has been minimized.

@jdemeyer

This comment has been minimized.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 15, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

9f529b0Improve getattr_from_other_class

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 15, 2016

Changed commit from 92f4520 to 9f529b0

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 15, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

ff3a496Improve getattr_from_other_class

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 15, 2016

Changed commit from 9f529b0 to ff3a496

@videlec
Copy link
Contributor

videlec commented Aug 31, 2016

comment:72

And also not related to _an_element_

**********************************************************************
File "src/sage/combinat/root_system/integrable_representations.py", line 189, in sage.combinat.root_system.integrable_representations.IntegrableRepresentation.
__init__
Failed example:
    TestSuite(V).run()
Expected nothing
Got:
    ...
    Failure in _test_not_implemented_methods:
    Traceback (most recent call last):
    ...
    AssertionError: Not implemented method: __contains__
    ...

@jdemeyer
Copy link

jdemeyer commented Sep 1, 2016

comment:73

The last patchbot report was green, so this must be a recently-introduced problem.

@jdemeyer
Copy link

jdemeyer commented Sep 1, 2016

comment:74

Interestingly, these failures are for classes inheriting from CategoryObject but not Parent.

@jdemeyer
Copy link

jdemeyer commented Sep 1, 2016

comment:75

Those failures are true bugs in HochschildComplex and IntegrableRepresentation: they pretend to be in some category but do not actually implement everything needed to be in that category. This ticket causes the category tests to be run (which is good) but then the tests fail.

sage: SGA = SymmetricGroupAlgebra(QQ, 3)
sage: T = SGA.trivial_representation()
sage: H = SGA.hochschild_complex(T)
sage: H in CommutativeAdditiveGroups()
True
sage: H.zero()
...
TypeError: 'HochschildComplex' object is not callable

and

sage: Lambda = RootSystem(['A',3,1]).weight_lattice(extended=true).fundamental_weights()
sage: V = IntegrableRepresentation(Lambda[1]+Lambda[2]+Lambda[3])
sage: V in CommutativeAdditiveGroups()
True
sage: V.zero()
...
TypeError: 'IntegrableRepresentation' object is not callable

I think the right thing to do here is to mark those tests as # known bug.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 1, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

74041f3Mark broken testsuites as # known bug

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 1, 2016

Changed commit from 0fabb7a to 74041f3

@jdemeyer
Copy link

jdemeyer commented Sep 1, 2016

comment:77

I created #21386 and #21387 for the broken testsuites.

@tscrim
Copy link
Collaborator

tscrim commented Sep 1, 2016

comment:78

I hope to fix both today.

@videlec
Copy link
Contributor

videlec commented Sep 1, 2016

comment:79

good to go!

@vbraun
Copy link
Member

vbraun commented Sep 3, 2016

Changed dependencies from #21030 to #21030, #21409

@vbraun
Copy link
Member

vbraun commented Sep 3, 2016

comment:80

This ticket seems to cause #21409

@vbraun
Copy link
Member

vbraun commented Sep 4, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants