Skip to content

Commit f77df2a

Browse files
committed
Improve sage_getfile by looking at __init__
1 parent 766c7a0 commit f77df2a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/sage/misc/sageinspect.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,16 @@ def sage_getfile(obj):
13281328
if isinstance(obj, functools.partial):
13291329
return sage_getfile(obj.func)
13301330
return sage_getfile(obj.__class__) # inspect.getabsfile(obj.__class__)
1331+
else:
1332+
try:
1333+
objinit = obj.__init__
1334+
except AttributeError:
1335+
pass
1336+
else:
1337+
pos = _extract_embedded_position(_sage_getdoc_unformatted(objinit))
1338+
if pos is not None:
1339+
(_, filename, _) = pos
1340+
return filename
13311341

13321342
# No go? fall back to inspect.
13331343
try:
@@ -1336,6 +1346,10 @@ def sage_getfile(obj):
13361346
return ''
13371347
for suffix in import_machinery.EXTENSION_SUFFIXES:
13381348
if sourcefile.endswith(suffix):
1349+
# TODO: the following is incorrect in meson editable install
1350+
# but as long as either the class or its __init__ method has a
1351+
# docstring, _sage_getdoc_unformatted should return correct result
1352+
# see https://github.com/mesonbuild/meson-python/issues/723
13391353
return sourcefile.removesuffix(suffix)+os.path.extsep+'pyx'
13401354
return sourcefile
13411355

0 commit comments

Comments
 (0)