Bug report
Bug description:
profiling.sampling.module_utils.extract_module_name() checks the stdlib directory before the site-packages directories. In a regular (non-venv) installation site-packages is a subdirectory of the stdlib directory (Lib\site-packages on Windows, lib/pythonX.Y/site-packages for python.org, pyenv, conda or Docker builds, and Lib/site-packages in a source build), so every file installed there matches the stdlib check first.
import os
import sysconfig
from profiling.sampling.module_utils import extract_module_name, get_python_path_info
path_info = get_python_path_info()
filename = os.path.join(sysconfig.get_path('purelib'), 'requests', 'api.py')
print(extract_module_name(filename, path_info))
Output (Windows, <prefix>\Lib is the stdlib directory and <prefix>\Lib\site-packages is the site-packages directory):
('site-packages.requests.api', 'stdlib')
Expected:
('requests.api', 'site-packages')
As a result, the heatmap index lists third-party code under "Standard Library", inside a folder named site-packages, instead of under "Site Packages", and the flamegraph labels of such frames look like get (site-packages.requests.api:10). Doc/library/profiling.sampling.rst describes the heatmap index as organized by type (stdlib, site-packages, project). Only virtual environments (and the user site directory), where site-packages is not inside the stdlib directory, are classified correctly.
Simply checking site-packages first is not enough: on Windows site.getsitepackages() also returns sys.prefix, which is a parent of the stdlib directory and would then capture the stdlib files. The most specific (longest) matching directory should win, with stdlib winning a tie.
CPython versions tested on:
CPython main branch
(Lib/profiling/sampling/module_utils.py is identical on the 3.15 branch.)
Operating systems tested on:
Windows
Linked PRs
Bug report
Bug description:
profiling.sampling.module_utils.extract_module_name()checks the stdlib directory before the site-packages directories. In a regular (non-venv) installationsite-packagesis a subdirectory of the stdlib directory (Lib\site-packageson Windows,lib/pythonX.Y/site-packagesfor python.org, pyenv, conda or Docker builds, andLib/site-packagesin a source build), so every file installed there matches the stdlib check first.Output (Windows,
<prefix>\Libis the stdlib directory and<prefix>\Lib\site-packagesis the site-packages directory):Expected:
As a result, the heatmap index lists third-party code under "Standard Library", inside a folder named
site-packages, instead of under "Site Packages", and the flamegraph labels of such frames look likeget (site-packages.requests.api:10).Doc/library/profiling.sampling.rstdescribes the heatmap index as organized by type (stdlib, site-packages, project). Only virtual environments (and the user site directory), where site-packages is not inside the stdlib directory, are classified correctly.Simply checking site-packages first is not enough: on Windows
site.getsitepackages()also returnssys.prefix, which is a parent of the stdlib directory and would then capture the stdlib files. The most specific (longest) matching directory should win, with stdlib winning a tie.CPython versions tested on:
CPython main branch
(
Lib/profiling/sampling/module_utils.pyis identical on the 3.15 branch.)Operating systems tested on:
Windows
Linked PRs