Skip to content

Commit ac45150

Browse files
committed
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes python#11828)
1 parent d07eaf1 commit ac45150

File tree

8 files changed

+197
-64
lines changed

8 files changed

+197
-64
lines changed

Lib/test/string_tests.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,63 @@ def test_rpartition(self):
11561156
self.checkraises(ValueError, S, 'rpartition', '')
11571157
self.checkraises(TypeError, S, 'rpartition', None)
11581158

1159+
def test_none_arguments(self):
1160+
# issue 11828
1161+
s = 'hello'
1162+
self.checkequal(2, s, 'find', 'l', None)
1163+
self.checkequal(3, s, 'find', 'l', -2, None)
1164+
self.checkequal(2, s, 'find', 'l', None, -2)
1165+
self.checkequal(0, s, 'find', 'h', None, None)
1166+
1167+
self.checkequal(3, s, 'rfind', 'l', None)
1168+
self.checkequal(3, s, 'rfind', 'l', -2, None)
1169+
self.checkequal(2, s, 'rfind', 'l', None, -2)
1170+
self.checkequal(0, s, 'rfind', 'h', None, None)
1171+
1172+
self.checkequal(2, s, 'index', 'l', None)
1173+
self.checkequal(3, s, 'index', 'l', -2, None)
1174+
self.checkequal(2, s, 'index', 'l', None, -2)
1175+
self.checkequal(0, s, 'index', 'h', None, None)
1176+
1177+
self.checkequal(3, s, 'rindex', 'l', None)
1178+
self.checkequal(3, s, 'rindex', 'l', -2, None)
1179+
self.checkequal(2, s, 'rindex', 'l', None, -2)
1180+
self.checkequal(0, s, 'rindex', 'h', None, None)
1181+
1182+
self.checkequal(2, s, 'count', 'l', None)
1183+
self.checkequal(1, s, 'count', 'l', -2, None)
1184+
self.checkequal(1, s, 'count', 'l', None, -2)
1185+
self.checkequal(0, s, 'count', 'x', None, None)
1186+
1187+
self.checkequal(True, s, 'endswith', 'o', None)
1188+
self.checkequal(True, s, 'endswith', 'lo', -2, None)
1189+
self.checkequal(True, s, 'endswith', 'l', None, -2)
1190+
self.checkequal(False, s, 'endswith', 'x', None, None)
1191+
1192+
self.checkequal(True, s, 'startswith', 'h', None)
1193+
self.checkequal(True, s, 'startswith', 'l', -2, None)
1194+
self.checkequal(True, s, 'startswith', 'h', None, -2)
1195+
self.checkequal(False, s, 'startswith', 'x', None, None)
1196+
1197+
def test_find_etc_raise_correct_error_messages(self):
1198+
# issue 11828
1199+
s = 'hello'
1200+
x = 'x'
1201+
self.assertRaisesRegexp(TypeError, r'^find\(', s.find,
1202+
x, None, None, None)
1203+
self.assertRaisesRegexp(TypeError, r'^rfind\(', s.rfind,
1204+
x, None, None, None)
1205+
self.assertRaisesRegexp(TypeError, r'^index\(', s.index,
1206+
x, None, None, None)
1207+
self.assertRaisesRegexp(TypeError, r'^rindex\(', s.rindex,
1208+
x, None, None, None)
1209+
self.assertRaisesRegexp(TypeError, r'^count\(', s.count,
1210+
x, None, None, None)
1211+
self.assertRaisesRegexp(TypeError, r'^startswith\(', s.startswith,
1212+
x, None, None, None)
1213+
self.assertRaisesRegexp(TypeError, r'^endswith\(', s.endswith,
1214+
x, None, None, None)
1215+
11591216

11601217
class MixinStrUnicodeTest:
11611218
# Additional tests that only work with str and unicode.

Lib/test/test_bytes.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,68 @@ def test_maketrans(self):
459459
self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq')
460460
self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def')
461461

462+
def test_none_arguments(self):
463+
# issue 11828
464+
b = self.type2test(b'hello')
465+
l = self.type2test(b'l')
466+
h = self.type2test(b'h')
467+
x = self.type2test(b'x')
468+
o = self.type2test(b'o')
469+
470+
self.assertEqual(2, b.find(l, None))
471+
self.assertEqual(3, b.find(l, -2, None))
472+
self.assertEqual(2, b.find(l, None, -2))
473+
self.assertEqual(0, b.find(h, None, None))
474+
475+
self.assertEqual(3, b.rfind(l, None))
476+
self.assertEqual(3, b.rfind(l, -2, None))
477+
self.assertEqual(2, b.rfind(l, None, -2))
478+
self.assertEqual(0, b.rfind(h, None, None))
479+
480+
self.assertEqual(2, b.index(l, None))
481+
self.assertEqual(3, b.index(l, -2, None))
482+
self.assertEqual(2, b.index(l, None, -2))
483+
self.assertEqual(0, b.index(h, None, None))
484+
485+
self.assertEqual(3, b.rindex(l, None))
486+
self.assertEqual(3, b.rindex(l, -2, None))
487+
self.assertEqual(2, b.rindex(l, None, -2))
488+
self.assertEqual(0, b.rindex(h, None, None))
489+
490+
self.assertEqual(2, b.count(l, None))
491+
self.assertEqual(1, b.count(l, -2, None))
492+
self.assertEqual(1, b.count(l, None, -2))
493+
self.assertEqual(0, b.count(x, None, None))
494+
495+
self.assertEqual(True, b.endswith(o, None))
496+
self.assertEqual(True, b.endswith(o, -2, None))
497+
self.assertEqual(True, b.endswith(l, None, -2))
498+
self.assertEqual(False, b.endswith(x, None, None))
499+
500+
self.assertEqual(True, b.startswith(h, None))
501+
self.assertEqual(True, b.startswith(l, -2, None))
502+
self.assertEqual(True, b.startswith(h, None, -2))
503+
self.assertEqual(False, b.startswith(x, None, None))
504+
505+
def test_find_etc_raise_correct_error_messages(self):
506+
# issue 11828
507+
b = self.type2test(b'hello')
508+
x = self.type2test(b'x')
509+
self.assertRaisesRegexp(TypeError, r'\bfind\b', b.find,
510+
x, None, None, None)
511+
self.assertRaisesRegexp(TypeError, r'\brfind\b', b.rfind,
512+
x, None, None, None)
513+
self.assertRaisesRegexp(TypeError, r'\bindex\b', b.index,
514+
x, None, None, None)
515+
self.assertRaisesRegexp(TypeError, r'\brindex\b', b.rindex,
516+
x, None, None, None)
517+
self.assertRaisesRegexp(TypeError, r'\bcount\b', b.count,
518+
x, None, None, None)
519+
self.assertRaisesRegexp(TypeError, r'\bstartswith\b', b.startswith,
520+
x, None, None, None)
521+
self.assertRaisesRegexp(TypeError, r'\bendswith\b', b.endswith,
522+
x, None, None, None)
523+
462524

463525
class BytesTest(BaseBytesTest):
464526
type2test = bytes

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Donald Beaudry
5858
David Beazley
5959
Robin Becker
6060
Neal Becker
61+
Torsten Becker
6162
Bill Bedford
6263
Stefan Behnel
6364
Reimer Behrends

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Core and Builtins
3838
- Issue #8278: On Windows and with a NTFS filesystem, os.stat() and os.utime()
3939
can now handle dates after 2038.
4040

41+
- issue #11828: startswith and endswith don't accept None as slice index.
42+
Patch by Torsten Becker.
43+
4144
- Issue #4236: PyModule_Create2 now checks the import machinery directly
4245
rather than the Py_IsInitialized flag, avoiding a Fatal Python
4346
error in certain circumstances when an import is done in __del__.

Objects/bytearrayobject.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,8 @@ bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
10821082
Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
10831083
Py_ssize_t res;
10841084

1085-
if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj,
1086-
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1085+
if (!stringlib_parse_args_finds("find/rfind/index/rindex",
1086+
args, &subobj, &start, &end))
10871087
return -2;
10881088
if (_getbuffer(subobj, &subbuf) < 0)
10891089
return -2;
@@ -1133,8 +1133,7 @@ bytearray_count(PyByteArrayObject *self, PyObject *args)
11331133
Py_buffer vsub;
11341134
PyObject *count_obj;
11351135

1136-
if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
1137-
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1136+
if (!stringlib_parse_args_finds("count", args, &sub_obj, &start, &end))
11381137
return NULL;
11391138

11401139
if (_getbuffer(sub_obj, &vsub) < 0)
@@ -1292,8 +1291,7 @@ bytearray_startswith(PyByteArrayObject *self, PyObject *args)
12921291
PyObject *subobj;
12931292
int result;
12941293

1295-
if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
1296-
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1294+
if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
12971295
return NULL;
12981296
if (PyTuple_Check(subobj)) {
12991297
Py_ssize_t i;
@@ -1332,8 +1330,7 @@ bytearray_endswith(PyByteArrayObject *self, PyObject *args)
13321330
PyObject *subobj;
13331331
int result;
13341332

1335-
if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
1336-
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1333+
if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
13371334
return NULL;
13381335
if (PyTuple_Check(subobj)) {
13391336
Py_ssize_t i;

Objects/bytesobject.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,19 +1567,9 @@ bytes_find_internal(PyBytesObject *self, PyObject *args, int dir)
15671567
const char *sub;
15681568
Py_ssize_t sub_len;
15691569
Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1570-
PyObject *obj_start=Py_None, *obj_end=Py_None;
15711570

1572-
if (!PyArg_ParseTuple(args, "O|OO:find/rfind/index/rindex", &subobj,
1573-
&obj_start, &obj_end))
1574-
return -2;
1575-
/* To support None in "start" and "end" arguments, meaning
1576-
the same as if they were not passed.
1577-
*/
1578-
if (obj_start != Py_None)
1579-
if (!_PyEval_SliceIndex(obj_start, &start))
1580-
return -2;
1581-
if (obj_end != Py_None)
1582-
if (!_PyEval_SliceIndex(obj_end, &end))
1571+
if (!stringlib_parse_args_finds("find/rfind/index/rindex",
1572+
args, &subobj, &start, &end))
15831573
return -2;
15841574

15851575
if (PyBytes_Check(subobj)) {
@@ -1826,8 +1816,7 @@ bytes_count(PyBytesObject *self, PyObject *args)
18261816
Py_ssize_t sub_len;
18271817
Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
18281818

1829-
if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
1830-
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1819+
if (!stringlib_parse_args_finds("count", args, &sub_obj, &start, &end))
18311820
return NULL;
18321821

18331822
if (PyBytes_Check(sub_obj)) {
@@ -2648,8 +2637,7 @@ bytes_startswith(PyBytesObject *self, PyObject *args)
26482637
PyObject *subobj;
26492638
int result;
26502639

2651-
if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
2652-
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
2640+
if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
26532641
return NULL;
26542642
if (PyTuple_Check(subobj)) {
26552643
Py_ssize_t i;
@@ -2689,8 +2677,7 @@ bytes_endswith(PyBytesObject *self, PyObject *args)
26892677
PyObject *subobj;
26902678
int result;
26912679

2692-
if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
2693-
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
2680+
if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
26942681
return NULL;
26952682
if (PyTuple_Check(subobj)) {
26962683
Py_ssize_t i;

Objects/stringlib/find.h

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,33 @@ stringlib_contains_obj(PyObject* str, PyObject* sub)
102102

103103
#endif /* STRINGLIB_STR */
104104

105-
#ifdef FROM_UNICODE
106-
107105
/*
108106
This function is a helper for the "find" family (find, rfind, index,
109-
rindex) of unicodeobject.c file, because they all have the same
110-
behaviour for the arguments.
107+
rindex) and for count, startswith and endswith, because they all have
108+
the same behaviour for the arguments.
111109
112110
It does not touch the variables received until it knows everything
113111
is ok.
114-
115-
Note that we receive a pointer to the pointer of the substring object,
116-
so when we create that object in this function we don't DECREF it,
117-
because it continues living in the caller functions (those functions,
118-
after finishing using the substring, must DECREF it).
119112
*/
120113

114+
#define FORMAT_BUFFER_SIZE 50
115+
121116
Py_LOCAL_INLINE(int)
122-
_ParseTupleFinds (PyObject *args, PyObject **substring,
123-
Py_ssize_t *start, Py_ssize_t *end) {
124-
PyObject *tmp_substring;
117+
stringlib_parse_args_finds(const char * function_name, PyObject *args,
118+
PyObject **subobj,
119+
Py_ssize_t *start, Py_ssize_t *end)
120+
{
121+
PyObject *tmp_subobj;
125122
Py_ssize_t tmp_start = 0;
126123
Py_ssize_t tmp_end = PY_SSIZE_T_MAX;
127124
PyObject *obj_start=Py_None, *obj_end=Py_None;
125+
char format[FORMAT_BUFFER_SIZE] = "O|OO:";
126+
size_t len = strlen(format);
128127

129-
if (!PyArg_ParseTuple(args, "O|OO:find", &tmp_substring,
130-
&obj_start, &obj_end))
128+
strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1);
129+
format[FORMAT_BUFFER_SIZE - 1] = '\0';
130+
131+
if (!PyArg_ParseTuple(args, format, &tmp_subobj, &obj_start, &obj_end))
131132
return 0;
132133

133134
/* To support None in "start" and "end" arguments, meaning
@@ -140,16 +141,44 @@ _ParseTupleFinds (PyObject *args, PyObject **substring,
140141
if (!_PyEval_SliceIndex(obj_end, &tmp_end))
141142
return 0;
142143

143-
tmp_substring = PyUnicode_FromObject(tmp_substring);
144-
if (!tmp_substring)
145-
return 0;
146-
147144
*start = tmp_start;
148145
*end = tmp_end;
149-
*substring = tmp_substring;
146+
*subobj = tmp_subobj;
150147
return 1;
151148
}
152149

150+
#undef FORMAT_BUFFER_SIZE
151+
152+
#ifdef FROM_UNICODE
153+
154+
/*
155+
Wraps stringlib_parse_args_finds() and additionally ensures that the
156+
first argument is a unicode object.
157+
158+
Note that we receive a pointer to the pointer of the substring object,
159+
so when we create that object in this function we don't DECREF it,
160+
because it continues living in the caller functions (those functions,
161+
after finishing using the substring, must DECREF it).
162+
*/
163+
164+
Py_LOCAL_INLINE(int)
165+
stringlib_parse_args_finds_unicode(const char * function_name, PyObject *args,
166+
PyUnicodeObject **substring,
167+
Py_ssize_t *start, Py_ssize_t *end)
168+
{
169+
PyObject *tmp_substring;
170+
171+
if(stringlib_parse_args_finds(function_name, args, &tmp_substring,
172+
start, end)) {
173+
tmp_substring = PyUnicode_FromObject(tmp_substring);
174+
if (!tmp_substring)
175+
return 0;
176+
*substring = (PyUnicodeObject *)tmp_substring;
177+
return 1;
178+
}
179+
return 0;
180+
}
181+
153182
#endif /* FROM_UNICODE */
154183

155184
#endif /* STRINGLIB_FIND_H */

0 commit comments

Comments
 (0)