From 232355ea845c6fc7b9e7228bf62909dc83f3dbdf Mon Sep 17 00:00:00 2001 From: Martin Panter Date: Mon, 25 Jul 2016 02:21:14 +0000 Subject: Issue #7063: Remove dead code from array slice handling Patch by Chuck. --- Modules/arraymodule.c | 55 ++++++++------------------------------------------- 1 file changed, 8 insertions(+), 47 deletions(-) (limited to 'Modules/arraymodule.c') diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index c785872a5f..4d9a23fb99 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -846,37 +846,10 @@ array_repeat(arrayobject *a, Py_ssize_t n) } static int -array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) +array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) { char *item; - Py_ssize_t n; /* Size of replacement array */ Py_ssize_t d; /* Change in size */ -#define b ((arrayobject *)v) - if (v == NULL) - n = 0; - else if (array_Check(v)) { - n = Py_SIZE(b); - if (a == b) { - /* Special case "a[i:j] = a" -- copy b first */ - int ret; - v = array_slice(b, 0, n); - if (!v) - return -1; - ret = array_ass_slice(a, ilow, ihigh, v); - Py_DECREF(v); - return ret; - } - if (b->ob_descr != a->ob_descr) { - PyErr_BadArgument(); - return -1; - } - } - else { - PyErr_Format(PyExc_TypeError, - "can only assign array (not \"%.200s\") to array slice", - Py_TYPE(v)->tp_name); - return -1; - } if (ilow < 0) ilow = 0; else if (ilow > Py_SIZE(a)) @@ -888,7 +861,7 @@ array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) else if (ihigh > Py_SIZE(a)) ihigh = Py_SIZE(a); item = a->ob_item; - d = n - (ihigh-ilow); + d = ihigh-ilow; /* Issue #4509: If the array has exported buffers and the slice assignment would change the size of the array, fail early to make sure we don't modify it. */ @@ -897,25 +870,14 @@ array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) "cannot resize an array that is exporting buffers"); return -1; } - if (d < 0) { /* Delete -d items */ - memmove(item + (ihigh+d)*a->ob_descr->itemsize, + if (d > 0) { /* Delete d items */ + memmove(item + (ihigh-d)*a->ob_descr->itemsize, item + ihigh*a->ob_descr->itemsize, (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); - if (array_resize(a, Py_SIZE(a) + d) == -1) + if (array_resize(a, Py_SIZE(a) - d) == -1) return -1; } - else if (d > 0) { /* Insert d items */ - if (array_resize(a, Py_SIZE(a) + d)) - return -1; - memmove(item + (ihigh+d)*a->ob_descr->itemsize, - item + ihigh*a->ob_descr->itemsize, - (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); - } - if (n > 0) - memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, - n*b->ob_descr->itemsize); return 0; -#undef b } static int @@ -927,7 +889,7 @@ array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) return -1; } if (v == NULL) - return array_ass_slice(a, i, i+1, v); + return array_del_slice(a, i, i+1); return (*a->ob_descr->setitem)(a, i, v); } @@ -1155,8 +1117,7 @@ array_array_remove(arrayobject *self, PyObject *v) cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); Py_DECREF(selfi); if (cmp > 0) { - if (array_ass_slice(self, i, i+1, - (PyObject *)NULL) != 0) + if (array_del_slice(self, i, i+1) != 0) return NULL; Py_INCREF(Py_None); return Py_None; @@ -1199,7 +1160,7 @@ array_array_pop_impl(arrayobject *self, Py_ssize_t i) v = getarrayitem((PyObject *)self, i); if (v == NULL) return NULL; - if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { + if (array_del_slice(self, i, i+1) != 0) { Py_DECREF(v); return NULL; } -- cgit v1.2.1 From 907ccded8b6fa68f6f572ae5de6760595df0d5a1 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 5 Sep 2016 17:44:18 -0700 Subject: require a long long data type (closes #27961) --- Modules/arraymodule.c | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'Modules/arraymodule.c') diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 4d9a23fb99..8637cb52b1 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -418,8 +418,6 @@ LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) return 0; } -#ifdef HAVE_LONG_LONG - static PyObject * q_getitem(arrayobject *ap, Py_ssize_t i) { @@ -469,7 +467,6 @@ QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) ((unsigned PY_LONG_LONG *)ap->ob_item)[i] = x; return 0; } -#endif static PyObject * f_getitem(arrayobject *ap, Py_ssize_t i) @@ -521,10 +518,8 @@ static const struct arraydescr descriptors[] = { {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0}, {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1}, {'L', sizeof(long), LL_getitem, LL_setitem, "L", 1, 0}, -#ifdef HAVE_LONG_LONG {'q', sizeof(PY_LONG_LONG), q_getitem, q_setitem, "q", 1, 1}, {'Q', sizeof(PY_LONG_LONG), QQ_getitem, QQ_setitem, "Q", 1, 0}, -#endif {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0}, {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0}, {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */ @@ -1814,7 +1809,6 @@ typecode_to_mformat_code(char typecode) intsize = sizeof(long); is_signed = 0; break; -#if HAVE_LONG_LONG case 'q': intsize = sizeof(PY_LONG_LONG); is_signed = 1; @@ -1823,7 +1817,6 @@ typecode_to_mformat_code(char typecode) intsize = sizeof(PY_LONG_LONG); is_signed = 0; break; -#endif default: return UNKNOWN_FORMAT; } @@ -2685,11 +2678,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } } PyErr_SetString(PyExc_ValueError, -#ifdef HAVE_LONG_LONG "bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)"); -#else - "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)"); -#endif return NULL; } -- cgit v1.2.1 From 914b59533a094e815809f62441d02b560e23057b Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 6 Sep 2016 10:46:49 -0700 Subject: replace PY_LONG_LONG with long long --- Modules/arraymodule.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'Modules/arraymodule.c') diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 8637cb52b1..86a58d9170 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -421,17 +421,17 @@ LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) static PyObject * q_getitem(arrayobject *ap, Py_ssize_t i) { - return PyLong_FromLongLong(((PY_LONG_LONG *)ap->ob_item)[i]); + return PyLong_FromLongLong(((long long *)ap->ob_item)[i]); } static int q_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { - PY_LONG_LONG x; + long long x; if (!PyArg_Parse(v, "L;array item must be integer", &x)) return -1; if (i >= 0) - ((PY_LONG_LONG *)ap->ob_item)[i] = x; + ((long long *)ap->ob_item)[i] = x; return 0; } @@ -439,20 +439,20 @@ static PyObject * QQ_getitem(arrayobject *ap, Py_ssize_t i) { return PyLong_FromUnsignedLongLong( - ((unsigned PY_LONG_LONG *)ap->ob_item)[i]); + ((unsigned long long *)ap->ob_item)[i]); } static int QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { - unsigned PY_LONG_LONG x; + unsigned long long x; if (PyLong_Check(v)) { x = PyLong_AsUnsignedLongLong(v); - if (x == (unsigned PY_LONG_LONG) -1 && PyErr_Occurred()) + if (x == (unsigned long long) -1 && PyErr_Occurred()) return -1; } else { - PY_LONG_LONG y; + long long y; if (!PyArg_Parse(v, "L;array item must be integer", &y)) return -1; if (y < 0) { @@ -460,11 +460,11 @@ QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) "unsigned long long is less than minimum"); return -1; } - x = (unsigned PY_LONG_LONG)y; + x = (unsigned long long)y; } if (i >= 0) - ((unsigned PY_LONG_LONG *)ap->ob_item)[i] = x; + ((unsigned long long *)ap->ob_item)[i] = x; return 0; } @@ -518,8 +518,8 @@ static const struct arraydescr descriptors[] = { {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0}, {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1}, {'L', sizeof(long), LL_getitem, LL_setitem, "L", 1, 0}, - {'q', sizeof(PY_LONG_LONG), q_getitem, q_setitem, "q", 1, 1}, - {'Q', sizeof(PY_LONG_LONG), QQ_getitem, QQ_setitem, "Q", 1, 0}, + {'q', sizeof(long long), q_getitem, q_setitem, "q", 1, 1}, + {'Q', sizeof(long long), QQ_getitem, QQ_setitem, "Q", 1, 0}, {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0}, {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0}, {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */ @@ -1810,11 +1810,11 @@ typecode_to_mformat_code(char typecode) is_signed = 0; break; case 'q': - intsize = sizeof(PY_LONG_LONG); + intsize = sizeof(long long); is_signed = 1; break; case 'Q': - intsize = sizeof(PY_LONG_LONG); + intsize = sizeof(long long); is_signed = 0; break; default: -- cgit v1.2.1 From a516e391d34bf1983282a237d9eb60d7c2cbb82f Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 13 Sep 2016 20:22:02 +0200 Subject: Issue #28126: Replace Py_MEMCPY with memcpy(). Visual Studio can properly optimize memcpy(). --- Modules/arraymodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Modules/arraymodule.c') diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 5868c52ca8..2caa8ee5a8 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -836,10 +836,10 @@ array_repeat(arrayobject *a, Py_ssize_t n) memset(np->ob_item, a->ob_item[0], newbytes); } else { Py_ssize_t done = oldbytes; - Py_MEMCPY(np->ob_item, a->ob_item, oldbytes); + memcpy(np->ob_item, a->ob_item, oldbytes); while (done < newbytes) { Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done; - Py_MEMCPY(np->ob_item+done, np->ob_item, ncopy); + memcpy(np->ob_item+done, np->ob_item, ncopy); done += ncopy; } } -- cgit v1.2.1