Skip to content

Commit 60b9e04

Browse files
bpo-45355: Use sizeof(_Py_CODEUNIT) instead of literal 2 for the size of the code unit (GH-28711)
1 parent 4f6e068 commit 60b9e04

5 files changed

Lines changed: 15 additions & 15 deletions

File tree

Modules/_tracemalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ static void
302302
tracemalloc_get_frame(InterpreterFrame *pyframe, frame_t *frame)
303303
{
304304
frame->filename = unknown_filename;
305-
int lineno = PyCode_Addr2Line(pyframe->f_code, pyframe->f_lasti*2);
305+
int lineno = PyCode_Addr2Line(pyframe->f_code, pyframe->f_lasti*sizeof(_Py_CODEUNIT));
306306
if (lineno < 0) {
307307
lineno = 0;
308308
}

Objects/frameobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ PyFrame_GetLineNumber(PyFrameObject *f)
4545
return f->f_lineno;
4646
}
4747
else {
48-
return PyCode_Addr2Line(f->f_frame->f_code, f->f_frame->f_lasti*2);
48+
return PyCode_Addr2Line(f->f_frame->f_code, f->f_frame->f_lasti*sizeof(_Py_CODEUNIT));
4949
}
5050
}
5151

@@ -67,7 +67,7 @@ frame_getlasti(PyFrameObject *f, void *closure)
6767
if (f->f_frame->f_lasti < 0) {
6868
return PyLong_FromLong(-1);
6969
}
70-
return PyLong_FromLong(f->f_frame->f_lasti*2);
70+
return PyLong_FromLong(f->f_frame->f_lasti*sizeof(_Py_CODEUNIT));
7171
}
7272

7373
static PyObject *

Objects/genobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ compute_cr_origin(int origin_depth)
12841284
PyCodeObject *code = frame->f_code;
12851285
PyObject *frameinfo = Py_BuildValue("OiO",
12861286
code->co_filename,
1287-
PyCode_Addr2Line(frame->f_code, frame->f_lasti*2),
1287+
PyCode_Addr2Line(frame->f_code, frame->f_lasti*sizeof(_Py_CODEUNIT)),
12881288
code->co_name);
12891289
if (!frameinfo) {
12901290
Py_DECREF(cr_origin);

Python/ceval.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4815,7 +4815,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
48154815
#endif
48164816
fprintf(stderr,
48174817
"XXX lineno: %d, opcode: %d\n",
4818-
PyCode_Addr2Line(frame->f_code, frame->f_lasti*2),
4818+
PyCode_Addr2Line(frame->f_code, frame->f_lasti*sizeof(_Py_CODEUNIT)),
48194819
opcode);
48204820
_PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
48214821
goto error;
@@ -5996,7 +5996,7 @@ call_trace(Py_tracefunc func, PyObject *obj,
59965996
}
59975997
else {
59985998
initialize_trace_info(&tstate->trace_info, frame);
5999-
f->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*2, &tstate->trace_info.bounds);
5999+
f->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*sizeof(_Py_CODEUNIT), &tstate->trace_info.bounds);
60006000
}
60016001
result = func(obj, f, what, arg);
60026002
f->f_lineno = 0;
@@ -6035,8 +6035,8 @@ maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
60356035
then call the trace function if we're tracing source lines.
60366036
*/
60376037
initialize_trace_info(&tstate->trace_info, frame);
6038-
int lastline = _PyCode_CheckLineNumber(instr_prev*2, &tstate->trace_info.bounds);
6039-
int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &tstate->trace_info.bounds);
6038+
int lastline = _PyCode_CheckLineNumber(instr_prev*sizeof(_Py_CODEUNIT), &tstate->trace_info.bounds);
6039+
int line = _PyCode_CheckLineNumber(frame->f_lasti*sizeof(_Py_CODEUNIT), &tstate->trace_info.bounds);
60406040
PyFrameObject *f = _PyFrame_GetFrameObject(frame);
60416041
if (f == NULL) {
60426042
return -1;
@@ -6978,7 +6978,7 @@ dtrace_function_entry(InterpreterFrame *frame)
69786978
PyCodeObject *code = frame->f_code;
69796979
filename = PyUnicode_AsUTF8(code->co_filename);
69806980
funcname = PyUnicode_AsUTF8(code->co_name);
6981-
lineno = PyCode_Addr2Line(frame->f_code, frame->f_lasti*2);
6981+
lineno = PyCode_Addr2Line(frame->f_code, frame->f_lasti*sizeof(_Py_CODEUNIT));
69826982

69836983
PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
69846984
}
@@ -6993,7 +6993,7 @@ dtrace_function_return(InterpreterFrame *frame)
69936993
PyCodeObject *code = frame->f_code;
69946994
filename = PyUnicode_AsUTF8(code->co_filename);
69956995
funcname = PyUnicode_AsUTF8(code->co_name);
6996-
lineno = PyCode_Addr2Line(frame->f_code, frame->f_lasti*2);
6996+
lineno = PyCode_Addr2Line(frame->f_code, frame->f_lasti*sizeof(_Py_CODEUNIT));
69976997

69986998
PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
69996999
}
@@ -7010,12 +7010,12 @@ maybe_dtrace_line(InterpreterFrame *frame,
70107010
instruction window, reset the window.
70117011
*/
70127012
initialize_trace_info(trace_info, frame);
7013-
int lastline = _PyCode_CheckLineNumber(instr_prev*2, &trace_info->bounds);
7014-
int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
7013+
int lastline = _PyCode_CheckLineNumber(instr_prev*sizeof(_Py_CODEUNIT), &trace_info->bounds);
7014+
int line = _PyCode_CheckLineNumber(frame->f_lasti*sizeof(_Py_CODEUNIT), &trace_info->bounds);
70157015
if (line != -1) {
70167016
/* Trace backward edges or first instruction of a new line */
70177017
if (frame->f_lasti < instr_prev ||
7018-
(line != lastline && frame->f_lasti*2 == trace_info->bounds.ar_start))
7018+
(line != lastline && frame->f_lasti*sizeof(_Py_CODEUNIT) == trace_info->bounds.ar_start))
70197019
{
70207020
co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
70217021
if (!co_filename) {

Python/traceback.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ _PyTraceBack_FromFrame(PyObject *tb_next, PyFrameObject *frame)
240240
assert(tb_next == NULL || PyTraceBack_Check(tb_next));
241241
assert(frame != NULL);
242242

243-
return tb_create_raw((PyTracebackObject *)tb_next, frame, frame->f_frame->f_lasti*2,
243+
return tb_create_raw((PyTracebackObject *)tb_next, frame, frame->f_frame->f_lasti*sizeof(_Py_CODEUNIT),
244244
PyFrame_GetLineNumber(frame));
245245
}
246246

@@ -1047,7 +1047,7 @@ dump_frame(int fd, InterpreterFrame *frame)
10471047
PUTS(fd, "???");
10481048
}
10491049

1050-
int lineno = PyCode_Addr2Line(code, frame->f_lasti*2);
1050+
int lineno = PyCode_Addr2Line(code, frame->f_lasti*sizeof(_Py_CODEUNIT));
10511051
PUTS(fd, ", line ");
10521052
if (lineno >= 0) {
10531053
_Py_DumpDecimal(fd, (size_t)lineno);

0 commit comments

Comments
 (0)