From f77df2a9d93c4c69a15c2233fc96ad280e43400b Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Wed, 12 Feb 2025 10:17:42 +0700 Subject: [PATCH 1/2] Improve sage_getfile by looking at __init__ --- src/sage/misc/sageinspect.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/sage/misc/sageinspect.py b/src/sage/misc/sageinspect.py index 4f7aed2820f..9066671f6f9 100644 --- a/src/sage/misc/sageinspect.py +++ b/src/sage/misc/sageinspect.py @@ -1328,6 +1328,16 @@ def sage_getfile(obj): if isinstance(obj, functools.partial): return sage_getfile(obj.func) return sage_getfile(obj.__class__) # inspect.getabsfile(obj.__class__) + else: + try: + objinit = obj.__init__ + except AttributeError: + pass + else: + pos = _extract_embedded_position(_sage_getdoc_unformatted(objinit)) + if pos is not None: + (_, filename, _) = pos + return filename # No go? fall back to inspect. try: @@ -1336,6 +1346,10 @@ 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 + # 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 From 6aee4983263425ab17a8050f199e3ae42499f7d4 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Fri, 14 Feb 2025 22:15:28 +0700 Subject: [PATCH 2/2] Apply suggestions --- src/sage/misc/sageinspect.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/sage/misc/sageinspect.py b/src/sage/misc/sageinspect.py index 9066671f6f9..6fc0e29551f 100644 --- a/src/sage/misc/sageinspect.py +++ b/src/sage/misc/sageinspect.py @@ -1329,12 +1329,8 @@ def sage_getfile(obj): return sage_getfile(obj.func) return sage_getfile(obj.__class__) # inspect.getabsfile(obj.__class__) else: - try: - objinit = obj.__init__ - except AttributeError: - pass - else: - pos = _extract_embedded_position(_sage_getdoc_unformatted(objinit)) + if hasattr(obj, '__init__'): + pos = _extract_embedded_position(_sage_getdoc_unformatted(obj.__init__)) if pos is not None: (_, filename, _) = pos return filename @@ -1347,6 +1343,7 @@ def sage_getfile(obj): 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 @@ -2370,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):