Skip to content

Commit a01815f

Browse files
author
Release Manager
committed
sagemathgh-39499: Improve sage_getfile by looking at __init__ Otherwise it will fail with meson editable install on objects like `x` of type `Expression`, where the class does not have a docstring but `__init__` method have. The strategy is similar to `sage_getsourcelines` where `__init__` method is looked in. I choose to implement this instead of the more complex workaround (see the comment below). This fixes the test that fails in meson editable mode: ``` 2025-02-11T20:13:36.4801998Z ********************************************************************** 2025-02-11T20:13:36.4803076Z File "src/sage/misc/sageinspect.py", line 1363, in sage.misc.sageinspect.sage_getfile_relative 2025-02-11T20:13:36.4804104Z Failed example: 2025-02-11T20:13:36.4804750Z sage_getfile_relative(x) # needs sage.symbolic 2025-02-11T20:13:36.4808395Z Expected: 2025-02-11T20:13:36.4813350Z 'sage/symbolic/expression.pyx' 2025-02-11T20:13:36.4814244Z Got: 2025-02-11T20:13:36.4815714Z '/home/runner/work/sage/sage/builddir/src/sage/symbolic/expression.pyx' 2025-02-11T20:13:44.3128543Z ********************************************************************** ``` This may still fail in another case where neither class nor `__init__` method has a docstring (in that case `?` will fail to get the file in meson editable mode), but that's not in scope I guess. (I left a comment there to explain) See also sagemath#39369 ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. (change should be invisible to users not using meson editable so…) ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#39499 Reported by: user202729 Reviewer(s):
2 parents b7da8f2 + 6aee498 commit a01815f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/sage/misc/sageinspect.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,12 @@ 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+
if hasattr(obj, '__init__'):
1333+
pos = _extract_embedded_position(_sage_getdoc_unformatted(obj.__init__))
1334+
if pos is not None:
1335+
(_, filename, _) = pos
1336+
return filename
13311337

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

@@ -2356,12 +2367,8 @@ class Element:
23562367
try:
23572368
return inspect.getsourcelines(obj)
23582369
except (OSError, TypeError) as err:
2359-
try:
2360-
objinit = obj.__init__
2361-
except AttributeError:
2362-
pass
2363-
else:
2364-
d = _sage_getdoc_unformatted(objinit)
2370+
if hasattr(obj, '__init__'):
2371+
d = _sage_getdoc_unformatted(obj.__init__)
23652372
pos = _extract_embedded_position(d)
23662373
if pos is None:
23672374
if inspect.isclass(obj):

0 commit comments

Comments
 (0)