Skip to content

Commit b954fc7

Browse files
committed
fix: fix python lint error and make code more simple
1 parent 2cb388b commit b954fc7

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
@@ -1228,21 +1228,32 @@ Return the lowest index in B where subsection 'sub' is found, such that 'sub' is
12281228
Return -1 on failure.
12291229
[clinic start generated code]*/
12301230

1231+
typedef PyObject* (*_ba_bytes_op)(const char *buf, Py_ssize_t len,
1232+
PyObject *sub, Py_ssize_t start,
1233+
Py_ssize_t end);
1234+
12311235
static PyObject *
1232-
bytearray_find_impl(PyByteArrayObject *self, PyObject *sub, Py_ssize_t start,
1233-
Py_ssize_t end)
1234-
/*[clinic end generated code: output=413e1cab2ae87da0 input=df3aa94840d893a7]*/
1236+
_bytearray_with_buffer(PyByteArrayObject *self, PyObject *sub,
1237+
Py_ssize_t start, Py_ssize_t end, _ba_bytes_op op)
12351238
{
1236-
Py_buffer selfbuf;
1239+
Py_buffer view;
12371240
PyObject *res;
1238-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1241+
if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_SIMPLE) != 0) {
12391242
return NULL;
12401243
}
1241-
res = _Py_bytes_find((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1242-
PyBuffer_Release(&selfbuf);
1244+
res = op((const char *)view.buf, view.len, sub, start, end);
1245+
PyBuffer_Release(&view);
12431246
return res;
12441247
}
12451248

1249+
static PyObject *
1250+
bytearray_find_impl(PyByteArrayObject *self, PyObject *sub, Py_ssize_t start,
1251+
Py_ssize_t end)
1252+
/*[clinic end generated code: output=413e1cab2ae87da0 input=df3aa94840d893a7]*/
1253+
{
1254+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_find);
1255+
}
1256+
12461257
/*[clinic input]
12471258
@permit_long_summary
12481259
@critical_section
@@ -1256,14 +1267,7 @@ bytearray_count_impl(PyByteArrayObject *self, PyObject *sub,
12561267
Py_ssize_t start, Py_ssize_t end)
12571268
/*[clinic end generated code: output=a21ee2692e4f1233 input=e8fcdca8272857e0]*/
12581269
{
1259-
Py_buffer selfbuf;
1260-
PyObject *res;
1261-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1262-
return NULL;
1263-
}
1264-
res = _Py_bytes_count((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1265-
PyBuffer_Release(&selfbuf);
1266-
return res;
1270+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_count);
12671271
}
12681272

12691273
/*[clinic input]
@@ -1311,14 +1315,7 @@ bytearray_index_impl(PyByteArrayObject *self, PyObject *sub,
13111315
Py_ssize_t start, Py_ssize_t end)
13121316
/*[clinic end generated code: output=067a1e78efc672a7 input=c37f177cfee19fe4]*/
13131317
{
1314-
Py_buffer selfbuf;
1315-
PyObject *res;
1316-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1317-
return NULL;
1318-
}
1319-
res = _Py_bytes_index((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1320-
PyBuffer_Release(&selfbuf);
1321-
return res;
1318+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_index);
13221319
}
13231320

13241321
/*[clinic input]
@@ -1336,14 +1333,7 @@ bytearray_rfind_impl(PyByteArrayObject *self, PyObject *sub,
13361333
Py_ssize_t start, Py_ssize_t end)
13371334
/*[clinic end generated code: output=51bf886f932b283c input=1265b11c437d2750]*/
13381335
{
1339-
Py_buffer selfbuf;
1340-
PyObject *res;
1341-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1342-
return NULL;
1343-
}
1344-
res = _Py_bytes_rfind((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1345-
PyBuffer_Release(&selfbuf);
1346-
return res;
1336+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_rfind);
13471337
}
13481338

13491339
/*[clinic input]
@@ -1361,14 +1351,7 @@ bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,
13611351
Py_ssize_t start, Py_ssize_t end)
13621352
/*[clinic end generated code: output=38e1cf66bafb08b9 input=7d198b3d6b0a62ce]*/
13631353
{
1364-
Py_buffer selfbuf;
1365-
PyObject *res;
1366-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1367-
return NULL;
1368-
}
1369-
res = _Py_bytes_rindex((const char *)selfbuf.buf, selfbuf.len, sub, start, end);
1370-
PyBuffer_Release(&selfbuf);
1371-
return res;
1354+
return _bytearray_with_buffer(self, sub, start, end, _Py_bytes_rindex);
13721355
}
13731356

13741357
static int
@@ -1406,15 +1389,7 @@ bytearray_startswith_impl(PyByteArrayObject *self, PyObject *subobj,
14061389
Py_ssize_t start, Py_ssize_t end)
14071390
/*[clinic end generated code: output=a3d9b6d44d3662a6 input=93f9ffee684f109a]*/
14081391
{
1409-
Py_buffer selfbuf;
1410-
PyObject *res;
1411-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1412-
return NULL;
1413-
}
1414-
res = _Py_bytes_startswith((const char *)selfbuf.buf, selfbuf.len,
1415-
subobj, start, end);
1416-
PyBuffer_Release(&selfbuf);
1417-
return res;
1392+
return _bytearray_with_buffer(self, subobj, start, end, _Py_bytes_startswith);
14181393
}
14191394

14201395
/*[clinic input]
@@ -1439,15 +1414,7 @@ bytearray_endswith_impl(PyByteArrayObject *self, PyObject *subobj,
14391414
Py_ssize_t start, Py_ssize_t end)
14401415
/*[clinic end generated code: output=e75ea8c227954caa input=d158b030a11d0b06]*/
14411416
{
1442-
Py_buffer selfbuf;
1443-
PyObject *res;
1444-
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) != 0) {
1445-
return NULL;
1446-
}
1447-
res = _Py_bytes_endswith((const char *)selfbuf.buf, selfbuf.len,
1448-
subobj, start, end);
1449-
PyBuffer_Release(&selfbuf);
1450-
return res;
1417+
return _bytearray_with_buffer(self, subobj, start, end, _Py_bytes_endswith);
14511418
}
14521419

14531420
/*[clinic input]

0 commit comments

Comments
 (0)