-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_patch_safety.py
More file actions
455 lines (390 loc) · 14.7 KB
/
Copy pathpython_patch_safety.py
File metadata and controls
455 lines (390 loc) · 14.7 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from __future__ import annotations
import ast
import builtins
from dataclasses import dataclass
@dataclass(frozen=True)
class ReplacementSpan:
start_line: int
end_line: int
def introduced_unbound_python_names(
*,
source_after_replacement: str,
replacement_offset: int,
replacement_length: int,
) -> tuple[str, ...]:
if replacement_length <= 0:
return ()
try:
tree = ast.parse(source_after_replacement)
except SyntaxError:
return ()
span = _span_from_offset(
source_after_replacement,
offset=replacement_offset,
length=replacement_length,
)
loaded_names = _loaded_names_in_span(tree, span)
if not loaded_names:
return ()
available_names = _available_names_at_span(tree, span)
bound_in_replacement = _bound_names_in_span(tree, span)
missing = loaded_names - available_names - bound_in_replacement
return tuple(sorted(missing))
def removed_imported_python_names_still_used(
*,
source_before_replacement: str,
source_after_replacement: str,
replacement_offset: int,
removed_length: int,
) -> tuple[str, ...]:
if removed_length <= 0:
return ()
try:
before_tree = ast.parse(source_before_replacement)
after_tree = ast.parse(source_after_replacement)
except SyntaxError:
return ()
removed_span = _span_from_offset(
source_before_replacement,
offset=replacement_offset,
length=removed_length,
)
removed_import_names = _module_import_names_in_span(before_tree, removed_span)
if not removed_import_names:
return ()
still_bound = _module_bound_names(after_tree)
loaded_after = _loaded_names(after_tree)
missing = removed_import_names - still_bound
return tuple(sorted(name for name in missing if name in loaded_after))
def removed_module_bound_python_names_still_used(
*,
source_before_replacement: str,
source_after_replacement: str,
replacement_offset: int,
removed_length: int,
) -> tuple[str, ...]:
if removed_length <= 0:
return ()
try:
before_tree = ast.parse(source_before_replacement)
after_tree = ast.parse(source_after_replacement)
except SyntaxError:
return ()
removed_span = _span_from_offset(
source_before_replacement,
offset=replacement_offset,
length=removed_length,
)
removed_names = _module_bound_names_in_span(before_tree, removed_span)
if not removed_names:
return ()
still_bound = _module_bound_names(after_tree)
loaded_after = _loaded_names(after_tree)
missing = removed_names - still_bound
return tuple(sorted(name for name in missing if name in loaded_after))
def introduced_duplicate_python_import_names(
*,
source_before_replacement: str,
source_after_replacement: str,
replacement_offset: int,
removed_length: int,
replacement_length: int,
) -> tuple[str, ...]:
if replacement_length <= 0:
return ()
try:
before_tree = ast.parse(source_before_replacement)
after_tree = ast.parse(source_after_replacement)
except SyntaxError:
return ()
removed_span = _span_from_offset(
source_before_replacement,
offset=replacement_offset,
length=removed_length,
)
replacement_span = _span_from_offset(
source_after_replacement,
offset=replacement_offset,
length=replacement_length,
)
before_removed = _module_import_signatures_in_span(before_tree, removed_span)
after_replacement = _module_import_signatures_in_span(after_tree, replacement_span)
after_outside = _module_import_signatures_outside_span(after_tree, replacement_span)
introduced = after_replacement - before_removed
duplicates = introduced & after_outside
return tuple(sorted(_import_signature_name(signature) for signature in duplicates))
def introduced_unknown_private_self_method_calls(
*,
source_after_replacement: str,
replacement_offset: int,
replacement_length: int,
) -> tuple[str, ...]:
if replacement_length <= 0:
return ()
try:
tree = ast.parse(source_after_replacement)
except SyntaxError:
return ()
span = _span_from_offset(
source_after_replacement,
offset=replacement_offset,
length=replacement_length,
)
missing: set[str] = set()
for node in ast.walk(tree):
if not isinstance(node, ast.Call) or not _node_overlaps_span(node, span):
continue
func = node.func
if not isinstance(func, ast.Attribute):
continue
if not isinstance(func.value, ast.Name) or func.value.id != "self":
continue
if not func.attr.startswith("_"):
continue
enclosing_class = _innermost_enclosing_class(tree, getattr(node, "lineno", 0))
if enclosing_class is None:
continue
if func.attr not in _class_method_names(enclosing_class):
missing.add(func.attr)
return tuple(sorted(missing))
def introduced_unknown_private_self_attribute_loads(
*,
source_after_replacement: str,
replacement_offset: int,
replacement_length: int,
) -> tuple[str, ...]:
if replacement_length <= 0:
return ()
try:
tree = ast.parse(source_after_replacement)
except SyntaxError:
return ()
span = _span_from_offset(
source_after_replacement,
offset=replacement_offset,
length=replacement_length,
)
missing: set[str] = set()
for node in ast.walk(tree):
if not isinstance(node, ast.Attribute) or not _node_overlaps_span(node, span):
continue
if not isinstance(node.value, ast.Name) or node.value.id != "self":
continue
if not node.attr.startswith("_") or isinstance(node.ctx, (ast.Store, ast.Del)):
continue
enclosing_class = _innermost_enclosing_class(tree, getattr(node, "lineno", 0))
if enclosing_class is None:
continue
if node.attr not in _class_private_member_names(enclosing_class):
missing.add(node.attr)
return tuple(sorted(missing))
def _span_from_offset(source: str, *, offset: int, length: int) -> ReplacementSpan:
start_line = source.count("\n", 0, offset) + 1
replacement_text = source[offset : offset + length]
end_line = start_line + replacement_text.count("\n")
return ReplacementSpan(start_line=start_line, end_line=max(start_line, end_line))
def _loaded_names_in_span(tree: ast.AST, span: ReplacementSpan) -> set[str]:
return {
node.id
for node in ast.walk(tree)
if isinstance(node, ast.Name)
and isinstance(node.ctx, ast.Load)
and _node_overlaps_span(node, span)
}
def _loaded_names(tree: ast.AST) -> set[str]:
return {
node.id
for node in ast.walk(tree)
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Load)
}
def _module_import_names_in_span(tree: ast.Module, span: ReplacementSpan) -> set[str]:
names: set[str] = set()
for statement in tree.body:
if not isinstance(statement, (ast.Import, ast.ImportFrom)):
continue
if _node_overlaps_span(statement, span):
names.update(_import_bound_names(statement))
return names
def _module_bound_names_in_span(tree: ast.Module, span: ReplacementSpan) -> set[str]:
names: set[str] = set()
for statement in tree.body:
if _node_overlaps_span(statement, span):
names.update(_bound_names_from_statement(statement))
return names
def _module_import_signatures_in_span(
tree: ast.Module,
span: ReplacementSpan,
) -> set[tuple[str, str, str, str]]:
signatures: set[tuple[str, str, str, str]] = set()
for statement in tree.body:
if not isinstance(statement, (ast.Import, ast.ImportFrom)):
continue
if _node_overlaps_span(statement, span):
signatures.update(_import_signatures(statement))
return signatures
def _module_import_signatures_outside_span(
tree: ast.Module,
span: ReplacementSpan,
) -> set[tuple[str, str, str, str]]:
signatures: set[tuple[str, str, str, str]] = set()
for statement in tree.body:
if not isinstance(statement, (ast.Import, ast.ImportFrom)):
continue
if not _node_overlaps_span(statement, span):
signatures.update(_import_signatures(statement))
return signatures
def _bound_names_in_span(tree: ast.AST, span: ReplacementSpan) -> set[str]:
names: set[str] = set()
for node in ast.walk(tree):
if not _node_overlaps_span(node, span):
continue
names.update(_bound_names_from_node(node))
return names
def _available_names_at_span(tree: ast.Module, span: ReplacementSpan) -> set[str]:
names = set(dir(builtins)) | {
"__builtins__",
"__file__",
"__loader__",
"__name__",
"__package__",
"__spec__",
}
names.update(_module_bound_names(tree))
for scope in _enclosing_scopes(tree, span.start_line):
if isinstance(scope, (ast.FunctionDef, ast.AsyncFunctionDef)):
names.update(_argument_names(scope.args))
names.update(_bound_names_before_line(scope.body, span.start_line))
elif isinstance(scope, ast.Lambda):
names.update(_argument_names(scope.args))
elif isinstance(scope, ast.ClassDef):
names.update(_bound_names_before_line(scope.body, span.start_line))
return names
def _module_bound_names(tree: ast.Module) -> set[str]:
return _bound_names_before_line(tree.body, line=10**9)
def _enclosing_scopes(tree: ast.AST, line: int) -> list[ast.AST]:
scopes: list[ast.AST] = []
for node in ast.walk(tree):
if not isinstance(
node,
(ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda, ast.ClassDef),
):
continue
if _line_inside_node(node, line):
scopes.append(node)
scopes.sort(key=lambda node: (getattr(node, "lineno", 0), -(getattr(node, "end_lineno", 0))))
return scopes
def _innermost_enclosing_class(tree: ast.AST, line: int) -> ast.ClassDef | None:
classes = [
node
for node in ast.walk(tree)
if isinstance(node, ast.ClassDef) and _line_inside_node(node, line)
]
if not classes:
return None
classes.sort(key=lambda node: (getattr(node, "lineno", 0), -(getattr(node, "end_lineno", 0))))
return classes[-1]
def _class_method_names(node: ast.ClassDef) -> set[str]:
return {
statement.name
for statement in node.body
if isinstance(statement, (ast.FunctionDef, ast.AsyncFunctionDef))
}
def _class_private_member_names(node: ast.ClassDef) -> set[str]:
names = _class_method_names(node)
for child in ast.walk(node):
if not isinstance(child, ast.Attribute):
continue
if not isinstance(child.value, ast.Name) or child.value.id != "self":
continue
if child.attr.startswith("_") and isinstance(child.ctx, ast.Store):
names.add(child.attr)
return names
def _bound_names_before_line(statements: list[ast.stmt], line: int) -> set[str]:
names: set[str] = set()
for statement in statements:
if getattr(statement, "lineno", line) >= line:
continue
names.update(_bound_names_from_statement(statement))
return names
def _bound_names_from_statement(statement: ast.stmt) -> set[str]:
if isinstance(statement, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
return {statement.name}
if isinstance(statement, (ast.Import, ast.ImportFrom)):
return _import_bound_names(statement)
if isinstance(statement, ast.If):
return _definitely_bound_names_from_if(statement)
return _bound_names_from_node(statement)
def _definitely_bound_names_from_if(statement: ast.If) -> set[str]:
if not statement.orelse:
return set()
return _bound_names_from_statements(statement.body) & _bound_names_from_statements(
statement.orelse
)
def _bound_names_from_statements(statements: list[ast.stmt]) -> set[str]:
names: set[str] = set()
for statement in statements:
names.update(_bound_names_from_statement(statement))
return names
def _bound_names_from_node(node: ast.AST) -> set[str]:
names: set[str] = set()
if isinstance(node, ast.ExceptHandler) and isinstance(node.name, str):
names.add(node.name)
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
names.add(node.name)
if isinstance(node, (ast.Import, ast.ImportFrom)):
names.update(_import_bound_names(node))
for child in ast.walk(node):
if isinstance(child, ast.Name) and isinstance(child.ctx, (ast.Store, ast.Param)):
names.add(child.id)
elif isinstance(child, ast.arg):
names.add(child.arg)
return names
def _import_bound_names(node: ast.Import | ast.ImportFrom) -> set[str]:
names: set[str] = set()
for alias in node.names:
if alias.asname:
names.add(alias.asname)
continue
names.add(alias.name.split(".", 1)[0])
return names
def _import_signatures(node: ast.Import | ast.ImportFrom) -> set[tuple[str, str, str, str]]:
signatures: set[tuple[str, str, str, str]] = set()
if isinstance(node, ast.Import):
for alias in node.names:
signatures.add(("import", alias.name, "", alias.asname or ""))
return signatures
module = node.module or ""
for alias in node.names:
signatures.add(("from", module, alias.name, alias.asname or ""))
return signatures
def _import_signature_name(signature: tuple[str, str, str, str]) -> str:
import_type, module, imported_name, alias = signature
if alias:
return alias
if import_type == "import":
return module.split(".", 1)[0]
return imported_name
def _argument_names(arguments: ast.arguments) -> set[str]:
args = [
*arguments.posonlyargs,
*arguments.args,
*arguments.kwonlyargs,
]
names = {arg.arg for arg in args}
if arguments.vararg is not None:
names.add(arguments.vararg.arg)
if arguments.kwarg is not None:
names.add(arguments.kwarg.arg)
return names
def _node_overlaps_span(node: ast.AST, span: ReplacementSpan) -> bool:
node_start = getattr(node, "lineno", None)
node_end = getattr(node, "end_lineno", node_start)
if node_start is None or node_end is None:
return False
return node_start <= span.end_line and node_end >= span.start_line
def _line_inside_node(node: ast.AST, line: int) -> bool:
node_start = getattr(node, "lineno", None)
node_end = getattr(node, "end_lineno", None)
if node_start is None or node_end is None:
return False
return node_start <= line <= node_end