From 7a29bd58614da9fc478d7167ba918d92c2dcca7e Mon Sep 17 00:00:00 2001 From: Tim Peters Date: Wed, 12 Sep 2001 03:03:31 +0000 Subject: [PATCH] More on bug 460020: disable many optimizations of unicode subclasses. --- Lib/test/test_descr.py | 57 +++++++++++++++++++++++++++++++++++++++-- Objects/unicodeobject.c | 21 +++++++-------- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index a29eb2312c5..b791037aa58 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1486,13 +1486,25 @@ def inherits(): verify(str(s) == base) verify(str(s).__class__ is str) verify((s + "").__class__ is str) + verify(s + "" == base) verify(("" + s).__class__ is str) + verify("" + s == base) verify((s * 0).__class__ is str) + verify(s * 0 == "") verify((s * 1).__class__ is str) + verify(s * 1 == base) verify((s * 2).__class__ is str) + verify(s * 2 == base + base) verify(s[:].__class__ is str) + verify(s[:] == base) verify(s[0:0].__class__ is str) + verify(s[0:0] == "") verify(s.strip().__class__ is str) + verify(s.strip() == base) + verify(s.lstrip().__class__ is str) + verify(s.lstrip() == base) + verify(s.rstrip().__class__ is str) + verify(s.rstrip() == base) identitytab = ''.join([chr(i) for i in range(256)]) verify(s.translate(identitytab).__class__ is str) verify(s.translate(identitytab) == base) @@ -1507,6 +1519,8 @@ def inherits(): verify(s.rjust(len(s)) == base) verify(s.center(len(s)).__class__ is str) verify(s.center(len(s)) == base) + verify(s.lower().__class__ is str) + verify(s.lower() == base) class madunicode(unicode): _rev = None @@ -1520,9 +1534,48 @@ def inherits(): u = madunicode("ABCDEF") verify(u.rev() == madunicode(u"FEDCBA")) verify(u.rev().rev() == madunicode(u"ABCDEF")) - u = madunicode(u"12345") - verify(unicode(u) == u"12345") + base = u"12345" + u = madunicode(base) + verify(unicode(u) == base) verify(unicode(u).__class__ is unicode) + verify(u.strip().__class__ is unicode) + verify(u.strip() == base) + verify(u.lstrip().__class__ is unicode) + verify(u.lstrip() == base) + verify(u.rstrip().__class__ is unicode) + verify(u.rstrip() == base) + verify(u.replace(u"x", u"x").__class__ is unicode) + verify(u.replace(u"x", u"x") == base) + verify(u.replace(u"xy", u"xy").__class__ is unicode) + verify(u.replace(u"xy", u"xy") == base) + verify(u.center(len(u)).__class__ is unicode) + verify(u.center(len(u)) == base) + verify(u.ljust(len(u)).__class__ is unicode) + verify(u.ljust(len(u)) == base) + verify(u.rjust(len(u)).__class__ is unicode) + verify(u.rjust(len(u)) == base) + verify(u.lower().__class__ is unicode) + verify(u.lower() == base) + verify(u.upper().__class__ is unicode) + verify(u.upper() == base) + verify(u.capitalize().__class__ is unicode) + verify(u.capitalize() == base) + verify(u.title().__class__ is unicode) + verify(u.title() == base) + verify((u + u"").__class__ is unicode) + verify(u + u"" == base) + verify((u"" + u).__class__ is unicode) + verify(u"" + u == base) + verify((u * 0).__class__ is unicode) + verify(u * 0 == u"") + verify((u * 1).__class__ is unicode) + verify(u * 1 == base) + verify((u * 2).__class__ is unicode) + verify(u * 2 == base + base) + verify(u[:].__class__ is unicode) + verify(u[:] == base) + verify(u[0:0].__class__ is unicode) + verify(u[0:0] == u"") def all(): lists() diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index a50b9256c91..c5912b51676 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -2703,7 +2703,7 @@ PyObject *fixup(PyUnicodeObject *self, Py_UNICODE_COPY(u->str, self->str, self->length); - if (!fixfct(u)) { + if (!fixfct(u) && PyUnicode_CheckExact(self)) { /* fixfct should return TRUE if it modified the buffer. If FALSE, return a reference to the original buffer instead (to save space, not time) */ @@ -2934,7 +2934,7 @@ PyUnicodeObject *pad(PyUnicodeObject *self, if (right < 0) right = 0; - if (left == 0 && right == 0) { + if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { Py_INCREF(self); return self; } @@ -3161,7 +3161,7 @@ PyObject *strip(PyUnicodeObject *self, while (end > start && Py_UNICODE_ISSPACE(p[end-1])) end--; - if (start == 0 && end == self->length) { + if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { /* couldn't strip anything off, return original string */ Py_INCREF(self); return (PyObject*) self; @@ -3188,7 +3188,8 @@ PyObject *replace(PyUnicodeObject *self, int i; /* replace characters */ - if (!findchar(self->str, self->length, str1->str[0])) { + if (!findchar(self->str, self->length, str1->str[0]) && + PyUnicode_CheckExact(self)) { /* nothing to replace, return original string */ Py_INCREF(self); u = self; @@ -3220,7 +3221,7 @@ PyObject *replace(PyUnicodeObject *self, n = count(self, 0, self->length, str1); if (n > maxcount) n = maxcount; - if (n == 0) { + if (n == 0 && PyUnicode_CheckExact(self)) { /* nothing to replace, return original string */ Py_INCREF(self); u = self; @@ -3329,7 +3330,7 @@ unicode_center(PyUnicodeObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "i:center", &width)) return NULL; - if (self->length >= width) { + if (self->length >= width && PyUnicode_CheckExact(self)) { Py_INCREF(self); return (PyObject*) self; } @@ -4085,7 +4086,7 @@ unicode_ljust(PyUnicodeObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "i:ljust", &width)) return NULL; - if (self->length >= width) { + if (self->length >= width && PyUnicode_CheckExact(self)) { Py_INCREF(self); return (PyObject*) self; } @@ -4126,7 +4127,7 @@ unicode_repeat(PyUnicodeObject *str, int len) if (len < 0) len = 0; - if (len == 1) { + if (len == 1 && PyUnicode_CheckExact(str)) { /* no repeat, return original string */ Py_INCREF(str); return (PyObject*) str; @@ -4309,7 +4310,7 @@ unicode_rjust(PyUnicodeObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "i:rjust", &width)) return NULL; - if (self->length >= width) { + if (self->length >= width && PyUnicode_CheckExact(self)) { Py_INCREF(self); return (PyObject*) self; } @@ -4338,7 +4339,7 @@ unicode_slice(PyUnicodeObject *self, int start, int end) end = 0; if (end > self->length) end = self->length; - if (start == 0 && end == self->length) { + if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { /* full slice, return original string */ Py_INCREF(self); return (PyObject*) self;