Skip to content

Commit cace8b2

Browse files
committed
Merge branches 'sort-filter-walk-packages' and 'sage-getfile-2' into meson-try-editable
3 parents 85fb2e6 + 4b24524 + f77df2a commit cace8b2

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/sage/misc/package_dir.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,9 @@ def iter_modules(path=None, prefix=''):
402402
yielded[name] = 1
403403
yield ModuleInfo(i, name, ispkg)
404404

405-
def iter_importer_modules(importer, prefix=''):
405+
def _iter_importer_modules_helper(importer, prefix=''):
406406
r"""
407-
Yield :class:`ModuleInfo` for all modules of ``importer``.
407+
Helper function for :func:`iter_importer_modules`.
408408
"""
409409
from importlib.machinery import FileFinder
410410

@@ -423,11 +423,6 @@ def iter_importer_modules(importer, prefix=''):
423423

424424
for fn in filenames:
425425
modname = inspect.getmodulename(fn)
426-
if modname and (modname in ['__init__', 'all']
427-
or modname.startswith('all__')
428-
or modname in yielded):
429-
continue
430-
431426
path = os.path.join(importer.path, fn)
432427
ispkg = False
433428

@@ -446,6 +441,18 @@ def iter_importer_modules(importer, prefix=''):
446441
else:
447442
yield from importer.iter_modules(prefix)
448443

444+
def iter_importer_modules(importer, prefix=''):
445+
r"""
446+
Yield :class:`ModuleInfo` for all modules of ``importer``.
447+
"""
448+
for name, ispkg in sorted(list(_iter_importer_modules_helper(importer, prefix))):
449+
# we sort again for consistency of output ordering if importer is not
450+
# a FileFinder (needed in doctest of :func:`sage.misc.dev_tools/load_submodules`)
451+
modname = name.rsplit('.', 1)[-1]
452+
if modname in ['__init__', 'all'] or modname.startswith('all__'):
453+
continue
454+
yield name, ispkg
455+
449456
def seen(p, m={}):
450457
if p in m:
451458
return True

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)