blob: 212eedb30bc384abb0d284c0212d1efbd25286f3 [file] [log] [blame]
Victor Stinner87d332d2017-10-24 01:29:53 -07001#!/usr/bin/env python
2# Script checking that all symbols exported by libpython start with Py or _Py
3
4import subprocess
5import sys
6import sysconfig
7
8
9def get_exported_symbols():
10 LIBRARY = sysconfig.get_config_var('LIBRARY')
11 if not LIBRARY:
12 raise Exception("failed to get LIBRARY")
13
14 args = ('nm', '-p', LIBRARY)
15 print("+ %s" % ' '.join(args))
16 proc = subprocess.run(args, stdout=subprocess.PIPE, universal_newlines=True)
17 if proc.returncode:
18 sys.stdout.write(proc.stdout)
19 sys.exit(proc.returncode)
20
21 stdout = proc.stdout.rstrip()
22 if not stdout:
23 raise Exception("command output is empty")
24 return stdout
25
26
27def get_smelly_symbols(stdout):
28 symbols = []
29 ignored_symtypes = set()
30 for line in stdout.splitlines():
31 # Split line '0000000000001b80 D PyTextIOWrapper_Type'
32 if not line:
33 continue
34
35 parts = line.split(maxsplit=2)
36 if len(parts) < 3:
37 continue
38
39 symtype = parts[1].strip()
40 # Ignore private symbols.
41 #
42 # If lowercase, the symbol is usually local; if uppercase, the symbol
43 # is global (external). There are however a few lowercase symbols that
44 # are shown for special global symbols ("u", "v" and "w").
45 if symtype.islower() and symtype not in "uvw":
46 ignored_symtypes.add(symtype)
47 continue
48
49 symbol = parts[-1]
50 if symbol.startswith(('Py', '_Py')):
51 continue
52 symbol = '%s (type: %s)' % (symbol, symtype)
53 symbols.append(symbol)
54
55 if ignored_symtypes:
56 print("Ignored symbol types: %s" % ', '.join(sorted(ignored_symtypes)))
57 print()
58 return symbols
59
60
61def main():
62 nm_output = get_exported_symbols()
63 symbols = get_smelly_symbols(nm_output)
64
65 if not symbols:
66 print("OK: no smelly symbol found")
67 sys.exit(0)
68
69 symbols.sort()
70 for symbol in symbols:
71 print("Smelly symbol: %s" % symbol)
72 print()
73 print("ERROR: Found %s smelly symbols!" % len(symbols))
74 sys.exit(1)
75
76
77if __name__ == "__main__":
78 main()