Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trac 16382: fix the dynamic_class._sage_src_lines etc. to return appr…
Browse files Browse the repository at this point in the history
…opriate values on a cdef derived class, rather than raising a TypeError
  • Loading branch information
Nils Bruin committed May 23, 2014
1 parent 4427521 commit 31c39fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
9 changes: 1 addition & 8 deletions src/sage/misc/sageinspect.py
Expand Up @@ -1151,11 +1151,7 @@ def sage_getfile(obj):
# other heuristics will find the source file, so we insist on the presence of
# a _sage_src_file_ method to provide us with the correct file.
if hasattr(obj,"_sage_src_lines_"):
try:
return obj._sage_src_file_()
except TypeError:
# That happes for instances of dynamic classes
return sage_getfile(obj.__class__)
return obj._sage_src_file_()

# We try to extract from docstrings, but not using Python's inspect
# because _sage_getdoc_unformatted is more robust.
Expand Down Expand Up @@ -1808,9 +1804,6 @@ class Element:
return obj._sage_src_lines_()
except AttributeError:
pass
except TypeError:
# That happes for instances of dynamic classes
return sage_getsourcelines(obj.__class__)

# Check if we deal with instance
if isclassinstance(obj):
Expand Down
22 changes: 16 additions & 6 deletions src/sage/structure/dynamic_class.py
Expand Up @@ -387,15 +387,25 @@ def dynamic_class_internal(name, bases, cls=None, reduction=None, doccls=None, p
methods['_reduction'] = reduction
if "_sage_src_lines_" not in methods:
from sage.misc.sageinspect import sage_getsourcelines,sage_getfile
#we'd like the following methods to work both on instances and
#on the class, so we need to be prepared to accept 0 or 1 arguments.
#some trickery is required here. On a dynamic class based on a cdef
#class, such what results from
#P.<x,y> = QQ[]
#these methods end up (bound!) in P.__dict__ itself. Furthermore,
#doccls has the wrong value there. So it's better to look up
#the source on self.__class__ in that case.
@staticmethod
def _sage_src_lines():
return sage_getsourcelines(doccls)
def _sage_src_lines(*args):
if args:
return sage_getsourcelines(args[0].__class__)
else:
return sage_getsourcelines(doccls)
methods['_sage_src_lines_'] = _sage_src_lines
@staticmethod
def _sage_src_file():
return sage_getfile(doccls)
def _sage_src_file(*args):
if args:
return sage_getfile(args[0].__class__)
else:
return sage_getfile(doccls)
methods['_sage_src_file_'] = _sage_src_file

methods['__doc__'] = doccls.__doc__
Expand Down

0 comments on commit 31c39fa

Please sign in to comment.