/
core.py
604 lines (496 loc) · 16.9 KB
/
core.py
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
from __future__ import annotations
import dataclasses
import dis
import functools
import inspect
import itertools
import operator as op
import os
import sys
import types
import warnings
from typing import *
from . import jsonl
import opcode
from . import get_stack
__all__ = ["Tracer", "setup", "finalize", "get_tracer"]
DEBUG = os.environ.get("PYTHON_RECORD_API_DEBUG", False)
# global cache for tracer based on env variables
TRACER = None
context_manager: Optional[ContextManager] = None
write_line: Optional[Callable[[dict], None]] = None
def get_tracer() -> Tracer:
global TRACER
if not TRACER:
FROM_MODULES = os.environ["PYTHON_RECORD_API_TO_MODULES"].split(",")
TO_MODULES = os.environ["PYTHON_RECORD_API_FROM_MODULES"].split(",")
TRACER = Tracer(FROM_MODULES, TO_MODULES)
return TRACER
def getmodulename(v):
if isinstance(v, types.ModuleType):
return v.__name__
if isinstance(v, types.MethodDescriptorType):
return getmodulename(v.__objclass__)
return getattr(v, "__module__", None)
@functools.singledispatch
def encode(o: object) -> object:
"""
Encodes an object for serialization. Type is always serialized as well
If falsy is returned, the value is not included.
"""
return None
@encode.register
def _encode_module(o: types.ModuleType):
return o.__name__
@encode.register
def _encode_slice(s: slice):
return {
"start": preprocess(s.start),
"stop": preprocess(s.stop),
"step": preprocess(s.step),
}
# # cache this b/c its expesnive
# @functools.lru_cache(None, False)
# def find_root_module(o: object):
# """
# Looks from the top of the module tree down to see if it is exportd from a parent module
# """
# full_module = o.__module__
# modules = full_module.split(".")
# if len(modules) == 1:
# return modules[0]
# for i in range(len(modules) - 1):
# parent_module = ".".join(modules[i:])
# mod = importlib.import_module(parent_module)
# # Export by module
# if o in mod
@encode.register(types.FunctionType)
@encode.register(type)
def encode_module_value(v):
"""
For all things not in builtins, return the module name, otherwise just return the name
"""
mod = v.__module__
v = getattr(v, "__qualname__", v.__name__)
if mod == "builtins":
return v
return {"module": mod, "name": v}
@encode.register
def _encode_builtin_function_method(m: types.BuiltinMethodType):
self = m.__self__
# If the self is a module, then this is a function and we can just emit the module
# as a string
if isinstance(self, types.ModuleType):
return encode_module_value(m)
# otherwise it's a method, so actually emit the self
return {"name": m.__name__, "self": preprocess(self)}
@encode.register
def _encode_method(m: types.MethodType):
return {"self": preprocess(m.__self__), "name": m.__name__}
@encode.register
def _encode_method_descriptor(m: types.MethodDescriptorType):
"""
This is actually a method, not a classmethod!
np.ndarray.mean(x)
or
np.arange(10).mean()
both end up being the same call, we can't tell the difference.
"""
# TODO: Remove class, we can grab off of first arg
return {"name": m.__name__, "class": m.__objclass__}
try:
import numpy
except ImportError:
pass
else:
@encode.register
def encode_array(a: numpy.ndarray):
return {"dtype": a.dtype.name}
@encode.register
def encode_dtype(d: numpy.dtype):
return d.name
@encode.register
def encode_ufunc(u: numpy.ufunc):
return u.__name__
@encode.register
def encode_convert_to_ma(u: numpy.ma.core._convert2ma):
return u._func.__name__
MAX_LIST = 10
MAX_TUPLE = 10
MAX_DICT = 10
MAX_STRING = 20
def preprocess(o):
"""
Preserves lists, strings, and None. Tuples and dicts are recursed.
All others primitive subclasses are replaced with their types.
All non primitives subclasses are passed through to be handled by `default`.
"""
tp = type(o)
if tp == str and len(o) <= MAX_STRING:
return o
if tp == list:
return [preprocess(v) for v in o[:MAX_LIST]]
if tp == dict:
return {
"t": "dict",
"v": [
[preprocess(k), preprocess(v)] for k, v in list(o.items())[:MAX_DICT]
],
}
if tp == tuple:
return {
"t": "tuple",
"v": [preprocess(v) for v in o[:MAX_TUPLE]],
}
if isinstance(o, (int, float, bool, list, str, dict, tuple)):
# Don't send literals of these
return {"t": encode_module_value(tp)}
# Other types will be encoded by the `default` function.
return o
def default(o: object) -> object:
"""
JSON encoder that special cases some types
"""
t = encode_module_value(type(o))
v = encode(o)
if v:
return {"t": t, "v": v}
return {"t": t}
# cache this b/c its expesnive
@functools.lru_cache(None, False)
def signature(fn):
return inspect.signature(fn)
def safe_signature(fn):
"""
If fn is hashable, cache it, otherwise dont use cached version
"""
try:
hash(fn)
except Exception:
return inspect.signature(fn)
return signature(fn)
@dataclasses.dataclass
class Bound:
"""
Bound args returns
"""
pos_only: List[Tuple[str, object]] = dataclasses.field(default_factory=list)
pos_or_kw: List[Tuple[str, object]] = dataclasses.field(default_factory=list)
var_pos: Optional[Tuple[str, List[object]]] = None
kw_only: Dict[str, object] = dataclasses.field(default_factory=dict)
var_kw: Optional[Tuple[str, Dict[str, object]]] = None
def as_dict(self):
"""
Turn into dict, ignoring empty values
"""
d = {}
for f in ["pos_only", "pos_or_kw", "var_pos", "kw_only", "var_kw"]:
v = getattr(self, f)
if v:
d[f] = v
return d
@classmethod
def create(cls, fn, args, kwargs) -> Optional[Bound]:
try:
sig = safe_signature(fn)
except ValueError:
return None
try:
bound = sig.bind(*args, **kwargs)
except TypeError:
return None
b = cls()
for k, v in bound.arguments.items():
kind = sig.parameters[k].kind
if kind == inspect.Parameter.POSITIONAL_ONLY:
b.pos_only.append((k, preprocess(v)))
elif kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
b.pos_or_kw.append((k, preprocess(v)))
elif kind == inspect.Parameter.VAR_POSITIONAL:
b.var_pos = k, preprocess(v)
elif kind == inspect.Parameter.KEYWORD_ONLY:
b.kw_only[k] = preprocess(v)
return b
def log_call(
location: str,
fn: Callable,
args: Iterable = (),
kwargs: Mapping[str, Any] = {},
) -> None:
bound = Bound.create(fn, args, kwargs)
line: Dict = {"location": location, "function": preprocess(fn)}
if bound is None:
line["params"] = {}
if args:
line["params"]["args"] = [preprocess(a) for a in args]
if kwargs:
line["params"]["kwargs"] = {k: preprocess(v) for k, v in kwargs.items()}
else:
line["bound_params"] = bound.as_dict()
assert write_line
write_line(line)
@dataclasses.dataclass
class Stack:
tracer: Tracer
frame: Any
NULL: ClassVar[object] = object()
current_i: int = dataclasses.field(init=False, default=0)
opcode: int = dataclasses.field(init=False)
def __post_init__(self):
self.op_stack = get_stack.OpStack(self.frame)
self.opcode = self.frame.f_code.co_code[self.frame.f_lasti]
@property
def oparg(self):
# sort of replicates logic in dis._unpack_opargs but doesn't account for extended
# args, oh well!
return self.frame.f_code.co_code[self.frame.f_lasti + 1]
@property
def opname(self):
return opcode.opname[self.opcode]
@property
def opvalname(self):
"""
We could use `dis.get_instructions` but it's really slow!
We move some of the logic as in `_get_instructions_bytes`
to later, so we can only self.process instructions we care about.
"""
return self.frame.f_code.co_names[self.oparg]
@property
def opvalcompare(self):
"""
We also remove the switches we dont use
"""
return dis.cmp_op[self.oparg]
@property
def TOS(self):
return self.op_stack[-1]
@property
def TOS1(self):
return self.op_stack[-2]
@property
def TOS2(self):
return self.op_stack[-3]
def pop(self):
"""
return the top item on the stack and removes it from the stack
"""
self.current_i += 1
try:
return self.op_stack[-self.current_i]
except ValueError:
# raised when "PyObject is NULL"
return self.NULL
def pop_n(self, n: int) -> List:
"""
removes the top N items from the stack and returns them so the top item is on the left.
"""
l = [self.pop() for _ in range(n)]
# so that top of the stack is on the right
l.reverse()
return l
def process(
self, keyed_args: Tuple, fn: Callable, args: Iterable, kwargs: Mapping = {}
) -> None:
# Note: This take args as an iterable, instead of as a varargs, so that if we don't trace we don't have to expand the iterable
if self.tracer.should_trace(*keyed_args):
filename = self.frame.f_code.co_filename
line = self.frame.f_lineno
# Don't pass kwargs if not used, so we can more easily test mock calls
log_call(f"{filename}:{line}", fn, tuple(args), *((kwargs,) if kwargs else ()))
def __call__(self) -> None:
"""
handle all opcodes from https://docs.python.org/3/library/dis.html
that we care about
"""
opname = self.opname
if opname in UNARY_OPS:
return self.process((self.TOS,), UNARY_OPS[opname], (self.TOS,))
if opname in BINARY_OPS:
return self.process(
(self.TOS, self.TOS1), BINARY_OPS[opname], (self.TOS1, self.TOS)
)
method_name = f"op_{opname}"
if hasattr(self, method_name):
getattr(self, method_name)()
return None
# special case subscr b/c we only check first arg, not both
def op_BINARY_SUBSCR(self):
self.process((self.TOS1,), op.getitem, (self.TOS1, self.TOS))
def op_STORE_SUBSCR(self):
self.process((self.TOS1,), op.setitem, (self.TOS1, self.TOS, self.TOS2))
def op_DELETE_SUBSCR(self):
self.process((self.TOS1,), op.delitem, (self.TOS1, self.TOS))
def op_LOAD_ATTR(self):
self.process((self.TOS,), getattr, (self.TOS, self.opvalname))
def op_STORE_ATTR(self):
self.process((self.TOS,), setattr, (self.TOS, self.opvalname, self.TOS1))
def op_DELETE_ATTR(self):
self.process((self.TOS,), delattr, (self.TOS, self.opvalname))
def op_BUILD_TUPLE_UNPACK(self):
for value in self.pop_n(self.oparg):
self.process((value,), iter, (value,))
def op_BUILD_LIST_UNPACK(self):
self.op_BUILD_TUPLE_UNPACK()
def op_BUILD_SET_UNPACK(self):
self.op_BUILD_TUPLE_UNPACK()
def op_BUILD_TUPLE_UNPACK_WITH_CALL(self):
iterables = []
for _ in range(self.oparg):
arg = self.pop()
if inspect.isgenerator(arg):
return
iterables.append(arg)
self.process((arg,), iter, (arg,))
fn = self.pop()
self.process((fn,), fn, itertools.chain.from_iterable(iterables))
# TODO: BUILD_MAP_UNPACK, BUILD_MAP_UNPACK_WITH_CALL
def op_COMPARE_OP(self):
# list from
# https://github.com/python/cpython/blob/81de3c225774179cdc82a1733a64e4a876ff02b5/Lib/opcode.py#L24-L25
val = self.opvalcompare
COMPARISONS = {
"<": op.lt,
"<=": op.le,
"==": op.eq,
"!=": op.ne,
">": op.gt,
">=": op.ge,
"in": op.contains,
"not in": op.contains,
# Ignore these for now since there are so many of them!
# "is": op.is_,
# "is not": op.is_not,
}
if val in COMPARISONS:
self.process(
(self.TOS, self.TOS1) if "in" not in val else (self.TOS,),
COMPARISONS[val],
# "in" and "not in" are reversed
(self.TOS1, self.TOS,) if "in" not in val else (self.TOS, self.TOS1,),
)
def op_CALL_FUNCTION(self):
args = self.pop_n(self.oparg)
fn = self.pop()
self.process((fn,), fn, args)
def op_CALL_FUNCTION_KW(self):
kwargs_keys = self.pop()
n_kwargs = len(kwargs_keys)
kwarg_values = self.pop_n(n_kwargs)
kwargs = {k: v for k, v in zip(kwargs_keys, kwarg_values)}
args = self.pop_n(self.oparg - n_kwargs)
fn = self.pop()
self.process((fn,), fn, args, kwargs)
def op_CALL_FUNCTION_EX(self):
has_kwarg = self.oparg & int("01", 2)
if has_kwarg:
kwargs = self.pop()
args = self.pop()
fn = self.pop()
else:
kwargs = {}
args = self.pop()
fn = self.pop()
if inspect.isgenerator(args):
return
self.process((fn,), fn, args, kwargs)
def op_CALL_METHOD(self):
args = self.pop_n(self.oparg)
function_or_self = self.pop()
null_or_method = self.pop()
if null_or_method is self.NULL:
function = function_or_self
self.process((function,), function, args)
else:
self_ = function_or_self
method = null_or_method
self.process(
(self_,), method, itertools.chain((self_,), args),
)
UNARY_OPS = {
"UNARY_POSITIVE": op.pos,
"UNARY_NEGATIVE": op.neg,
"UNARY_NOT": op.not_,
"UNARY_INVERT": op.invert,
"GET_ITER": iter,
"GET_YIELD_FROM_ITER": iter,
"UNPACK_SEQUENCE": iter,
"UNPACK_EX": iter,
"FOR_ITER": iter,
}
BINARY_OPS = {
"BINARY_POWER": op.pow,
"BINARY_MULTIPLY": op.mul,
"BINARY_MATRIX_MULTIPLY": op.matmul,
"BINARY_FLOOR_DIVIDE": op.floordiv,
"BINARY_TRUE_DIVIDE": op.truediv,
"BINARY_MODULO": op.mod,
"BINARY_ADD": op.add,
"BINARY_SUBTRACT": op.sub,
"BINARY_LSHIFT": op.lshift,
"BINARY_RSHIFT": op.rshift,
"BINARY_AND": op.and_,
"BINARY_XOR": op.xor,
"BINARY_OR": op.or_,
"INPLACE_POWER": op.ipow,
"INPLACE_MULTIPLY": op.imul,
"INPLACE_MATRIX_MULTIPLY": op.imatmul,
"INPLACE_FLOOR_DIVIDE": op.ifloordiv,
"INPLACE_TRUE_DIVIDE": op.itruediv,
"INPLACE_MODULO": op.imod,
"INPLACE_ADD": op.iadd,
"INPLACE_SUBTRACT": op.isub,
"INPLACE_LSHIFT": op.ilshift,
"INPLACE_RSHIFT": op.irshift,
"INPLACE_AND": op.iand,
"INPLACE_OR": op.ior,
"INPLACE_XOR": op.ixor,
}
@dataclasses.dataclass
class Tracer:
# the modules we should trace calls to
calls_to_modules: List[str]
# the modules we should trace calls from
calls_from_modules: List[str]
def __enter__(self):
sys.settrace(self)
def __exit__(self, exc_type, exc_val, exc_tb):
sys.settrace(None)
def should_trace(self, *values) -> bool:
for value in values:
if isinstance(value, types.BuiltinMethodType):
# if this was a method defined in C, use the instance as the value
value = value.__self__
module = getmodulename(value) or getmodulename(type(value))
if not module:
warnings.warn(f"Cannot get module of {value}")
continue
if any(module.startswith(mod) for mod in self.calls_to_modules):
return True
return False
def __call__(self, frame, event, arg) -> Optional[Tracer]:
if event == "call":
frame.f_trace_opcodes = True
return self
elif event != "opcode":
return None
if self.should_trace_frame(frame):
Stack(self, frame)()
return None
def should_trace_frame(self, frame) -> bool:
# Ignore frames which are not from the `calls_from_module`
try:
frame_module_name = frame.f_globals["__name__"]
except KeyError:
return False
return frame_module_name == "__main__" or any(
frame_module_name == mod or frame_module_name.startswith(mod + ".")
for mod in self.calls_from_modules
)
def setup():
global write_line, context_manager
FILE_NAME = os.environ["PYTHON_RECORD_API_OUTPUT_FILE"]
context_manager = jsonl.write(FILE_NAME, default=default)
write_line = context_manager.__enter__()
def finalize():
assert context_manager
context_manager.__exit__(*sys.exc_info())