8000 Remove redundant check from arraymodule b_getitem (GH-14676) · python/cpython@13ab570 · GitHub
[go: up one dir, main page]

Skip to content

Commit 13ab570

Browse files
disconnect3dmiss-islington
authored andcommitted
Remove redundant check from arraymodule b_getitem (GH-14676)
The `arraymodule`'s `b_getitem` function returns a `PyLong` converted from `arrayobject`'s array, by dereferencing a pointer to `char`. When the `char` type is `signed char`, the `if (x >= 128) x -= 256;` comparison/code is redundant because a `signed char` will have a value of `[-128, 127]` and so `x` will never be greater or equal than 128. This check was indeed needed for situations where a given compiler would assume `char` being `unsigned char` which would make `x` in `[0, 256]` range. However, the check can be removed if we cast the `ob_item` into a signed char pointer (`signed char*`) instead of `char*`. This PR/commit introduces this change.
1 parent 4737265 commit 13ab570

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

Modules/arraymodule.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ in bounds; that's the responsibility of the caller.
185185
static PyObject *
186186
b_getitem(arrayobject *ap, Py_ssize_t i)
187187
{
188-
long x = ((char *)ap->ob_item)[i];
189-
if (x >= 128)
190-
x -= 256;
188+
long x = ((signed char *)ap->ob_item)[i];
191189
return PyLong_FromLong(x);
192190
}
193191

0 commit comments

Comments
 (0)
0