Skip to content

How to get path to .pyx file instead of .so file? #723

@user202729

Description

@user202729

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions