This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: TypeError when f_trace is None and tracing.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Winterflower, belopolsky, georg.brandl, python-dev, rmast, serhiy.storchaka, xdegaye
Priority: normal Keywords: patch

Created on 2013-12-21 14:01 by xdegaye, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
tracer.py xdegaye, 2013-12-21 14:01
f_trace.patch xdegaye, 2013-12-21 14:01 review
tests.patch xdegaye, 2013-12-24 15:15 review
none_f_trace.patch xdegaye, 2016-06-04 16:32 review
Messages (8)
msg206735 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2013-12-21 14:01
Section 3.2 of 'The Python Language Reference' states:
  f_trace, if not None, is a function called at the start of each source code line

Run the attached tracer.py and see that although f_trace is None in both cases when 'cmd' is either
'delete' or 'set', the second case raises a TypeError exception:
$ python tracer.py
f_trace: None
delete start
delete done
f_trace: None
set start
Traceback (most recent call last):
  File "tracer.py", line 19, in <module>
    foo('set')
  File "tracer.py", line 15, in foo
    print(cmd, 'done')
  File "tracer.py", line 15, in foo
    print(cmd, 'done')
TypeError: 'NoneType' object is not callable

Also, the frame.f_lineno may be wrong in a traceback when f_trace is set to None because
PyFrame_GetLineNumber() does not handle this case.

The attached patch fixes this issue.
The patch also fixes issue 11992 and issue 20040.
The patch also fixes the dispatch_call() method of Bdb in the bdb module when the frame is a
generator and the previous command is next, until or return.
The patch also provides a backward compatible solution to the performance enhancement described in
issue 16672.
msg206896 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2013-12-24 15:15
Adding the corresponding tests.
msg267094 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-03 12:25
I think the code would be simpler if convert the argument of frame_settrace() to NULL if it is Py_None.
msg267157 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2016-06-03 20:15
On a 'call' trace event, the bdb dispatch_call() function returns None when there is nothing to trace in this function (no breakpoints). With the changes made by the patch in ceval.c, the costly maybe_call_line_trace() function is not called on each line in this case since f->f_trace is Py_None. This provides the performance enhancements described in issue 16672 without breaking the _hotshot extension module or other extension modules using PyEval_SetTrace().

I agree that the code would be much simpler when f->f_trace is set to NULL by the frame_settrace() setter or trace_trampoline() when it is Py_None. The performance gain described above by using Py_None may not be worth the complexity.

Thanks for looking into this Serhiy.
msg267158 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-03 20:25
This may be worthwhile optimization, but this is different issue. Let first fix a TypeError, and then open new issue for the optimization.
msg267265 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2016-06-04 16:32
This patch fixes the TypeError.
New issue 27218: improve tracing performance with f_trace set to Py_None.
msg267281 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-04 17:20
LGTM.
msg267288 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-06-04 17:42
New changeset 4d916be61d46 by Serhiy Storchaka in branch '2.7':
Issue #20041: Fixed TypeError when frame.f_trace is set to None.
https://hg.python.org/cpython/rev/4d916be61d46

New changeset 74ad78d2dd8d by Serhiy Storchaka in branch '3.5':
Issue #20041: Fixed TypeError when frame.f_trace is set to None.
https://hg.python.org/cpython/rev/74ad78d2dd8d

New changeset f993dbeb2ad2 by Serhiy Storchaka in branch 'default':
Issue #20041: Fixed TypeError when frame.f_trace is set to None.
https://hg.python.org/cpython/rev/f993dbeb2ad2
History
Date User Action Args
2022-04-11 14:57:55adminsetgithub: 64240
2021-08-18 06:37:50rmastsetnosy: + rmast
2016-06-04 17:42:47python-devsetnosy: + python-dev
messages: + msg267288
2016-06-04 17:34:11serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: commit review -> resolved
2016-06-04 17:20:20serhiy.storchakasetassignee: serhiy.storchaka
stage: patch review -> commit review
messages: + msg267281
versions: + Python 3.5, Python 3.6, - Python 3.3, Python 3.4
2016-06-04 16:32:57xdegayesetfiles: + none_f_trace.patch

messages: + msg267265
2016-06-03 20:25:55serhiy.storchakasetmessages: + msg267158
2016-06-03 20:15:45xdegayesetmessages: + msg267157
2016-06-03 12:25:17serhiy.storchakasetmessages: + msg267094
2016-05-18 21:02:29Winterflowersetnosy: + Winterflower
2013-12-24 15:18:24xdegayesetcomponents: + Interpreter Core, - 2to3 (2.x to 3.x conversion tool)
2013-12-24 15:15:16xdegayesetfiles: + tests.patch

messages: + msg206896
components: + 2to3 (2.x to 3.x conversion tool), - Interpreter Core
2013-12-22 01:10:50pitrousetnosy: + georg.brandl, belopolsky, serhiy.storchaka
stage: patch review

versions: + Python 3.3
2013-12-21 14:01:44xdegayesetfiles: + f_trace.patch
keywords: + patch
2013-12-21 14:01:27xdegayecreate