-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
bpo-31810: Add smelly.py to check exported symbols #4057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/usr/bin/env python | ||
| # Script checking that all symbols exported by libpython start with Py or _Py | ||
|
|
||
| import subprocess | ||
| import sys | ||
| import sysconfig | ||
|
|
||
|
|
||
| def get_exported_symbols(): | ||
| LIBRARY = sysconfig.get_config_var('LIBRARY') | ||
| if not LIBRARY: | ||
| raise Exception("failed to get LIBRARY") | ||
|
|
||
| args = ('nm', '-p', LIBRARY) | ||
| print("+ %s" % ' '.join(args)) | ||
| proc = subprocess.run(args, stdout=subprocess.PIPE, universal_newlines=True) | ||
| if proc.returncode: | ||
| sys.stdout.write(proc.stdout) | ||
| sys.exit(proc.returncode) | ||
|
|
||
| stdout = proc.stdout.rstrip() | ||
| if not stdout: | ||
| raise Exception("command output is empty") | ||
| return stdout | ||
|
|
||
|
|
||
| def get_smelly_symbols(stdout): | ||
| symbols = [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not have this function be a generator and just yield the smelly symbols? Just style preference so feel free to ignore.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer a list :-) |
||
| ignored_symtypes = set() | ||
| for line in stdout.splitlines(): | ||
| # Split line '0000000000001b80 D PyTextIOWrapper_Type' | ||
| if not line: | ||
| continue | ||
|
|
||
| parts = line.split(maxsplit=2) | ||
| if len(parts) < 3: | ||
| continue | ||
|
|
||
| symtype = parts[1].strip() | ||
| # Ignore private symbols. | ||
| # | ||
| # If lowercase, the symbol is usually local; if uppercase, the symbol | ||
| # is global (external). There are however a few lowercase symbols that | ||
| # are shown for special global symbols ("u", "v" and "w"). | ||
| if symtype.islower() and symtype not in "uvw": | ||
| ignored_symtypes.add(symtype) | ||
| continue | ||
|
|
||
| symbol = parts[-1] | ||
| if symbol.startswith(('Py', '_Py')): | ||
| continue | ||
| symbol = '%s (type: %s)' % (symbol, symtype) | ||
| symbols.append(symbol) | ||
|
|
||
| if ignored_symtypes: | ||
| print("Ignored symbol types: %s" % ', '.join(sorted(ignored_symtypes))) | ||
| print() | ||
| return symbols | ||
|
|
||
|
|
||
| def main(): | ||
| nm_output = get_exported_symbols() | ||
| symbols = get_smelly_symbols(nm_output) | ||
|
|
||
| if not symbols: | ||
| print("OK: no smelly symbol found") | ||
| sys.exit(0) | ||
|
|
||
| symbols.sort() | ||
| for symbol in symbols: | ||
| print("Smelly symbol: %s" % symbol) | ||
| print() | ||
| print("ERROR: Found %s smelly symbols!" % len(symbols)) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stdoutis not a very informative name here, and somewhat confusing in the body of the function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, renamed to nm_output.