Skip to content

Commit 252b7bc

Browse files
bpo-45355: More use of sizeof(_Py_CODEUNIT) (GH-28720)
1 parent 9be930f commit 252b7bc

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

Objects/codeobject.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -656,15 +656,13 @@ _PyCode_Addr2Offset(PyCodeObject* co, int addrq)
656656
if (co->co_columntable == Py_None || addrq < 0) {
657657
return -1;
658658
}
659-
if (addrq % 2 == 1) {
660-
--addrq;
661-
}
662-
if (addrq >= PyBytes_GET_SIZE(co->co_columntable)) {
659+
addrq /= sizeof(_Py_CODEUNIT);
660+
if (addrq*2 >= PyBytes_GET_SIZE(co->co_columntable)) {
663661
return -1;
664662
}
665663

666664
unsigned char* bytes = (unsigned char*)PyBytes_AS_STRING(co->co_columntable);
667-
return bytes[addrq] - 1;
665+
return bytes[addrq*2] - 1;
668666
}
669667

670668
int
@@ -673,15 +671,13 @@ _PyCode_Addr2EndOffset(PyCodeObject* co, int addrq)
673671
if (co->co_columntable == Py_None || addrq < 0) {
674672
return -1;
675673
}
676-
if (addrq % 2 == 0) {
677-
++addrq;
678-
}
679-
if (addrq >= PyBytes_GET_SIZE(co->co_columntable)) {
674+
addrq /= sizeof(_Py_CODEUNIT);
675+
if (addrq*2+1 >= PyBytes_GET_SIZE(co->co_columntable)) {
680676
return -1;
681677
}
682678

683679
unsigned char* bytes = (unsigned char*)PyBytes_AS_STRING(co->co_columntable);
684-
return bytes[addrq] - 1;
680+
return bytes[addrq*2+1] - 1;
685681
}
686682

687683
void

Objects/frameobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ marklines(PyCodeObject *code, int len)
373373
}
374374

375375
while (PyLineTable_NextAddressRange(&bounds)) {
376-
assert(bounds.ar_start/2 < len);
377-
linestarts[bounds.ar_start/2] = bounds.ar_line;
376+
assert(bounds.ar_start/(int)sizeof(_Py_CODEUNIT) < len);
377+
linestarts[bounds.ar_start/sizeof(_Py_CODEUNIT)] = bounds.ar_line;
378378
}
379379
return linestarts;
380380
}

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7081,7 +7081,7 @@ assemble_line_range(struct assembler* a, int current, PyObject** table,
70817081
int* prev, int* start, int* offset)
70827082
{
70837083
int ldelta, bdelta;
7084-
bdelta = (a->a_offset - *start) * 2;
7084+
bdelta = (a->a_offset - *start) * sizeof(_Py_CODEUNIT);
70857085
if (bdelta == 0) {
70867086
return 1;
70877087
}

0 commit comments

Comments
 (0)