-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
bpo-17799: Explain real behaviour of sys.settrace and sys.setprofile #4056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
zhangyangyu
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The C API part https://docs.python.org/3.7/c-api/init.html?highlight=pyeval_settrace#profiling-and-tracing also needs editing.
|
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. |
Doc/c-api/init.rst
Outdated
| | :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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/s/recieve/receive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Corrected in 18b88ec
Doc/c-api/init.rst
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why line event is also here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I misread that part.
Doc/c-api/init.rst
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Doc/library/sys.rst
Outdated
|
|
||
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line will be received here?
Doc/library/sys.rst
Outdated
|
|
||
| ``'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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
Doc/library/sys.rst
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here and the following return, exception part about the global/local and name.
Doc/library/sys.rst
Outdated
| ``'c_exception'`` | ||
| A C function has raised an exception. *arg* is the C function object. | ||
|
|
||
| ``'opcode'`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opcode events won't be triggered for profile functions, also note the above list needs also be modified.
Doc/c-api/init.rst
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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