[Python-checkins] Remove redundant check from arraymodule b_getitem (GH-14676)

Miss Islington (bot) webhook-mailer at python.org
Thu Jul 11 17:57:54 EDT 2019


https://github.com/python/cpython/commit/13ab570febac64bb55fb46d866a9ba5a46ab8902
commit: 13ab570febac64bb55fb46d866a9ba5a46ab8902
branch: master
author: Disconnect3d <dominik.b.czarnota at gmail.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-07-11T14:57:42-07:00
summary:

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.

files:
M Modules/arraymodule.c

diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 48986c419029..6aa981daca1d 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -185,9 +185,7 @@ in bounds; that's the responsibility of the caller.
 static PyObject *
 b_getitem(arrayobject *ap, Py_ssize_t i)
 {
-    long x = ((char *)ap->ob_item)[i];
-    if (x >= 128)
-        x -= 256;
+    long x = ((signed char *)ap->ob_item)[i];
     return PyLong_FromLong(x);
 }
 



More information about the Python-checkins mailing list