summaryrefslogtreecommitdiffstats
path: root/Objects/frameobject.c
diff options
context:
space:
mode:
authorTian Gao <gaogaotiantian@hotmail.com>2024-04-29 08:54:52 (GMT)
committerGitHub <noreply@github.com>2024-04-29 08:54:52 (GMT)
commit375c94c75dd9eaefaddd89a7f704a031441af286 (patch)
treed0de838e2aac579a125283531a4042bf178c2bfd /Objects/frameobject.c
parentc7e7bfc4ca26bf90e0d4959e303770fbfc3a3795 (diff)
downloadcpython-375c94c75dd9eaefaddd89a7f704a031441af286.zip
cpython-375c94c75dd9eaefaddd89a7f704a031441af286.tar.gz
cpython-375c94c75dd9eaefaddd89a7f704a031441af286.tar.bz2
gh-107674: Lazy load line number to improve performance of tracing (GH-118127)
Diffstat (limited to 'Objects/frameobject.c')
-rw-r--r--Objects/frameobject.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index 07b7ef3..36538b1 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -41,12 +41,20 @@ int
PyFrame_GetLineNumber(PyFrameObject *f)
{
assert(f != NULL);
- if (f->f_lineno != 0) {
- return f->f_lineno;
+ if (f->f_lineno == -1) {
+ // We should calculate it once. If we can't get the line number,
+ // set f->f_lineno to 0.
+ f->f_lineno = PyUnstable_InterpreterFrame_GetLine(f->f_frame);
+ if (f->f_lineno < 0) {
+ f->f_lineno = 0;
+ return -1;
+ }
}
- else {
- return PyUnstable_InterpreterFrame_GetLine(f->f_frame);
+
+ if (f->f_lineno > 0) {
+ return f->f_lineno;
}
+ return PyUnstable_InterpreterFrame_GetLine(f->f_frame);
}
static PyObject *