ยปCore Development>Code coverage>Tools/scripts/nm2def.py

Python code coverage for Tools/scripts/nm2def.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a"""nm2def.py
3n/a
4n/aHelpers to extract symbols from Unix libs and auto-generate
5n/aWindows definition files from them. Depends on nm(1). Tested
6n/aon Linux and Solaris only (-p option to nm is for Solaris only).
7n/a
8n/aBy Marc-Andre Lemburg, Aug 1998.
9n/a
10n/aAdditional notes: the output of nm is supposed to look like this:
11n/a
12n/aacceler.o:
13n/a000001fd T PyGrammar_AddAccelerators
14n/a U PyGrammar_FindDFA
15n/a00000237 T PyGrammar_RemoveAccelerators
16n/a U _IO_stderr_
17n/a U exit
18n/a U fprintf
19n/a U free
20n/a U malloc
21n/a U printf
22n/a
23n/agrammar1.o:
24n/a00000000 T PyGrammar_FindDFA
25n/a00000034 T PyGrammar_LabelRepr
26n/a U _PyParser_TokenNames
27n/a U abort
28n/a U printf
29n/a U sprintf
30n/a
31n/a...
32n/a
33n/aEven if this isn't the default output of your nm, there is generally an
34n/aoption to produce this format (since it is the original v7 Unix format).
35n/a
36n/a"""
37n/aimport os, sys
38n/a
39n/aPYTHONLIB = 'libpython%d.%d.a' % sys.version_info[:2]
40n/aPC_PYTHONLIB = 'Python%d%d.dll' % sys.version_info[:2]
41n/aNM = 'nm -p -g %s' # For Linux, use "nm -g %s"
42n/a
43n/adef symbols(lib=PYTHONLIB,types=('T','C','D')):
44n/a
45n/a lines = os.popen(NM % lib).readlines()
46n/a lines = [s.strip() for s in lines]
47n/a symbols = {}
48n/a for line in lines:
49n/a if len(line) == 0 or ':' in line:
50n/a continue
51n/a items = line.split()
52n/a if len(items) != 3:
53n/a continue
54n/a address, type, name = items
55n/a if type not in types:
56n/a continue
57n/a symbols[name] = address,type
58n/a return symbols
59n/a
60n/adef export_list(symbols):
61n/a
62n/a data = []
63n/a code = []
64n/a for name,(addr,type) in symbols.items():
65n/a if type in ('C','D'):
66n/a data.append('\t'+name)
67n/a else:
68n/a code.append('\t'+name)
69n/a data.sort()
70n/a data.append('')
71n/a code.sort()
72n/a return ' DATA\n'.join(data)+'\n'+'\n'.join(code)
73n/a
74n/a# Definition file template
75n/aDEF_TEMPLATE = """\
76n/aEXPORTS
77n/a%s
78n/a"""
79n/a
80n/a# Special symbols that have to be included even though they don't
81n/a# pass the filter
82n/aSPECIALS = (
83n/a )
84n/a
85n/adef filter_Python(symbols,specials=SPECIALS):
86n/a
87n/a for name in list(symbols.keys()):
88n/a if name[:2] == 'Py' or name[:3] == '_Py':
89n/a pass
90n/a elif name not in specials:
91n/a del symbols[name]
92n/a
93n/adef main():
94n/a
95n/a s = symbols(PYTHONLIB)
96n/a filter_Python(s)
97n/a exports = export_list(s)
98n/a f = sys.stdout # open('PC/python_nt.def','w')
99n/a f.write(DEF_TEMPLATE % (exports))
100n/a f.close()
101n/a
102n/aif __name__ == '__main__':
103n/a main()