Skip to content

Commit 3550489

Browse files
Issue python#26765: Moved wrappers for bytes and bytearray methods to common header
file.
1 parent 0855e70 commit 3550489

3 files changed

Lines changed: 77 additions & 138 deletions

File tree

‎Objects/bytearrayobject.c‎

Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,18 +1097,6 @@ bytearray_dealloc(PyByteArrayObject *self)
10971097
#include "stringlib/transmogrify.h"
10981098

10991099

1100-
static PyObject *
1101-
bytearray_find(PyByteArrayObject *self, PyObject *args)
1102-
{
1103-
return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1104-
}
1105-
1106-
static PyObject *
1107-
bytearray_count(PyByteArrayObject *self, PyObject *args)
1108-
{
1109-
return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1110-
}
1111-
11121100
/*[clinic input]
11131101
bytearray.clear
11141102
@@ -1138,42 +1126,6 @@ bytearray_copy_impl(PyByteArrayObject *self)
11381126
PyByteArray_GET_SIZE(self));
11391127
}
11401128

1141-
static PyObject *
1142-
bytearray_index(PyByteArrayObject *self, PyObject *args)
1143-
{
1144-
return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1145-
}
1146-
1147-
static PyObject *
1148-
bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1149-
{
1150-
return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1151-
}
1152-
1153-
static PyObject *
1154-
bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1155-
{
1156-
return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1157-
}
1158-
1159-
static int
1160-
bytearray_contains(PyObject *self, PyObject *arg)
1161-
{
1162-
return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1163-
}
1164-
1165-
static PyObject *
1166-
bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1167-
{
1168-
return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1169-
}
1170-
1171-
static PyObject *
1172-
bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1173-
{
1174-
return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1175-
}
1176-
11771129

11781130
/*[clinic input]
11791131
bytearray.translate
@@ -1329,8 +1281,8 @@ bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
13291281
/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
13301282
{
13311283
return stringlib_replace((PyObject *)self,
1332-
(const char *)old->buf, old->len,
1333-
(const char *)new->buf, new->len, count);
1284+
old->buf, old->len,
1285+
new->buf, new->len, count);
13341286
}
13351287

13361288
/*[clinic input]
@@ -1995,14 +1947,6 @@ PyDoc_STRVAR(hex__doc__,
19951947
Create a string of hexadecimal numbers from a bytearray object.\n\
19961948
Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
19971949

1998-
static PyObject *
1999-
bytearray_hex(PyBytesObject *self)
2000-
{
2001-
char* argbuf = PyByteArray_AS_STRING(self);
2002-
Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2003-
return _Py_strhex(argbuf, arglen);
2004-
}
2005-
20061950
static PyObject *
20071951
_common_reduce(PyByteArrayObject *self, int proto)
20081952
{
@@ -2091,7 +2035,7 @@ static PySequenceMethods bytearray_as_sequence = {
20912035
0, /* sq_slice */
20922036
(ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
20932037
0, /* sq_ass_slice */
2094-
(objobjproc)bytearray_contains, /* sq_contains */
2038+
(objobjproc)stringlib_contains, /* sq_contains */
20952039
(binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
20962040
(ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
20972041
};
@@ -2119,19 +2063,19 @@ bytearray_methods[] = {
21192063
{"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__},
21202064
BYTEARRAY_CLEAR_METHODDEF
21212065
BYTEARRAY_COPY_METHODDEF
2122-
{"count", (PyCFunction)bytearray_count, METH_VARARGS,
2066+
{"count", (PyCFunction)stringlib_method_count, METH_VARARGS,
21232067
_Py_count__doc__},
21242068
BYTEARRAY_DECODE_METHODDEF
2125-
{"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
2069+
{"endswith", (PyCFunction)stringlib_endswith, METH_VARARGS,
21262070
_Py_endswith__doc__},
21272071
{"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
21282072
_Py_expandtabs__doc__},
21292073
BYTEARRAY_EXTEND_METHODDEF
2130-
{"find", (PyCFunction)bytearray_find, METH_VARARGS,
2074+
{"find", (PyCFunction)stringlib_method_find, METH_VARARGS,
21312075
_Py_find__doc__},
21322076
BYTEARRAY_FROMHEX_METHODDEF
2133-
{"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2134-
{"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
2077+
{"hex", (PyCFunction)stringlib_hex, METH_NOARGS, hex__doc__},
2078+
{"index", (PyCFunction)stringlib_index, METH_VARARGS, _Py_index__doc__},
21352079
BYTEARRAY_INSERT_METHODDEF
21362080
{"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
21372081
_Py_isalnum__doc__},
@@ -2157,15 +2101,16 @@ bytearray_methods[] = {
21572101
BYTEARRAY_REMOVE_METHODDEF
21582102
BYTEARRAY_REPLACE_METHODDEF
21592103
BYTEARRAY_REVERSE_METHODDEF
2160-
{"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2161-
{"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
2104+
{"rfind", (PyCFunction)stringlib_method_rfind, METH_VARARGS,
2105+
_Py_rfind__doc__},
2106+
{"rindex", (PyCFunction)stringlib_rindex, METH_VARARGS, _Py_rindex__doc__},
21622107
{"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__},
21632108
BYTEARRAY_RPARTITION_METHODDEF
21642109
BYTEARRAY_RSPLIT_METHODDEF
21652110
BYTEARRAY_RSTRIP_METHODDEF
21662111
BYTEARRAY_SPLIT_METHODDEF
21672112
BYTEARRAY_SPLITLINES_METHODDEF
2168-
{"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
2113+
{"startswith", (PyCFunction)stringlib_startswith, METH_VARARGS,
21692114
_Py_startswith__doc__},
21702115
BYTEARRAY_STRIP_METHODDEF
21712116
{"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,

‎Objects/bytesobject.c‎

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,12 +1485,6 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
14851485
return (PyObject *) op;
14861486
}
14871487

1488-
static int
1489-
bytes_contains(PyObject *self, PyObject *arg)
1490-
{
1491-
return _Py_bytes_contains(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), arg);
1492-
}
1493-
14941488
static PyObject *
14951489
bytes_item(PyBytesObject *a, Py_ssize_t i)
14961490
{
@@ -1701,7 +1695,7 @@ static PySequenceMethods bytes_as_sequence = {
17011695
0, /*sq_slice*/
17021696
0, /*sq_ass_item*/
17031697
0, /*sq_ass_slice*/
1704-
(objobjproc)bytes_contains /*sq_contains*/
1698+
(objobjproc)stringlib_contains /*sq_contains*/
17051699
};
17061700

17071701
static PyMappingMethods bytes_as_mapping = {
@@ -1873,32 +1867,6 @@ _PyBytes_Join(PyObject *sep, PyObject *x)
18731867
return bytes_join((PyBytesObject*)sep, x);
18741868
}
18751869

1876-
static PyObject *
1877-
bytes_find(PyBytesObject *self, PyObject *args)
1878-
{
1879-
return _Py_bytes_find(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
1880-
}
1881-
1882-
static PyObject *
1883-
bytes_index(PyBytesObject *self, PyObject *args)
1884-
{
1885-
return _Py_bytes_index(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
1886-
}
1887-
1888-
1889-
static PyObject *
1890-
bytes_rfind(PyBytesObject *self, PyObject *args)
1891-
{
1892-
return _Py_bytes_rfind(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
1893-
}
1894-
1895-
1896-
static PyObject *
1897-
bytes_rindex(PyBytesObject *self, PyObject *args)
1898-
{
1899-
return _Py_bytes_rindex(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
1900-
}
1901-
19021870

19031871
Py_LOCAL_INLINE(PyObject *)
19041872
do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj)
@@ -2035,13 +2003,6 @@ bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes)
20352003
}
20362004

20372005

2038-
static PyObject *
2039-
bytes_count(PyBytesObject *self, PyObject *args)
2040-
{
2041-
return _Py_bytes_count(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
2042-
}
2043-
2044-
20452006
/*[clinic input]
20462007
bytes.translate
20472008
@@ -2228,19 +2189,6 @@ bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
22282189
/** End DALKE **/
22292190

22302191

2231-
static PyObject *
2232-
bytes_startswith(PyBytesObject *self, PyObject *args)
2233-
{
2234-
return _Py_bytes_startswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
2235-
}
2236-
2237-
static PyObject *
2238-
bytes_endswith(PyBytesObject *self, PyObject *args)
2239-
{
2240-
return _Py_bytes_endswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
2241-
}
2242-
2243-
22442192
/*[clinic input]
22452193
bytes.decode
22462194
@@ -2394,14 +2342,6 @@ PyDoc_STRVAR(hex__doc__,
23942342
Create a string of hexadecimal numbers from a bytes object.\n\
23952343
Example: b'\\xb9\\x01\\xef'.hex() -> 'b901ef'.");
23962344

2397-
static PyObject *
2398-
bytes_hex(PyBytesObject *self)
2399-
{
2400-
char* argbuf = PyBytes_AS_STRING(self);
2401-
Py_ssize_t arglen = PyBytes_GET_SIZE(self);
2402-
return _Py_strhex(argbuf, arglen);
2403-
}
2404-
24052345
static PyObject *
24062346
bytes_getnewargs(PyBytesObject *v)
24072347
{
@@ -2414,20 +2354,19 @@ bytes_methods[] = {
24142354
{"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS},
24152355
{"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
24162356
_Py_capitalize__doc__},
2417-
{"center", (PyCFunction)stringlib_center, METH_VARARGS,
2418-
_Py_center__doc__},
2419-
{"count", (PyCFunction)bytes_count, METH_VARARGS,
2357+
{"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__},
2358+
{"count", (PyCFunction)stringlib_method_count, METH_VARARGS,
24202359
_Py_count__doc__},
24212360
BYTES_DECODE_METHODDEF
2422-
{"endswith", (PyCFunction)bytes_endswith, METH_VARARGS,
2361+
{"endswith", (PyCFunction)stringlib_endswith, METH_VARARGS,
24232362
_Py_endswith__doc__},
24242363
{"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
24252364
_Py_expandtabs__doc__},
2426-
{"find", (PyCFunction)bytes_find, METH_VARARGS,
2365+
{"find", (PyCFunction)stringlib_method_find, METH_VARARGS,
24272366
_Py_find__doc__},
24282367
BYTES_FROMHEX_METHODDEF
2429-
{"hex", (PyCFunction)bytes_hex, METH_NOARGS, hex__doc__},
2430-
{"index", (PyCFunction)bytes_index, METH_VARARGS, _Py_index__doc__},
2368+
{"hex", (PyCFunction)stringlib_hex, METH_NOARGS, hex__doc__},
2369+
{"index", (PyCFunction)stringlib_index, METH_VARARGS, _Py_index__doc__},
24312370
{"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
24322371
_Py_isalnum__doc__},
24332372
{"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
@@ -2449,15 +2388,16 @@ bytes_methods[] = {
24492388
BYTES_MAKETRANS_METHODDEF
24502389
BYTES_PARTITION_METHODDEF
24512390
BYTES_REPLACE_METHODDEF
2452-
{"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, _Py_rfind__doc__},
2453-
{"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, _Py_rindex__doc__},
2391+
{"rfind", (PyCFunction)stringlib_method_rfind, METH_VARARGS,
2392+
_Py_rfind__doc__},
2393+
{"rindex", (PyCFunction)stringlib_rindex, METH_VARARGS, _Py_rindex__doc__},
24542394
{"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__},
24552395
BYTES_RPARTITION_METHODDEF
24562396
BYTES_RSPLIT_METHODDEF
24572397
BYTES_RSTRIP_METHODDEF
24582398
BYTES_SPLIT_METHODDEF
24592399
BYTES_SPLITLINES_METHODDEF
2460-
{"startswith", (PyCFunction)bytes_startswith, METH_VARARGS,
2400+
{"startswith", (PyCFunction)stringlib_startswith, METH_VARARGS,
24612401
_Py_startswith__doc__},
24622402
BYTES_STRIP_METHODDEF
24632403
{"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,

‎Objects/stringlib/transmogrify.h‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,60 @@
22
# error "transmogrify.h only compatible with byte-wise strings"
33
#endif
44

5+
Py_LOCAL(PyObject *)
6+
stringlib_method_find(PyObject *self, PyObject *args)
7+
{
8+
return _Py_bytes_find(STRINGLIB_STR(self), STRINGLIB_LEN(self), args);
9+
}
10+
11+
Py_LOCAL(PyObject *)
12+
stringlib_index(PyObject *self, PyObject *args)
13+
{
14+
return _Py_bytes_index(STRINGLIB_STR(self), STRINGLIB_LEN(self), args);
15+
}
16+
17+
Py_LOCAL(PyObject *)
18+
stringlib_method_rfind(PyObject *self, PyObject *args)
19+
{
20+
return _Py_bytes_rfind(STRINGLIB_STR(self), STRINGLIB_LEN(self), args);
21+
}
22+
23+
Py_LOCAL(PyObject *)
24+
stringlib_rindex(PyObject *self, PyObject *args)
25+
{
26+
return _Py_bytes_rindex(STRINGLIB_STR(self), STRINGLIB_LEN(self), args);
27+
}
28+
29+
Py_LOCAL(PyObject *)
30+
stringlib_method_count(PyObject *self, PyObject *args)
31+
{
32+
return _Py_bytes_count(STRINGLIB_STR(self), STRINGLIB_LEN(self), args);
33+
}
34+
35+
Py_LOCAL(int)
36+
stringlib_contains(PyObject *self, PyObject *arg)
37+
{
38+
return _Py_bytes_contains(STRINGLIB_STR(self), STRINGLIB_LEN(self), arg);
39+
}
40+
41+
Py_LOCAL(PyObject *)
42+
stringlib_startswith(PyObject *self, PyObject *args)
43+
{
44+
return _Py_bytes_startswith(STRINGLIB_STR(self), STRINGLIB_LEN(self), args);
45+
}
46+
47+
Py_LOCAL(PyObject *)
48+
stringlib_endswith(PyObject *self, PyObject *args)
49+
{
50+
return _Py_bytes_endswith(STRINGLIB_STR(self), STRINGLIB_LEN(self), args);
51+
}
52+
53+
Py_LOCAL(PyObject *)
54+
stringlib_hex(PyObject *self)
55+
{
56+
return _Py_strhex(STRINGLIB_STR(self), STRINGLIB_LEN(self));
57+
}
58+
559
/* the more complicated methods. parts of these should be pulled out into the
660
shared code in bytes_methods.c to cut down on duplicate code bloat. */
761

0 commit comments

Comments
 (0)