summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-31 06:15:51 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-31 06:15:51 (GMT)
commitd4ea03c785d659576e0ae65c12fe5c03ada872d0 (patch)
treef1df6f933651ff70bb27a72f3cf518ed406844b9
parent3d4a457663d5b211e9357bd2ccfd60e0b5d9e57b (diff)
downloadcpython-d4ea03c785d659576e0ae65c12fe5c03ada872d0.zip
cpython-d4ea03c785d659576e0ae65c12fe5c03ada872d0.tar.gz
cpython-d4ea03c785d659576e0ae65c12fe5c03ada872d0.tar.bz2
Issue #24284: The startswith and endswith methods of the str class no longer
return True when finding the empty string and the indexes are completely out of range.
-rw-r--r--Doc/whatsnew/3.5.rst4
-rw-r--r--Lib/test/string_tests.py6
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/unicodeobject.c6
4 files changed, 17 insertions, 3 deletions
diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
index c39d975..ee0e5d1 100644
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -1049,6 +1049,10 @@ Changes in the Python API
program depends on patching the module level variable to capture the debug
output, you will need to update it to capture sys.stderr instead.
+* The :meth:`str.startswith` and :meth:`str.endswith` methods no longer return
+ ``True`` when finding the empty string and the indexes are completely out of
+ range. See :issue:`24284`.
+
Changes in the C API
--------------------
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 6e26474..e086994 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -976,6 +976,9 @@ class MixinStrUnicodeUserStringTest:
self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
+ self.checkequal(True, '', 'startswith', '', 0, 1)
+ self.checkequal(True, '', 'startswith', '', 0, 0)
+ self.checkequal(False, '', 'startswith', '', 1, 0)
# test negative indices
self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
@@ -1022,6 +1025,9 @@ class MixinStrUnicodeUserStringTest:
self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
+ self.checkequal(True, '', 'endswith', '', 0, 1)
+ self.checkequal(True, '', 'endswith', '', 0, 0)
+ self.checkequal(False, '', 'endswith', '', 1, 0)
# test negative indices
self.checkequal(True, 'hello', 'endswith', 'lo', -2)
diff --git a/Misc/NEWS b/Misc/NEWS
index d0223c5..b56cb49 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ Release date: 2015-07-05
Core and Builtins
-----------------
+- Issue #24284: The startswith and endswith methods of the str class no longer
+ return True when finding the empty string and the indexes are completely out
+ of range.
+
- Issue #24115: Update uses of PyObject_IsTrue(), PyObject_Not(),
PyObject_IsInstance(), PyObject_RichCompareBool() and _PyDict_Contains()
to check for and handle errors correctly.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 84e67e6..1eaf2e9 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -9280,14 +9280,14 @@ tailmatch(PyObject *self,
PyUnicode_READY(substring) == -1)
return -1;
- if (PyUnicode_GET_LENGTH(substring) == 0)
- return 1;
-
ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self));
end -= PyUnicode_GET_LENGTH(substring);
if (end < start)
return 0;
+ if (PyUnicode_GET_LENGTH(substring) == 0)
+ return 1;
+
kind_self = PyUnicode_KIND(self);
data_self = PyUnicode_DATA(self);
kind_sub = PyUnicode_KIND(substring);