Skip to content

Commit

Permalink
Debugging code to log PyObject_GetAttr and python method calls (pytho…
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan authored May 28, 2020
1 parent e80585a commit c48624a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,35 @@ static void CPy_TypeError(const char *expected, PyObject *value) {
}
}


#ifdef MYPYC_LOG_GETATTR
static void CPy_LogGetAttr(const char *method, PyObject *obj, PyObject *attr) {
PyObject *module = PyImport_ImportModule("getattr_hook");
if (module) {
PyObject *res = PyObject_CallMethod(module, method, "OO", obj, attr);
Py_XDECREF(res);
Py_DECREF(module);
}
PyErr_Clear();
}
#else
#define CPy_LogGetAttr(method, obj, attr) (void)0
#endif

// Intercept a method call and log it. This needs to be a macro
// because there is no API that accepts va_args for making a
// call. Worse, it needs to use the comma operator to return the right
// value.
#define CPyObject_CallMethodObjArgs(obj, attr, ...) \
(CPy_LogGetAttr("log_method", (obj), (attr)), \
PyObject_CallMethodObjArgs((obj), (attr), __VA_ARGS__))

// This one is a macro for consistency with the above, I guess.
#define CPyObject_GetAttr(obj, attr) \
(CPy_LogGetAttr("log", (obj), (attr)), \
PyObject_GetAttr((obj), (attr)))


// These functions are basically exactly PyCode_NewEmpty and
// _PyTraceback_Add which are available in all the versions we support.
// We're continuing to use them because we'll probably optimize them later.
Expand Down
4 changes: 2 additions & 2 deletions mypyc/primitives/generic_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
arg_types=[object_rprimitive, object_rprimitive],
result_type=object_rprimitive,
error_kind=ERR_MAGIC,
emit=call_emit('PyObject_GetAttr')
emit=call_emit('CPyObject_GetAttr')
)

# getattr(obj, attr, default)
Expand Down Expand Up @@ -224,7 +224,7 @@
is_var_arg=True,
error_kind=ERR_MAGIC,
format_str='{dest} = py_method_call({comma_args})',
emit=simple_emit('{dest} = PyObject_CallMethodObjArgs({comma_args}, NULL);'))
emit=simple_emit('{dest} = CPyObject_CallMethodObjArgs({comma_args}, NULL);'))

# len(obj)
func_op(name='builtins.len',
Expand Down

0 comments on commit c48624a

Please sign in to comment.