-
Notifications
You must be signed in to change notification settings - Fork 81
Closed
Description
I'm installing sage in editable mode with meson.
Currently __file__
returns the path to the .so
file
sage: from sage.rings import integer
sage: integer.__file__
'/home/user202729/sage/build/cp311/src/sage/rings/integer.cpython-311-x86_64-linux-gnu.so'
so is inspect
module functions
sage: import inspect
sage: inspect.getfile(integer)
'/home/user202729/sage/build/cp311/src/sage/rings/integer.cpython-311-x86_64-linux-gnu.so'
How do I get path to the .pyx
file? The best I can come up with is something like this
sage: def find_mesonpy_finder():
....: """
....: Look for the finder object whose class name is 'MesonpyMetaFinder'
....: in sys.meta_path.
....: """
....: for finder in sys.meta_path:
....: cls_name = type(finder).__name__
....: if 'MesonpyMetaFinder' in cls_name:
....: return finder
....: return None
....:
....: def reverse_lookup(node, path_tuple=()):
....: """
....: Recursively traverse the nested dictionary 'node' and return a dict mapping:
....: file_path (str) -> lookup path (tuple of keys)
....: Leaves in the node are assumed to be file paths.
....: """
....: mapping = {}
....: if isinstance(node, dict):
....: for key, value in node.items():
....: new_path = path_tuple + (key,)
....: # if the value is itself a dict then process recursively
....: if isinstance(value, dict):
....: mapping.update(reverse_lookup(value, new_path))
....: else:
....: mapping[value] = new_path
....: return mapping
....:
....: finder = find_mesonpy_finder()
....: node = finder._rebuild()
....: rev_map = reverse_lookup(node)
....:
sage: rev_map[integer.__file__]
('sage', 'rings', 'integer.cpython-311-x86_64-linux-gnu.so')
sage: node["sage"]["rings"]["integer.pyx"]
'/home/user202729/sage/src/sage/rings/integer.pyx'
but it accesses private method _rebuild
.
Metadata
Metadata
Assignees
Labels
No labels