Skip to content

Commit e4bdc6c

Browse files
committed
fix: fix python lint error and make code more simple
1 parent ca7f893 commit e4bdc6c

File tree

2 files changed

+24
-58
lines changed

2 files changed

+24
-58
lines changed

Lib/test/test_bytes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2078,7 +2078,6 @@ def __index__(self):
20782078
ba.rfind(Evil())
20792079

20802080

2081-
20822081
class AssortedBytesTest(unittest.TestCase):
20832082
#
20842083
# Test various combinations of bytes and bytearray

Objects/bytearrayobject.c

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,21 +1243,32 @@ Return the lowest index in B where subsection 'sub' is found, such that 'sub' is
12431243
Return -1 on failure.
12441244
[clinic start generated code]*/
12451245

1246+
typedef PyObject* (*_ba_bytes_op)(const char *buf, Py_ssize_t len,
1247+
PyObject *sub, Py_ssize_t start,
1248+
Py_ssize_t end);
1249+
12461250
static PyObject *
1247-
bytearray_find_impl(PyByteArrayObject *self, PyObject *sub, Py_ssize_t start,
1248-
Py_ssize_t end)
1249-
/*[clinic end generated code: output=413e1cab2ae87da0 input=df3aa94840d893a7]*/
1251+
_bytearray_with_buffer(PyByteArrayObject *self, PyObject *sub,
1252+
Py_ssize_t start, Py_ssize_t end, _ba_bytes_op op)
12501253
{
1251-
Py_buffer selfbuf;
1254+
Py_buffer view;
12521255
PyObject *res;
1253-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1256+
if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_SIMPLE) != 0) {
12541257
return NULL;
12551258
}
1256-
res = _Py_bytes_find((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1257-
PyBuffer_Release(&selfbuf);
1259+
res = op((const char *)view.buf, view.len, sub, start, end);
1260+
PyBuffer_Release(&view);
12581261
return res;
12591262
}
12601263

1264+
static PyObject *
1265+
bytearray_find_impl(PyByteArrayObject *self, PyObject *sub, Py_ssize_t start,
1266+
Py_ssize_t end)
1267+
/*[clinic end generated code: output=413e1cab2ae87da0 input=df3aa94840d893a7]*/
1268+
{
1269+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_find);
1270+
}
1271+
12611272
/*[clinic input]
12621273
@permit_long_summary
12631274
@critical_section
@@ -1271,14 +1282,7 @@ bytearray_count_impl(PyByteArrayObject *self, PyObject *sub,
12711282
Py_ssize_t start, Py_ssize_t end)
12721283
/*[clinic end generated code: output=a21ee2692e4f1233 input=e8fcdca8272857e0]*/
12731284
{
1274-
Py_buffer selfbuf;
1275-
PyObject *res;
1276-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1277-
return NULL;
1278-
}
1279-
res = _Py_bytes_count((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1280-
PyBuffer_Release(&selfbuf);
1281-
return res;
1285+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_count);
12821286
}
12831287

12841288
/*[clinic input]
@@ -1326,14 +1330,7 @@ bytearray_index_impl(PyByteArrayObject *self, PyObject *sub,
13261330
Py_ssize_t start, Py_ssize_t end)
13271331
/*[clinic end generated code: output=067a1e78efc672a7 input=c37f177cfee19fe4]*/
13281332
{
1329-
Py_buffer selfbuf;
1330-
PyObject *res;
1331-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1332-
return NULL;
1333-
}
1334-
res = _Py_bytes_index((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1335-
PyBuffer_Release(&selfbuf);
1336-
return res;
1333+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_index);
13371334
}
13381335

13391336
/*[clinic input]
@@ -1351,14 +1348,7 @@ bytearray_rfind_impl(PyByteArrayObject *self, PyObject *sub,
13511348
Py_ssize_t start, Py_ssize_t end)
13521349
/*[clinic end generated code: output=51bf886f932b283c input=1265b11c437d2750]*/
13531350
{
1354-
Py_buffer selfbuf;
1355-
PyObject *res;
1356-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1357-
return NULL;
1358-
}
1359-
res = _Py_bytes_rfind((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1360-
PyBuffer_Release(&selfbuf);
1361-
return res;
1351+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_rfind);
13621352
}
13631353

13641354
/*[clinic input]
@@ -1376,14 +1366,7 @@ bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,
13761366
Py_ssize_t start, Py_ssize_t end)
13771367
/*[clinic end generated code: output=38e1cf66bafb08b9 input=7d198b3d6b0a62ce]*/
13781368
{
1379-
Py_buffer selfbuf;
1380-
PyObject *res;
1381-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1382-
return NULL;
1383-
}
1384-
res = _Py_bytes_rindex((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1385-
PyBuffer_Release(&selfbuf);
1386-
return res;
1369+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_rindex);
13871370
}
13881371

13891372
static int
@@ -1421,15 +1404,7 @@ bytearray_startswith_impl(PyByteArrayObject *self, PyObject *subobj,
14211404
Py_ssize_t start, Py_ssize_t end)
14221405
/*[clinic end generated code: output=a3d9b6d44d3662a6 input=93f9ffee684f109a]*/
14231406
{
1424-
Py_buffer selfbuf;
1425-
PyObject *res;
1426-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1427-
return NULL;
1428-
}
1429-
res = _Py_bytes_startswith((const char *)selfbuf.buf, selfbuf.len,
1430-
subobj, start, end);
1431-
PyBuffer_Release(&selfbuf);
1432-
return res;
1407+
return _bytearray_with_buffer(self, subobj, start, end, _Py_bytes_startswith);
14331408
}
14341409

14351410
/*[clinic input]
@@ -1454,15 +1429,7 @@ bytearray_endswith_impl(PyByteArrayObject *self, PyObject *subobj,
14541429
Py_ssize_t start, Py_ssize_t end)
14551430
/*[clinic end generated code: output=e75ea8c227954caa input=d158b030a11d0b06]*/
14561431
{
1457-
Py_buffer selfbuf;
1458-
PyObject *res;
1459-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1460-
return NULL;
1461-
}
1462-
res = _Py_bytes_endswith((const char *)selfbuf.buf, selfbuf.len,
1463-
subobj, start, end);
1464-
PyBuffer_Release(&selfbuf);
1465-
return res;
1432+
return _bytearray_with_buffer(self, subobj, start, end, _Py_bytes_endswith);
14661433
}
14671434

14681435
/*[clinic input]

0 commit comments

Comments
 (0)