-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstats.py
More file actions
executable file
·286 lines (222 loc) · 7.73 KB
/
stats.py
File metadata and controls
executable file
·286 lines (222 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/python
import contextlib
import os
from pythoncapi import (
GIT_DIR, git_switch_branch,
PATH_LIMITED_API, PATH_CPYTHON_API, PATH_INTERNAL_API,
list_files,
get_types, get_macros_static_inline_funcs,
get_functions, get_variables, get_line_numbers, get_file_numbers)
BRANCHES = [
('v2.7', '2.7.0'),
('v3.6.0', '3.6.0'),
('v3.7.0', '3.7.0'),
('v3.8.0', '3.8.0'),
('v3.9.0', '3.9.0'),
('v3.10.0', '3.10.0'),
('v3.11.0', '3.11.0'),
('v3.12.0', '3.12.0'),
('v3.13.0', '3.13.0'),
('v3.14.0', '3.14.0'),
('main', 'main (3.15)'),
]
RST_FILENAME = os.path.normpath(os.path.join(os.path.dirname(__file__), 'stats.rst'))
COLUMNS = ['Python', 'Limited API', 'CPython API', 'Internal API', 'Total']
TABLE_SPACE = ' '
output = []
def log(msg=''):
output.append(msg)
def display_title(title):
log(title)
log('=' * len(title))
log()
def paragraph(text):
log(text.strip())
log()
class Data:
pass
def _get_data():
data = Data()
data.line_numbers = get_line_numbers()
data.file_numbers = get_file_numbers()
data.functions = get_functions()
data.variables = get_variables()
data.macro_static_inline_funcs = get_macros_static_inline_funcs()
data.types = get_types()
return data
def get_data():
result = []
for branch, version in BRANCHES:
git_switch_branch(branch)
print(f"Parse {branch} header files")
os.chdir(GIT_DIR)
data = _get_data()
result.append((version, data))
return result
def main_title():
title = 'Statistics on the Python C API'
log('+' * len(title))
log(title)
log('+' * len(title))
log()
def render_table_line(widths, line):
text = []
for width, cell in zip(widths[:-1], line[:-1]):
cell = cell.ljust(width)
text.append(cell)
text.append(line[-1])
log(TABLE_SPACE.join(text))
def render_table(lines):
widths = [0] * len(lines[0])
for line in lines:
for index, cell in enumerate(line):
widths[index] = max(widths[index], len(cell))
table_line = []
for width in widths:
table_line.append('=' * width)
table_line = TABLE_SPACE.join(table_line)
log(table_line)
for line_number, line in enumerate(lines, 1):
render_table_line(widths, line)
if line_number == 1:
log(table_line)
log(table_line)
log()
def format_number(number):
return format(number, ',d')
def format_diff(number):
if number != 0:
return format(number, '+,d')
else:
return 'same'
def table_compute_diff(lines):
previous = None
for index, line in enumerate(lines):
if index >= 2:
new_line = [line[0]]
for prev_value, value in zip(previous[1:], line[1:]):
cell = f'{format_number(value)} ({format_diff(value - prev_value)})'
new_line.append(cell)
previous = line
lines[index] = new_line
elif index == 1:
previous = line
new_line = [line[0]]
for value in line[1:]:
cell = f'{format_number(value)}'
new_line.append(cell)
lines[index] = new_line
else:
pass
def has_include_cpython(name):
return (name not in ('2.7', '3.6', '3.7'))
def line_numbers(results):
display_title('Line Numbers')
paragraph('Number of C API line numbers per Python version:')
lines = [COLUMNS]
for name, data in results:
limited, cpython, internal = data.line_numbers
total = limited + cpython + internal
line = [name]
for value in (limited, cpython, internal):
line.append(f'{format_number(value)} ({value * 100 / total:.0f}%)')
line.append(format_number(total))
lines.append(line)
render_table(lines)
def file_numbers(results):
display_title('File Numbers')
paragraph('Number of header file numbers per Python version:')
lines = [COLUMNS]
for name, data in results:
limited, cpython, internal = data.file_numbers
line = [name, limited, cpython, internal, limited + cpython + internal]
lines.append(line)
table_compute_diff(lines)
render_table(lines)
def list_functions(results):
display_title('Functions')
paragraph('Functions exported with PyAPI_FUNC():')
lines = [('Python', 'Public', 'Private', 'Internal', 'Total')]
for name, data in results:
public, private, internal = data.functions
total = len(public) + len(private) + len(internal)
line = [name, len(public), len(private), len(internal), total]
lines.append(line)
table_compute_diff(lines)
render_table(lines)
paragraph("""
Since Python 3.9, Python is now built with ``-fvisibility=hidden`` to avoid
exporting symbols which are not **explicitly** exported.
The ``make smelly`` command checks for public symbols of libpython and C
extension which are prefixed by ``Py`` or ``_Py``. See
the ``Tools/scripts/smelly.py`` script.
""")
def list_variables(results):
display_title('Variables')
paragraph('Symbols exported with PyAPI_DATA():')
lines = [('Python', 'Public', 'Private', 'Internal', 'Total')]
for name, data in results:
public, private, internal = data.variables
total = len(public) + len(private) + len(internal)
line = [name, len(public), len(private), len(internal), total]
lines.append(line)
table_compute_diff(lines)
render_table(lines)
def _static_inline_func(results, public):
if public:
display_title('Public functions defined as macros and static inline functions')
paragraph('Public functions defined as macros and static inline functions:')
else:
display_title('Private functions defined as macros and static inline functions')
paragraph('Private functions defined as macros and static inline functions:')
lines = [('Python', 'Macro', 'Static inline', 'Total')]
for name, data in results:
if public:
macros, static_inline = data.macro_static_inline_funcs[:2]
else:
macros, static_inline = data.macro_static_inline_funcs[2:]
line = [name, len(macros), len(static_inline),
len(macros) + len(static_inline)]
lines.append(line)
table_compute_diff(lines)
render_table(lines)
if public:
paragraph('Only count public macros and public static inline functions '
'(names starting with "Py" or "PY").')
else:
paragraph('Only count private macros and public static inline functions '
'(ignore names starting with "Py" or "PY").')
def static_inline_func(results):
_static_inline_func(results, True)
_static_inline_func(results, False)
def structures(results):
display_title('Structures')
paragraph('Structures in the Python C API:')
lines = [COLUMNS]
for name, data in results:
limited, cpython, internal = data.types
total = len(limited) + len(cpython) + len(internal)
line = [name, len(limited), len(cpython), len(internal), total]
lines.append(line)
table_compute_diff(lines)
render_table(lines)
paragraph('Count also private structures like "_PyCFrame" '
'and structures with names not starting with Py like "_frozen".')
def render_page():
results = get_data()
main_title()
list_functions(results)
list_variables(results)
line_numbers(results)
file_numbers(results)
static_inline_func(results)
structures(results)
def main():
render_page()
filename = RST_FILENAME
with open(filename, 'w') as fp:
for line in output:
print(line, file=fp)
print(f"Write into {filename}")
if __name__ == "__main__":
main()