MCPcopy Index your code
hub / github.com/python/cpython / extract_module_name

Function extract_module_name

Lib/profiling/sampling/heatmap_collector.py:96–143  ·  view source on GitHub ↗

Extract Python module name and type from file path. Args: filename: Path to the Python file path_info: Dictionary from get_python_path_info() Returns: tuple: (module_name, module_type) where module_type is one of: 'stdlib', 'site-packages', 'project',

(filename, path_info)

Source from the content-addressed store, hash-verified

94
95
96def extract_module_name(filename, path_info):
97 """Extract Python module name and type from file path.
98
99 Args:
100 filename: Path to the Python file
101 path_info: Dictionary from get_python_path_info()
102
103 Returns:
104 tuple: (module_name, module_type) where module_type is one of:
105 'stdlib', 'site-packages', 'project', or 'other'
106 """
107 if not filename:
108 return ('unknown', 'other')
109
110 try:
111 file_path = Path(filename)
112 except (ValueError, OSError):
113 return (str(filename), 'other')
114
115 # Check if it's in stdlib
116 if path_info['stdlib'] and _is_subpath(file_path, path_info['stdlib']):
117 try:
118 rel_path = file_path.relative_to(path_info['stdlib'])
119 return (_path_to_module(rel_path), 'stdlib')
120 except ValueError:
121 pass
122
123 # Check site-packages
124 for site_pkg in path_info['site_packages']:
125 if _is_subpath(file_path, site_pkg):
126 try:
127 rel_path = file_path.relative_to(site_pkg)
128 return (_path_to_module(rel_path), 'site-packages')
129 except ValueError:
130 continue
131
132 # Check other sys.path entries (project files)
133 if not str(file_path).startswith(('<', '[')): # Skip special files
134 for path_entry in path_info['sys_path']:
135 if _is_subpath(file_path, path_entry):
136 try:
137 rel_path = file_path.relative_to(path_entry)
138 return (_path_to_module(rel_path), 'project')
139 except ValueError:
140 continue
141
142 # Fallback: just use the filename
143 return (_path_to_module(file_path), 'other')
144
145
146def _is_subpath(file_path, parent_path):

Calls 6

relative_toMethod · 0.95
PathClass · 0.90
strFunction · 0.85
_is_subpathFunction · 0.85
_path_to_moduleFunction · 0.85
startswithMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…