Issue #21863: cProfile now displays the module name of C extension functions, in addition to their own name.

This commit is contained in:
Antoine Pitrou 2014-06-27 23:49:29 -04:00
parent 0882e27e2a
commit 8477f7af13
3 changed files with 17 additions and 7 deletions

View file

@ -11,7 +11,7 @@ from test.profilee import testfunc
class CProfileTest(ProfileTest): class CProfileTest(ProfileTest):
profilerclass = cProfile.Profile profilerclass = cProfile.Profile
profilermodule = cProfile profilermodule = cProfile
expected_max_output = "{built-in method max}" expected_max_output = "{built-in method builtins.max}"
def get_expected_output(self): def get_expected_output(self):
return _ProfileOutput return _ProfileOutput
@ -72,9 +72,9 @@ profilee.py:84(helper2_indirect) <- 2 0.000 0.140
profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper) profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper)
2 0.078 0.100 profilee.py:84(helper2_indirect) 2 0.078 0.100 profilee.py:84(helper2_indirect)
profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2) profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2)
{built-in method exc_info} <- 4 0.000 0.000 profilee.py:73(helper1) {built-in method builtins.hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
{built-in method hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
8 0.000 0.008 profilee.py:88(helper2) 8 0.000 0.008 profilee.py:88(helper2)
{built-in method sys.exc_info} <- 4 0.000 0.000 profilee.py:73(helper1)
{method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)""" {method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)"""
_ProfileOutput['print_callees'] = """\ _ProfileOutput['print_callees'] = """\
<string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc) <string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc)
@ -87,12 +87,12 @@ profilee.py:48(mul) ->
profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1) profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1)
2 0.000 0.140 profilee.py:84(helper2_indirect) 2 0.000 0.140 profilee.py:84(helper2_indirect)
6 0.234 0.300 profilee.py:88(helper2) 6 0.234 0.300 profilee.py:88(helper2)
profilee.py:73(helper1) -> 4 0.000 0.000 {built-in method exc_info} profilee.py:73(helper1) -> 4 0.000 0.004 {built-in method builtins.hasattr}
profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial) profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial)
2 0.078 0.100 profilee.py:88(helper2) 2 0.078 0.100 profilee.py:88(helper2)
profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper) profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper)
profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__) profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__)
{built-in method hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)""" {built-in method builtins.hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)"""
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -103,6 +103,9 @@ Core and Builtins
Library Library
------- -------
- Issue #21863: cProfile now displays the module name of C extension functions,
in addition to their own name.
- Issue #11453: asyncore: emit a ResourceWarning when an unclosed file_wrapper - Issue #11453: asyncore: emit a ResourceWarning when an unclosed file_wrapper
object is destroyed. The destructor now closes the file if needed. The object is destroyed. The destructor now closes the file if needed. The
close() method can now be called twice: the second call does nothing. close() method can now be called twice: the second call does nothing.

View file

@ -202,6 +202,8 @@ normalizeUserObj(PyObject *obj)
*/ */
PyObject *self = fn->m_self; PyObject *self = fn->m_self;
PyObject *name = PyUnicode_FromString(fn->m_ml->ml_name); PyObject *name = PyUnicode_FromString(fn->m_ml->ml_name);
PyObject *modname = fn->m_module;
if (name != NULL) { if (name != NULL) {
PyObject *mo = _PyType_Lookup(Py_TYPE(self), name); PyObject *mo = _PyType_Lookup(Py_TYPE(self), name);
Py_XINCREF(mo); Py_XINCREF(mo);
@ -213,7 +215,12 @@ normalizeUserObj(PyObject *obj)
return res; return res;
} }
} }
/* Otherwise, use __module__ */
PyErr_Clear(); PyErr_Clear();
if (modname != NULL && PyUnicode_Check(modname))
return PyUnicode_FromFormat("<built-in method %S.%s>",
modname, fn->m_ml->ml_name);
else
return PyUnicode_FromFormat("<built-in method %s>", return PyUnicode_FromFormat("<built-in method %s>",
fn->m_ml->ml_name); fn->m_ml->ml_name);
} }