More on bug 460020: disable many optimizations of unicode subclasses.
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index a29eb23..b791037 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1486,13 +1486,25 @@
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 @@
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 @@
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 a50b925..c5912b5 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -2703,7 +2703,7 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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;