Skip to content

gh-158986: Fix profiling.sampling module type for nested site-packages - #158993

Merged
pablogsal merged 1 commit into
python:mainfrom
sricursion:profiling-site-packages-in-stdlib
Nov 24, 2026
Merged

pablogsal merged 1 commit into
python:mainfrom
sricursion:profiling-site-packages-in-stdlib

Conversation

@sricursion

@sricursion sricursion commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

extract_module_name() checked the stdlib directory before the site-packages directories. In a regular installation site-packages is inside the stdlib directory, so files in it were reported as stdlib modules named site-packages.<module>. The heatmap index then listed them under "Standard Library" and flamegraph labels carried the site-packages. prefix. The fix collects the stdlib and site-packages directories that contain the file and uses the one with the most path components, so a stdlib file still wins over a broader entry such as sys.prefix (which site.getsitepackages() also returns on Windows), and stdlib wins a tie. The classification dates from the heatmap collector (gh-140677) and was moved to module_utils in gh-142927.

…ackages

extract_module_name() checked the stdlib directory first, so files in a
site-packages directory inside it were reported as stdlib modules. Use
the most specific matching directory instead.
@sricursion
sricursion requested a review from pablogsal as a code owner October 7, 2026 20:27
@python-cla-bot

python-cla-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

if matches:
# max() returns the first of equal candidates, so stdlib wins ties
base, module_type = max(matches, key=lambda m: len(m[0].parts))
return (_path_to_module(file_path.relative_to(base)), module_type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems slightly complicated. Wouldn’t it be correct to swap the two tests to check site-packages first?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swapping them works on the usual POSIX layouts, but not on Windows, where site.getsitepackages() returns sys.prefix as well as Lib\site-packages. sys.prefix contains the stdlib directory, so with site-packages checked first every stdlib file is reported as third-party:

>>> extract_module_name(r'<prefix>\Lib\json\decoder.py', info)
('Lib.json.decoder', 'site-packages')
>>> extract_module_name(r'<prefix>\Lib\site-packages\requests\api.py', info)
('Lib.site-packages.requests.api', 'site-packages')

Picking the most specific matching directory handles both layouts. I'm happy to restructure it if you think there's a clearer way to write that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, I see the need. Wish the implementation was simpler.


Is the code showing a bug with the current code? Module names (first element in tuples shown) should be json.decoder and requests.api.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, sorry, that wasn't clear: those outputs are what you get when the site-packages check is moved before the stdlib check. The three versions side by side on Windows:

<prefix>\Lib\json\decoder.py <prefix>\Lib\site-packages\requests\api.py
main ('json.decoder', 'stdlib') ('site-packages.requests.api', 'stdlib')
site-packages checked first ('Lib.json.decoder', 'site-packages') ('Lib.site-packages.requests.api', 'site-packages')
this PR ('json.decoder', 'stdlib') ('requests.api', 'site-packages')

So on main, only the site-packages file is wrong, which is the bug this fixes.

@pablogsal pablogsal added the needs backport to 3.15 bugs and security fixes label Nov 24, 2026

@pablogsal pablogsal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix!

@pablogsal
pablogsal merged commit f05dde9 into python:main Nov 24, 2026
54 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @sricursion for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Nov 24, 2026

Copy link
Copy Markdown

GH-162545 is a backport of this pull request to the 3.15 branch.

@bedevere-app

bedevere-app Bot commented Nov 24, 2026

Copy link
Copy Markdown

GH-162546 is a backport of this pull request to the 3.15 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants