bpo-17799: Explain real behaviour of sys.settrace and sys.setprofile#4056
bpo-17799: Explain real behaviour of sys.settrace and sys.setprofile#4056zhangyangyu merged 14 commits intopython:masterfrom
Conversation
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @zhangyangyu: please review the changes made to this pull request. |
| | :const:`PyTrace_C_RETURN` | Function object being called. | | ||
| +------------------------------+--------------------------------------+ | ||
|
|
||
| Any trace function registered using :c:func:`PyEval_SetTrace` will not recieve |
There was a problem hiding this comment.
Thanks! Corrected in 18b88ec
| Any trace function registered using :c:func:`PyEval_SetTrace` will not receive | ||
| :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION`, :const:`PyTrace_C_RETURN` | ||
| or :const:`PyTrace_LINE` as a value for the *what* parameter. | ||
|
|
There was a problem hiding this comment.
Why line event is also here?
There was a problem hiding this comment.
Sorry, I misread that part.
| Set the tracing function to *func*. This is similar to | ||
| :c:func:`PyEval_SetProfile`, except the tracing function does receive line-number | ||
| events. | ||
| events or any event related to C function objects being called. |
There was a problem hiding this comment.
It is does receive, so I think it is wrong here. Maybe just declare it does not receive PyTrace_C_CALL/RETURN/EXCEPTION here, so don't have to write the previous added block.
|
|
||
| Profile functions should have three arguments: *frame*, *event*, and | ||
| *arg*. *frame* is the current stack frame. *event* is a string: ``'call'``, | ||
| ``'line'``, ``'return'``, ``'exception'``, ``'c_call'``, ``'c_return'``, or |
There was a problem hiding this comment.
line will be received here?
|
|
||
| ``'call'`` | ||
| A function is called (or some other code block entered). The | ||
| global trace function is called; *arg* is ``None``; the return value |
There was a problem hiding this comment.
Although the previous doc says profile function is called similiarly to trace function, but from the implementation, there is no concept like global trace function or local trace function. And at least we call it profile function here not trace function.
|
@zhangyangyu Thank you very much for the comments. I have addressed your feedback in a9f381f and 3887c6c. |
| profile function is called; *arg* is ``None``. | ||
|
|
||
| ``'return'`` | ||
| A function (or other code block) is about to return. The local trace |
There was a problem hiding this comment.
Also here and the following return, exception part about the global/local and name.
| ``'c_exception'`` | ||
| A C function has raised an exception. *arg* is the C function object. | ||
|
|
||
| ``'opcode'`` |
There was a problem hiding this comment.
opcode events won't be triggered for profile functions, also note the above list needs also be modified.
| Any trace function registered using :c:func:`PyEval_SetTrace` will not receive | ||
| :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION` or :const:`PyTrace_C_RETURN` | ||
| as a value for the *what* parameter. | ||
|
|
There was a problem hiding this comment.
I prefer to combine this section with the specified PyEval_SetTrace part.
|
@zhangyangyu Upon inspection and testing I think that profile functions cannot receive "exception" events as well. Can you confirm this? |
|
Yes. But profile functions receive even PyTrace_C_EXCEPTION, why not Python exception events? |
|
From the unitest test_sys_setprofile, exception events should never be received by profile functions, so seems it's by design. |
…not being received.
|
PyEval_SetProfile should also be updated that not only line number events but also Python exception events. |
|
I think the following paragraph need some modification, something like "... but it is called with different events, for example ..."
And your NEWS entry needs updated too. It's not only about C events now. BTW, we need a new issue to complete the C part about PyTrace_OPCODE. |
|
Thanks @pablogsal for the PR, and @zhangyangyu for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6. |
|
@pablogsal I merged it! Nice work! |
|
Sorry, @pablogsal and @zhangyangyu, I could not cleanly backport this to |
|
Sorry, @pablogsal and @zhangyangyu, I could not cleanly backport this to |
…ofile (pythonGH-4056). (cherry picked from commit 131fd7f)
|
GH-5298 is a backport of this pull request to the 3.6 branch. |
…ofile (pythonGH-4056). (cherry picked from commit 131fd7f)
|
GH-5299 is a backport of this pull request to the 2.7 branch. |
The docs for
sys.settracemention that:But in the code for
ceval.cthe only point wherecall_traceis invoked withPyTrace_C_CALLorPyTrace_C_RETURNis under theC_TRACEmacro. In this macro this line prevents any function set up usingsys.settraceto callcall_tracewith the mentioned arguments:Notice that from the code of
PyEval_SetTraceandPyEval_SetProfile, only the later setststate->c_profilefuncand therefore only functions installed usingsys.setprofilewill recieve ac_callfor the event:This PR updates the documentation clarifying the real behaviour of both functions.
https://bugs.python.org/issue17799