Repository navigation
gh-158986: Fix profiling.sampling module type for nested site-packages - #158993
Conversation
…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.
| 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) |
There was a problem hiding this comment.
This seems slightly complicated. Wouldn’t it be correct to swap the two tests to check site-packages first?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Thanks @sricursion for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-162545 is a backport of this pull request to the 3.15 branch. |
|
GH-162546 is a backport of this pull request to the 3.15 branch. |
extract_module_name()checked the stdlib directory before the site-packages directories. In a regular installationsite-packagesis inside the stdlib directory, so files in it were reported as stdlib modules namedsite-packages.<module>. The heatmap index then listed them under "Standard Library" and flamegraph labels carried thesite-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 assys.prefix(whichsite.getsitepackages()also returns on Windows), and stdlib wins a tie. The classification dates from the heatmap collector (gh-140677) and was moved tomodule_utilsin gh-142927.