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

Improve sage_getfile by looking at __init__ #39499

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/sage/misc/sageinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,12 @@ def sage_getfile(obj):
if isinstance(obj, functools.partial):
return sage_getfile(obj.func)
return sage_getfile(obj.__class__) # inspect.getabsfile(obj.__class__)
else:
if hasattr(obj, '__init__'):
pos = _extract_embedded_position(_sage_getdoc_unformatted(obj.__init__))
if pos is not None:
(_, filename, _) = pos
return filename

# No go? fall back to inspect.
try:
Expand All @@ -1336,6 +1342,11 @@ def sage_getfile(obj):
return ''
for suffix in import_machinery.EXTENSION_SUFFIXES:
if sourcefile.endswith(suffix):
# TODO: the following is incorrect in meson editable install
# because the build is out-of-tree,
# but as long as either the class or its __init__ method has a
# docstring, _sage_getdoc_unformatted should return correct result
# see https://github.com/mesonbuild/meson-python/issues/723
return sourcefile.removesuffix(suffix)+os.path.extsep+'pyx'
return sourcefile

Expand Down Expand Up @@ -2356,12 +2367,8 @@ class Element:
try:
return inspect.getsourcelines(obj)
except (OSError, TypeError) as err:
try:
objinit = obj.__init__
except AttributeError:
pass
else:
d = _sage_getdoc_unformatted(objinit)
if hasattr(obj, '__init__'):
d = _sage_getdoc_unformatted(obj.__init__)
pos = _extract_embedded_position(d)
if pos is None:
if inspect.isclass(obj):
Expand Down
Loading