Added checks to prevent PyUnicode_Count() from dumping core
in case the parameters are out of bounds and fixes error handling for .count(), .startswith() and .endswith() for the case of mixed string/Unicode objects. This patch adds Python style index semantics to PyUnicode_Count() indices (including the special handling of negative indices). The patch is an extended version of patch #103249 submitted by Michael Hudson (mwh) on SF. It also includes new test cases.
This commit is contained in:
parent
1c5aa6901f
commit
3a645e4dd4
@ -53,6 +53,10 @@ def run_method_tests(test):
|
|||||||
|
|
||||||
test('capitalize', ' hello ', ' hello ')
|
test('capitalize', ' hello ', ' hello ')
|
||||||
test('capitalize', 'hello ', 'Hello ')
|
test('capitalize', 'hello ', 'Hello ')
|
||||||
|
|
||||||
|
test('count', 'aaa', 3, 'a')
|
||||||
|
test('count', 'aaa', 0, 'b')
|
||||||
|
|
||||||
test('find', 'abcdefghiabc', 0, 'abc')
|
test('find', 'abcdefghiabc', 0, 'abc')
|
||||||
test('find', 'abcdefghiabc', 9, 'abc', 1)
|
test('find', 'abcdefghiabc', 9, 'abc', 1)
|
||||||
test('find', 'abcdefghiabc', -1, 'def', 4)
|
test('find', 'abcdefghiabc', -1, 'def', 4)
|
||||||
|
@ -32,6 +32,13 @@ def test(method, input, output, *args):
|
|||||||
test('capitalize', u' hello ', u' hello ')
|
test('capitalize', u' hello ', u' hello ')
|
||||||
test('capitalize', u'hello ', u'Hello ')
|
test('capitalize', u'hello ', u'Hello ')
|
||||||
|
|
||||||
|
test('count', u'aaa', 3, u'a')
|
||||||
|
test('count', u'aaa', 0, u'b')
|
||||||
|
test('count', 'aaa', 3, u'a')
|
||||||
|
test('count', 'aaa', 0, u'b')
|
||||||
|
test('count', u'aaa', 3, 'a')
|
||||||
|
test('count', u'aaa', 0, 'b')
|
||||||
|
|
||||||
test('title', u' hello ', u' Hello ')
|
test('title', u' hello ', u' Hello ')
|
||||||
test('title', u'hello ', u'Hello ')
|
test('title', u'hello ', u'Hello ')
|
||||||
test('title', u"fOrMaT thIs aS titLe String", u'Format This As Title String')
|
test('title', u"fOrMaT thIs aS titLe String", u'Format This As Title String')
|
||||||
|
@ -1236,9 +1236,14 @@ string_count(PyStringObject *self, PyObject *args)
|
|||||||
sub = PyString_AS_STRING(subobj);
|
sub = PyString_AS_STRING(subobj);
|
||||||
n = PyString_GET_SIZE(subobj);
|
n = PyString_GET_SIZE(subobj);
|
||||||
}
|
}
|
||||||
else if (PyUnicode_Check(subobj))
|
else if (PyUnicode_Check(subobj)) {
|
||||||
return PyInt_FromLong(
|
int count;
|
||||||
PyUnicode_Count((PyObject *)self, subobj, i, last));
|
count = PyUnicode_Count((PyObject *)self, subobj, i, last);
|
||||||
|
if (count == -1)
|
||||||
|
return NULL;
|
||||||
|
else
|
||||||
|
return PyInt_FromLong((long) count);
|
||||||
|
}
|
||||||
else if (PyObject_AsCharBuffer(subobj, &sub, &n))
|
else if (PyObject_AsCharBuffer(subobj, &sub, &n))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@ -1637,10 +1642,15 @@ string_startswith(PyStringObject *self, PyObject *args)
|
|||||||
prefix = PyString_AS_STRING(subobj);
|
prefix = PyString_AS_STRING(subobj);
|
||||||
plen = PyString_GET_SIZE(subobj);
|
plen = PyString_GET_SIZE(subobj);
|
||||||
}
|
}
|
||||||
else if (PyUnicode_Check(subobj))
|
else if (PyUnicode_Check(subobj)) {
|
||||||
return PyInt_FromLong(
|
int rc;
|
||||||
PyUnicode_Tailmatch((PyObject *)self,
|
rc = PyUnicode_Tailmatch((PyObject *)self,
|
||||||
subobj, start, end, -1));
|
subobj, start, end, -1);
|
||||||
|
if (rc == -1)
|
||||||
|
return NULL;
|
||||||
|
else
|
||||||
|
return PyInt_FromLong((long) rc);
|
||||||
|
}
|
||||||
else if (PyObject_AsCharBuffer(subobj, &prefix, &plen))
|
else if (PyObject_AsCharBuffer(subobj, &prefix, &plen))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@ -1690,10 +1700,15 @@ string_endswith(PyStringObject *self, PyObject *args)
|
|||||||
suffix = PyString_AS_STRING(subobj);
|
suffix = PyString_AS_STRING(subobj);
|
||||||
slen = PyString_GET_SIZE(subobj);
|
slen = PyString_GET_SIZE(subobj);
|
||||||
}
|
}
|
||||||
else if (PyUnicode_Check(subobj))
|
else if (PyUnicode_Check(subobj)) {
|
||||||
return PyInt_FromLong(
|
int rc;
|
||||||
PyUnicode_Tailmatch((PyObject *)self,
|
rc = PyUnicode_Tailmatch((PyObject *)self,
|
||||||
subobj, start, end, +1));
|
subobj, start, end, +1);
|
||||||
|
if (rc == -1)
|
||||||
|
return NULL;
|
||||||
|
else
|
||||||
|
return PyInt_FromLong((long) rc);
|
||||||
|
}
|
||||||
else if (PyObject_AsCharBuffer(subobj, &suffix, &slen))
|
else if (PyObject_AsCharBuffer(subobj, &suffix, &slen))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -2392,6 +2392,17 @@ int count(PyUnicodeObject *self,
|
|||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
|
if (start < 0)
|
||||||
|
start += self->length;
|
||||||
|
if (start < 0)
|
||||||
|
start = 0;
|
||||||
|
if (end > self->length)
|
||||||
|
end = self->length;
|
||||||
|
if (end < 0)
|
||||||
|
end += self->length;
|
||||||
|
if (end < 0)
|
||||||
|
end = 0;
|
||||||
|
|
||||||
if (substring->length == 0)
|
if (substring->length == 0)
|
||||||
return (end - start + 1);
|
return (end - start + 1);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user