mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 19:34:08 +00:00 
			
		
		
		
	bpo-17799: Explain real behaviour of sys.settrace and sys.setprofile (#4056)
This commit is contained in:
		
							parent
							
								
									018e1b7aad
								
							
						
					
					
						commit
						131fd7f96c
					
				
					 3 changed files with 40 additions and 22 deletions
				
			
		| 
						 | 
					@ -1300,7 +1300,6 @@ Python-level trace functions in previous versions.
 | 
				
			||||||
   | :const:`PyTrace_C_RETURN`    | Function object being called.        |
 | 
					   | :const:`PyTrace_C_RETURN`    | Function object being called.        |
 | 
				
			||||||
   +------------------------------+--------------------------------------+
 | 
					   +------------------------------+--------------------------------------+
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
.. c:var:: int PyTrace_CALL
 | 
					.. c:var:: int PyTrace_CALL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   The value of the *what* parameter to a :c:type:`Py_tracefunc` function when a new
 | 
					   The value of the *what* parameter to a :c:type:`Py_tracefunc` function when a new
 | 
				
			||||||
| 
						 | 
					@ -1357,16 +1356,18 @@ Python-level trace functions in previous versions.
 | 
				
			||||||
   function as its first parameter, and may be any Python object, or *NULL*.  If
 | 
					   function as its first parameter, and may be any Python object, or *NULL*.  If
 | 
				
			||||||
   the profile function needs to maintain state, using a different value for *obj*
 | 
					   the profile function needs to maintain state, using a different value for *obj*
 | 
				
			||||||
   for each thread provides a convenient and thread-safe place to store it.  The
 | 
					   for each thread provides a convenient and thread-safe place to store it.  The
 | 
				
			||||||
   profile function is called for all monitored events except the line-number
 | 
					   profile function is called for all monitored events except :const:`PyTrace_LINE`
 | 
				
			||||||
   events.
 | 
					   and :const:`PyTrace_EXCEPTION`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. c:function:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
 | 
					.. c:function:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   Set the tracing function to *func*.  This is similar to
 | 
					   Set the tracing function to *func*.  This is similar to
 | 
				
			||||||
   :c:func:`PyEval_SetProfile`, except the tracing function does receive line-number
 | 
					   :c:func:`PyEval_SetProfile`, except the tracing function does receive line-number
 | 
				
			||||||
   events.
 | 
					   events and does not receive any event related to C function objects being called. 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.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. _advanced-debugging:
 | 
					.. _advanced-debugging:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1084,13 +1084,38 @@ always available.
 | 
				
			||||||
   Set the system's profile function, which allows you to implement a Python source
 | 
					   Set the system's profile function, which allows you to implement a Python source
 | 
				
			||||||
   code profiler in Python.  See chapter :ref:`profile` for more information on the
 | 
					   code profiler in Python.  See chapter :ref:`profile` for more information on the
 | 
				
			||||||
   Python profiler.  The system's profile function is called similarly to the
 | 
					   Python profiler.  The system's profile function is called similarly to the
 | 
				
			||||||
   system's trace function (see :func:`settrace`), but it isn't called for each
 | 
					   system's trace function (see :func:`settrace`), but it is called with different events,
 | 
				
			||||||
   executed line of code (only on call and return, but the return event is reported
 | 
					   for example it isn't called for each executed line of code (only on call and return,
 | 
				
			||||||
   even when an exception has been set).  The function is thread-specific, but
 | 
					   but the return event is reported even when an exception has been set). The function is
 | 
				
			||||||
   there is no way for the profiler to know about context switches between threads,
 | 
					   thread-specific, but there is no way for the profiler to know about context switches between
 | 
				
			||||||
   so it does not make sense to use this in the presence of multiple threads. Also,
 | 
					   threads, so it does not make sense to use this in the presence of multiple threads. Also,
 | 
				
			||||||
   its return value is not used, so it can simply return ``None``.
 | 
					   its return value is not used, so it can simply return ``None``.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   Profile functions should have three arguments: *frame*, *event*, and
 | 
				
			||||||
 | 
					   *arg*. *frame* is the current stack frame.  *event* is a string: ``'call'``,
 | 
				
			||||||
 | 
					   ``'return'``, ``'c_call'``, ``'c_return'``, or ``'c_exception'``. *arg* depends
 | 
				
			||||||
 | 
					   on the event type.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   The events have the following meaning:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   ``'call'``
 | 
				
			||||||
 | 
					      A function is called (or some other code block entered).  The
 | 
				
			||||||
 | 
					      profile function is called; *arg* is ``None``.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   ``'return'``
 | 
				
			||||||
 | 
					      A function (or other code block) is about to return.  The profile
 | 
				
			||||||
 | 
					      function is called; *arg* is the value that will be returned, or ``None``
 | 
				
			||||||
 | 
					      if the event is caused by an exception being raised.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   ``'c_call'``
 | 
				
			||||||
 | 
					      A C function is about to be called.  This may be an extension function or
 | 
				
			||||||
 | 
					      a built-in.  *arg* is the C function object.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   ``'c_return'``
 | 
				
			||||||
 | 
					      A C function has returned. *arg* is the C function object.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   ``'c_exception'``
 | 
				
			||||||
 | 
					      A C function has raised an exception.  *arg* is the C function object.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. function:: setrecursionlimit(limit)
 | 
					.. function:: setrecursionlimit(limit)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1137,8 +1162,8 @@ always available.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   Trace functions should have three arguments: *frame*, *event*, and
 | 
					   Trace functions should have three arguments: *frame*, *event*, and
 | 
				
			||||||
   *arg*. *frame* is the current stack frame.  *event* is a string: ``'call'``,
 | 
					   *arg*. *frame* is the current stack frame.  *event* is a string: ``'call'``,
 | 
				
			||||||
   ``'line'``, ``'return'``, ``'exception'``, ``'c_call'``, ``'c_return'``, or
 | 
					   ``'line'``, ``'return'``, ``'exception'`` or ``'opcode'``.  *arg* depends on
 | 
				
			||||||
   ``'c_exception'``, ``'opcode'``. *arg* depends on the event type.
 | 
					   the event type.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   The trace function is invoked (with *event* set to ``'call'``) whenever a new
 | 
					   The trace function is invoked (with *event* set to ``'call'``) whenever a new
 | 
				
			||||||
   local scope is entered; it should return a reference to a local trace
 | 
					   local scope is entered; it should return a reference to a local trace
 | 
				
			||||||
| 
						 | 
					@ -1175,16 +1200,6 @@ always available.
 | 
				
			||||||
      tuple ``(exception, value, traceback)``; the return value specifies the
 | 
					      tuple ``(exception, value, traceback)``; the return value specifies the
 | 
				
			||||||
      new local trace function.
 | 
					      new local trace function.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   ``'c_call'``
 | 
					 | 
				
			||||||
      A C function is about to be called.  This may be an extension function or
 | 
					 | 
				
			||||||
      a built-in.  *arg* is the C function object.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
   ``'c_return'``
 | 
					 | 
				
			||||||
      A C function has returned. *arg* is the C function object.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
   ``'c_exception'``
 | 
					 | 
				
			||||||
      A C function has raised an exception.  *arg* is the C function object.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
   ``'opcode'``
 | 
					   ``'opcode'``
 | 
				
			||||||
      The interpreter is about to execute a new opcode (see :mod:`dis` for
 | 
					      The interpreter is about to execute a new opcode (see :mod:`dis` for
 | 
				
			||||||
      opcode details).  The local trace function is called; *arg* is
 | 
					      opcode details).  The local trace function is called; *arg* is
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					Explain real behaviour of sys.settrace and sys.setprofile and their C-API counterparts
 | 
				
			||||||
 | 
					regarding which type of events are received in each function. Patch by Pablo Galindo Salgado.
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue