From b328cd8c2bd46f38c4b7e947520fa77c3f591aeb Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 1 Jul 2016 17:22:31 +0300 Subject: Issue #27007: The fromhex() class methods of bytes and bytearray subclasses now return an instance of corresponding subclass. --- Objects/bytearrayobject.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'Objects/bytearrayobject.c') diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index b7dfd6f20f..85990e0be4 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1968,7 +1968,6 @@ bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) @classmethod bytearray.fromhex - cls: self(type="PyObject*") string: unicode / @@ -1979,10 +1978,15 @@ Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') [clinic start generated code]*/ static PyObject * -bytearray_fromhex_impl(PyObject*cls, PyObject *string) -/*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/ +bytearray_fromhex_impl(PyTypeObject *type, PyObject *string) +/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/ { - return _PyBytes_FromHex(string, 1); + PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type); + if (type != &PyByteArray_Type && result != NULL) { + Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type, + result, NULL)); + } + return result; } PyDoc_STRVAR(hex__doc__, -- cgit v1.2.1 From e4807217e8bcde18faaf68cb608079fe4429e73e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 1 Jul 2016 17:57:30 +0300 Subject: Issue #26765: Moved wrappers for bytes and bytearray methods to common header file. --- Objects/bytearrayobject.c | 79 +++++++---------------------------------------- 1 file changed, 12 insertions(+), 67 deletions(-) (limited to 'Objects/bytearrayobject.c') diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 85990e0be4..fcde77b09e 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1097,18 +1097,6 @@ bytearray_dealloc(PyByteArrayObject *self) #include "stringlib/transmogrify.h" -static PyObject * -bytearray_find(PyByteArrayObject *self, PyObject *args) -{ - return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); -} - -static PyObject * -bytearray_count(PyByteArrayObject *self, PyObject *args) -{ - return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); -} - /*[clinic input] bytearray.clear @@ -1138,42 +1126,6 @@ bytearray_copy_impl(PyByteArrayObject *self) PyByteArray_GET_SIZE(self)); } -static PyObject * -bytearray_index(PyByteArrayObject *self, PyObject *args) -{ - return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); -} - -static PyObject * -bytearray_rfind(PyByteArrayObject *self, PyObject *args) -{ - return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); -} - -static PyObject * -bytearray_rindex(PyByteArrayObject *self, PyObject *args) -{ - return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); -} - -static int -bytearray_contains(PyObject *self, PyObject *arg) -{ - return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg); -} - -static PyObject * -bytearray_startswith(PyByteArrayObject *self, PyObject *args) -{ - return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); -} - -static PyObject * -bytearray_endswith(PyByteArrayObject *self, PyObject *args) -{ - return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); -} - /*[clinic input] bytearray.translate @@ -1329,8 +1281,8 @@ bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/ { return stringlib_replace((PyObject *)self, - (const char *)old->buf, old->len, - (const char *)new->buf, new->len, count); + old->buf, old->len, + new->buf, new->len, count); } /*[clinic input] @@ -1995,14 +1947,6 @@ PyDoc_STRVAR(hex__doc__, Create a string of hexadecimal numbers from a bytearray object.\n\ Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); -static PyObject * -bytearray_hex(PyBytesObject *self) -{ - char* argbuf = PyByteArray_AS_STRING(self); - Py_ssize_t arglen = PyByteArray_GET_SIZE(self); - return _Py_strhex(argbuf, arglen); -} - static PyObject * _common_reduce(PyByteArrayObject *self, int proto) { @@ -2091,7 +2035,7 @@ static PySequenceMethods bytearray_as_sequence = { 0, /* sq_slice */ (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ 0, /* sq_ass_slice */ - (objobjproc)bytearray_contains, /* sq_contains */ + (objobjproc)stringlib_contains, /* sq_contains */ (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ }; @@ -2119,19 +2063,19 @@ bytearray_methods[] = { {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__}, BYTEARRAY_CLEAR_METHODDEF BYTEARRAY_COPY_METHODDEF - {"count", (PyCFunction)bytearray_count, METH_VARARGS, + {"count", (PyCFunction)stringlib_method_count, METH_VARARGS, _Py_count__doc__}, BYTEARRAY_DECODE_METHODDEF - {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, + {"endswith", (PyCFunction)stringlib_endswith, METH_VARARGS, _Py_endswith__doc__}, {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, _Py_expandtabs__doc__}, BYTEARRAY_EXTEND_METHODDEF - {"find", (PyCFunction)bytearray_find, METH_VARARGS, + {"find", (PyCFunction)stringlib_method_find, METH_VARARGS, _Py_find__doc__}, BYTEARRAY_FROMHEX_METHODDEF - {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, - {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__}, + {"hex", (PyCFunction)stringlib_hex, METH_NOARGS, hex__doc__}, + {"index", (PyCFunction)stringlib_index, METH_VARARGS, _Py_index__doc__}, BYTEARRAY_INSERT_METHODDEF {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, _Py_isalnum__doc__}, @@ -2157,15 +2101,16 @@ bytearray_methods[] = { BYTEARRAY_REMOVE_METHODDEF BYTEARRAY_REPLACE_METHODDEF BYTEARRAY_REVERSE_METHODDEF - {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__}, - {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__}, + {"rfind", (PyCFunction)stringlib_method_rfind, METH_VARARGS, + _Py_rfind__doc__}, + {"rindex", (PyCFunction)stringlib_rindex, METH_VARARGS, _Py_rindex__doc__}, {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__}, BYTEARRAY_RPARTITION_METHODDEF BYTEARRAY_RSPLIT_METHODDEF BYTEARRAY_RSTRIP_METHODDEF BYTEARRAY_SPLIT_METHODDEF BYTEARRAY_SPLITLINES_METHODDEF - {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , + {"startswith", (PyCFunction)stringlib_startswith, METH_VARARGS, _Py_startswith__doc__}, BYTEARRAY_STRIP_METHODDEF {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, -- cgit v1.2.1 From 6341b89ccbfcfb8720a07d133d0eb0d36a0a1bfc Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 3 Jul 2016 13:57:48 +0300 Subject: Backed out changeset b0087e17cd5e (issue #26765) For unknown reasons it perhaps caused a crash on 32-bit Windows (issue #). --- Objects/bytearrayobject.c | 79 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 12 deletions(-) (limited to 'Objects/bytearrayobject.c') diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index fcde77b09e..85990e0be4 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1097,6 +1097,18 @@ bytearray_dealloc(PyByteArrayObject *self) #include "stringlib/transmogrify.h" +static PyObject * +bytearray_find(PyByteArrayObject *self, PyObject *args) +{ + return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); +} + +static PyObject * +bytearray_count(PyByteArrayObject *self, PyObject *args) +{ + return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); +} + /*[clinic input] bytearray.clear @@ -1126,6 +1138,42 @@ bytearray_copy_impl(PyByteArrayObject *self) PyByteArray_GET_SIZE(self)); } +static PyObject * +bytearray_index(PyByteArrayObject *self, PyObject *args) +{ + return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); +} + +static PyObject * +bytearray_rfind(PyByteArrayObject *self, PyObject *args) +{ + return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); +} + +static PyObject * +bytearray_rindex(PyByteArrayObject *self, PyObject *args) +{ + return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); +} + +static int +bytearray_contains(PyObject *self, PyObject *arg) +{ + return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg); +} + +static PyObject * +bytearray_startswith(PyByteArrayObject *self, PyObject *args) +{ + return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); +} + +static PyObject * +bytearray_endswith(PyByteArrayObject *self, PyObject *args) +{ + return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); +} + /*[clinic input] bytearray.translate @@ -1281,8 +1329,8 @@ bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/ { return stringlib_replace((PyObject *)self, - old->buf, old->len, - new->buf, new->len, count); + (const char *)old->buf, old->len, + (const char *)new->buf, new->len, count); } /*[clinic input] @@ -1947,6 +1995,14 @@ PyDoc_STRVAR(hex__doc__, Create a string of hexadecimal numbers from a bytearray object.\n\ Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); +static PyObject * +bytearray_hex(PyBytesObject *self) +{ + char* argbuf = PyByteArray_AS_STRING(self); + Py_ssize_t arglen = PyByteArray_GET_SIZE(self); + return _Py_strhex(argbuf, arglen); +} + static PyObject * _common_reduce(PyByteArrayObject *self, int proto) { @@ -2035,7 +2091,7 @@ static PySequenceMethods bytearray_as_sequence = { 0, /* sq_slice */ (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ 0, /* sq_ass_slice */ - (objobjproc)stringlib_contains, /* sq_contains */ + (objobjproc)bytearray_contains, /* sq_contains */ (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ }; @@ -2063,19 +2119,19 @@ bytearray_methods[] = { {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__}, BYTEARRAY_CLEAR_METHODDEF BYTEARRAY_COPY_METHODDEF - {"count", (PyCFunction)stringlib_method_count, METH_VARARGS, + {"count", (PyCFunction)bytearray_count, METH_VARARGS, _Py_count__doc__}, BYTEARRAY_DECODE_METHODDEF - {"endswith", (PyCFunction)stringlib_endswith, METH_VARARGS, + {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, _Py_endswith__doc__}, {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, _Py_expandtabs__doc__}, BYTEARRAY_EXTEND_METHODDEF - {"find", (PyCFunction)stringlib_method_find, METH_VARARGS, + {"find", (PyCFunction)bytearray_find, METH_VARARGS, _Py_find__doc__}, BYTEARRAY_FROMHEX_METHODDEF - {"hex", (PyCFunction)stringlib_hex, METH_NOARGS, hex__doc__}, - {"index", (PyCFunction)stringlib_index, METH_VARARGS, _Py_index__doc__}, + {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, + {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__}, BYTEARRAY_INSERT_METHODDEF {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, _Py_isalnum__doc__}, @@ -2101,16 +2157,15 @@ bytearray_methods[] = { BYTEARRAY_REMOVE_METHODDEF BYTEARRAY_REPLACE_METHODDEF BYTEARRAY_REVERSE_METHODDEF - {"rfind", (PyCFunction)stringlib_method_rfind, METH_VARARGS, - _Py_rfind__doc__}, - {"rindex", (PyCFunction)stringlib_rindex, METH_VARARGS, _Py_rindex__doc__}, + {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__}, + {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__}, {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__}, BYTEARRAY_RPARTITION_METHODDEF BYTEARRAY_RSPLIT_METHODDEF BYTEARRAY_RSTRIP_METHODDEF BYTEARRAY_SPLIT_METHODDEF BYTEARRAY_SPLITLINES_METHODDEF - {"startswith", (PyCFunction)stringlib_startswith, METH_VARARGS, + {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , _Py_startswith__doc__}, BYTEARRAY_STRIP_METHODDEF {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, -- cgit v1.2.1 From 06fa68daa61a76e3876025d093fe19e62560a885 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 15 Aug 2016 09:46:07 +0300 Subject: Issue #27704: Optimized creating bytes and bytearray from byte-like objects and iterables. Speed up to 3 times for short objects. Original patch by Naoki Inada. --- Objects/bytearrayobject.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'Objects/bytearrayobject.c') diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index f8c21d4e62..de2dca95ec 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -795,17 +795,15 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) } /* Is it an int? */ - count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); - if (count == -1 && PyErr_Occurred()) { - if (PyErr_ExceptionMatches(PyExc_OverflowError)) + if (PyIndex_Check(arg)) { + count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); + if (count == -1 && PyErr_Occurred()) { return -1; - PyErr_Clear(); - } - else if (count < 0) { - PyErr_SetString(PyExc_ValueError, "negative count"); - return -1; - } - else { + } + if (count < 0) { + PyErr_SetString(PyExc_ValueError, "negative count"); + return -1; + } if (count > 0) { if (PyByteArray_Resize((PyObject *)self, count)) return -1; -- cgit v1.2.1 From cfca21d2c0676e30605feffdd4012b631163dedc Mon Sep 17 00:00:00 2001 From: Martin Panter Date: Sat, 27 Aug 2016 08:35:02 +0000 Subject: Issue #27506: Support bytes/bytearray.translate() delete as keyword argument Patch by Xiang Zhang. --- Objects/bytearrayobject.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'Objects/bytearrayobject.c') diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index de2dca95ec..b6631f9ac5 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1175,21 +1175,19 @@ bytearray.translate table: object Translation table, which must be a bytes object of length 256. - [ - deletechars: object - ] / + delete as deletechars: object(c_default="NULL") = b'' Return a copy with each character mapped by the given translation table. -All characters occurring in the optional argument deletechars are removed. +All characters occurring in the optional argument delete are removed. The remaining characters are mapped through the given translation table. [clinic start generated code]*/ static PyObject * bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, - int group_right_1, PyObject *deletechars) -/*[clinic end generated code: output=2bebc86a9a1ff083 input=846a01671bccc1c5]*/ + PyObject *deletechars) +/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/ { char *input, *output; const char *table_chars; @@ -1258,8 +1256,7 @@ bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, for (i = inlen; --i >= 0; ) { c = Py_CHARMASK(*input++); if (trans_table[c] != -1) - if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) - continue; + *output++ = (char)trans_table[c]; } /* Fix the size of the resulting string */ if (inlen > 0) -- cgit v1.2.1 From 4846ba81c69ebc815b35a89ed54fef7b5ffb1417 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 30 Aug 2016 10:47:49 -0700 Subject: Issue #27895: Spelling fixes (Contributed by Ville Skytt?). --- Objects/bytearrayobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Objects/bytearrayobject.c') diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index b6631f9ac5..c6d0707167 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -481,7 +481,7 @@ bytearray_setslice_linear(PyByteArrayObject *self, If growth < 0 and lo != 0, the operation is completed, but a MemoryError is still raised and the memory block is not - shrinked. Otherwise, the bytearray is restored in its previous + shrunk. Otherwise, the bytearray is restored in its previous state and a MemoryError is raised. */ if (lo == 0) { self->ob_start += growth; -- cgit v1.2.1 From 4a409ba347a7454d73db9802ab31f7fe17e5e6bc Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 6 Jan 2017 17:32:01 +0900 Subject: Issue #29159: Fix regression in bytes(x) when x.__index__() raises Exception. --- Objects/bytearrayobject.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'Objects/bytearrayobject.c') diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index c6d0707167..a8d6980250 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -798,18 +798,22 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) if (PyIndex_Check(arg)) { count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); if (count == -1 && PyErr_Occurred()) { - return -1; - } - if (count < 0) { - PyErr_SetString(PyExc_ValueError, "negative count"); - return -1; + if (PyErr_ExceptionMatches(PyExc_OverflowError)) + return -1; + PyErr_Clear(); /* fall through */ } - if (count > 0) { - if (PyByteArray_Resize((PyObject *)self, count)) + else { + if (count < 0) { + PyErr_SetString(PyExc_ValueError, "negative count"); return -1; - memset(PyByteArray_AS_STRING(self), 0, count); + } + if (count > 0) { + if (PyByteArray_Resize((PyObject *)self, count)) + return -1; + memset(PyByteArray_AS_STRING(self), 0, count); + } + return 0; } - return 0; } /* Use the buffer API */ -- cgit v1.2.1