Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a29bd5

Browse files
committed
More on bug 460020: disable many optimizations of unicode subclasses.
1 parent 8fa5dd0 commit 7a29bd5

2 files changed

Lines changed: 66 additions & 12 deletions

File tree

Lib/test/test_descr.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,13 +1486,25 @@ def rev(self):
14861486
verify(str(s) == base)
14871487
verify(str(s).__class__ is str)
14881488
verify((s + "").__class__ is str)
1489+
verify(s + "" == base)
14891490
verify(("" + s).__class__ is str)
1491+
verify("" + s == base)
14901492
verify((s * 0).__class__ is str)
1493+
verify(s * 0 == "")
14911494
verify((s * 1).__class__ is str)
1495+
verify(s * 1 == base)
14921496
verify((s * 2).__class__ is str)
1497+
verify(s * 2 == base + base)
14931498
verify(s[:].__class__ is str)
1499+
verify(s[:] == base)
14941500
verify(s[0:0].__class__ is str)
1501+
verify(s[0:0] == "")
14951502
verify(s.strip().__class__ is str)
1503+
verify(s.strip() == base)
1504+
verify(s.lstrip().__class__ is str)
1505+
verify(s.lstrip() == base)
1506+
verify(s.rstrip().__class__ is str)
1507+
verify(s.rstrip() == base)
14961508
identitytab = ''.join([chr(i) for i in range(256)])
14971509
verify(s.translate(identitytab).__class__ is str)
14981510
verify(s.translate(identitytab) == base)
@@ -1507,6 +1519,8 @@ def rev(self):
15071519
verify(s.rjust(len(s)) == base)
15081520
verify(s.center(len(s)).__class__ is str)
15091521
verify(s.center(len(s)) == base)
1522+
verify(s.lower().__class__ is str)
1523+
verify(s.lower() == base)
15101524

15111525
class madunicode(unicode):
15121526
_rev = None
@@ -1520,9 +1534,48 @@ def rev(self):
15201534
u = madunicode("ABCDEF")
15211535
verify(u.rev() == madunicode(u"FEDCBA"))
15221536
verify(u.rev().rev() == madunicode(u"ABCDEF"))
1523-
u = madunicode(u"12345")
1524-
verify(unicode(u) == u"12345")
1537+
base = u"12345"
1538+
u = madunicode(base)
1539+
verify(unicode(u) == base)
15251540
verify(unicode(u).__class__ is unicode)
1541+
verify(u.strip().__class__ is unicode)
1542+
verify(u.strip() == base)
1543+
verify(u.lstrip().__class__ is unicode)
1544+
verify(u.lstrip() == base)
1545+
verify(u.rstrip().__class__ is unicode)
1546+
verify(u.rstrip() == base)
1547+
verify(u.replace(u"x", u"x").__class__ is unicode)
1548+
verify(u.replace(u"x", u"x") == base)
1549+
verify(u.replace(u"xy", u"xy").__class__ is unicode)
1550+
verify(u.replace(u"xy", u"xy") == base)
1551+
verify(u.center(len(u)).__class__ is unicode)
1552+
verify(u.center(len(u)) == base)
1553+
verify(u.ljust(len(u)).__class__ is unicode)
1554+
verify(u.ljust(len(u)) == base)
1555+
verify(u.rjust(len(u)).__class__ is unicode)
1556+
verify(u.rjust(len(u)) == base)
1557+
verify(u.lower().__class__ is unicode)
1558+
verify(u.lower() == base)
1559+
verify(u.upper().__class__ is unicode)
1560+
verify(u.upper() == base)
1561+
verify(u.capitalize().__class__ is unicode)
1562+
verify(u.capitalize() == base)
1563+
verify(u.title().__class__ is unicode)
1564+
verify(u.title() == base)
1565+
verify((u + u"").__class__ is unicode)
1566+
verify(u + u"" == base)
1567+
verify((u"" + u).__class__ is unicode)
1568+
verify(u"" + u == base)
1569+
verify((u * 0).__class__ is unicode)
1570+
verify(u * 0 == u"")
1571+
verify((u * 1).__class__ is unicode)
1572+
verify(u * 1 == base)
1573+
verify((u * 2).__class__ is unicode)
1574+
verify(u * 2 == base + base)
1575+
verify(u[:].__class__ is unicode)
1576+
verify(u[:] == base)
1577+
verify(u[0:0].__class__ is unicode)
1578+
verify(u[0:0] == u"")
15261579

15271580
def all():
15281581
lists()

Objects/unicodeobject.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,7 +2703,7 @@ PyObject *fixup(PyUnicodeObject *self,
27032703

27042704
Py_UNICODE_COPY(u->str, self->str, self->length);
27052705

2706-
if (!fixfct(u)) {
2706+
if (!fixfct(u) && PyUnicode_CheckExact(self)) {
27072707
/* fixfct should return TRUE if it modified the buffer. If
27082708
FALSE, return a reference to the original buffer instead
27092709
(to save space, not time) */
@@ -2934,7 +2934,7 @@ PyUnicodeObject *pad(PyUnicodeObject *self,
29342934
if (right < 0)
29352935
right = 0;
29362936

2937-
if (left == 0 && right == 0) {
2937+
if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) {
29382938
Py_INCREF(self);
29392939
return self;
29402940
}
@@ -3161,7 +3161,7 @@ PyObject *strip(PyUnicodeObject *self,
31613161
while (end > start && Py_UNICODE_ISSPACE(p[end-1]))
31623162
end--;
31633163

3164-
if (start == 0 && end == self->length) {
3164+
if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) {
31653165
/* couldn't strip anything off, return original string */
31663166
Py_INCREF(self);
31673167
return (PyObject*) self;
@@ -3188,7 +3188,8 @@ PyObject *replace(PyUnicodeObject *self,
31883188
int i;
31893189

31903190
/* replace characters */
3191-
if (!findchar(self->str, self->length, str1->str[0])) {
3191+
if (!findchar(self->str, self->length, str1->str[0]) &&
3192+
PyUnicode_CheckExact(self)) {
31923193
/* nothing to replace, return original string */
31933194
Py_INCREF(self);
31943195
u = self;
@@ -3220,7 +3221,7 @@ PyObject *replace(PyUnicodeObject *self,
32203221
n = count(self, 0, self->length, str1);
32213222
if (n > maxcount)
32223223
n = maxcount;
3223-
if (n == 0) {
3224+
if (n == 0 && PyUnicode_CheckExact(self)) {
32243225
/* nothing to replace, return original string */
32253226
Py_INCREF(self);
32263227
u = self;
@@ -3329,7 +3330,7 @@ unicode_center(PyUnicodeObject *self, PyObject *args)
33293330
if (!PyArg_ParseTuple(args, "i:center", &width))
33303331
return NULL;
33313332

3332-
if (self->length >= width) {
3333+
if (self->length >= width && PyUnicode_CheckExact(self)) {
33333334
Py_INCREF(self);
33343335
return (PyObject*) self;
33353336
}
@@ -4085,7 +4086,7 @@ unicode_ljust(PyUnicodeObject *self, PyObject *args)
40854086
if (!PyArg_ParseTuple(args, "i:ljust", &width))
40864087
return NULL;
40874088

4088-
if (self->length >= width) {
4089+
if (self->length >= width && PyUnicode_CheckExact(self)) {
40894090
Py_INCREF(self);
40904091
return (PyObject*) self;
40914092
}
@@ -4126,7 +4127,7 @@ unicode_repeat(PyUnicodeObject *str, int len)
41264127
if (len < 0)
41274128
len = 0;
41284129

4129-
if (len == 1) {
4130+
if (len == 1 && PyUnicode_CheckExact(str)) {
41304131
/* no repeat, return original string */
41314132
Py_INCREF(str);
41324133
return (PyObject*) str;
@@ -4309,7 +4310,7 @@ unicode_rjust(PyUnicodeObject *self, PyObject *args)
43094310
if (!PyArg_ParseTuple(args, "i:rjust", &width))
43104311
return NULL;
43114312

4312-
if (self->length >= width) {
4313+
if (self->length >= width && PyUnicode_CheckExact(self)) {
43134314
Py_INCREF(self);
43144315
return (PyObject*) self;
43154316
}
@@ -4338,7 +4339,7 @@ unicode_slice(PyUnicodeObject *self, int start, int end)
43384339
end = 0;
43394340
if (end > self->length)
43404341
end = self->length;
4341-
if (start == 0 && end == self->length) {
4342+
if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) {
43424343
/* full slice, return original string */
43434344
Py_INCREF(self);
43444345
return (PyObject*) self;

0 commit comments

Comments
 (0)