From: Victor Stinner Date: Thu, 3 Jan 2013 02:18:09 +0000 (+0100) Subject: Close #16281: handle tailmatch() failure and remove useless comment X-Git-Tag: v3.4.0a1~1700 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=18aa4477d37b8ead78f5f6c36c5da8b8ac593091;p=python Close #16281: handle tailmatch() failure and remove useless comment "honor direction and do a forward or backwards search": the runtime speed may be different, but I consider that it doesn't really matter in practice. The direction was never honored before: Python 2.7 uses memcmp() for the str type for example. --- diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 1f602b7ea6..88010e674c 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8935,7 +8935,7 @@ tailmatch(PyObject *self, if (PyUnicode_READY(self) == -1 || PyUnicode_READY(substring) == -1) - return 0; + return -1; if (PyUnicode_GET_LENGTH(substring) == 0) return 1; @@ -8973,7 +8973,6 @@ tailmatch(PyObject *self, /* We do not need to compare 0 and len(substring)-1 because the if statement above ensured already that they are equal when we end up here. */ - /* TODO: honor direction and do a forward or backwards search */ for (i = 1; i < end_sub; ++i) { if (PyUnicode_READ(kind_self, data_self, offset + i) != PyUnicode_READ(kind_sub, data_sub, i)) @@ -12597,6 +12596,8 @@ unicode_startswith(PyObject *self, return NULL; result = tailmatch(self, substring, start, end, -1); Py_DECREF(substring); + if (result == -1) + return NULL; if (result) { Py_RETURN_TRUE; } @@ -12613,6 +12614,8 @@ unicode_startswith(PyObject *self, } result = tailmatch(self, substring, start, end, -1); Py_DECREF(substring); + if (result == -1) + return NULL; return PyBool_FromLong(result); } @@ -12646,6 +12649,8 @@ unicode_endswith(PyObject *self, return NULL; result = tailmatch(self, substring, start, end, +1); Py_DECREF(substring); + if (result == -1) + return NULL; if (result) { Py_RETURN_TRUE; } @@ -12660,6 +12665,8 @@ unicode_endswith(PyObject *self, return NULL; } result = tailmatch(self, substring, start, end, +1); + if (result == -1) + return NULL; Py_DECREF(substring); return PyBool_FromLong(result); }