Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/string_tests.py

    r2 r391  
    66from test import test_support
    77from UserList import UserList
     8import _testcapi
    89
    910class Sequence:
     
    6364            object = subtype(object)
    6465            realresult = getattr(object, methodname)(*args)
    65             self.assert_(object is not realresult)
     66            self.assertTrue(object is not realresult)
    6667
    6768    # check that object.method(*args) raises exc
     
    186187        self.checkequal(-1, '', 'find', 'xx', sys.maxint, 0)
    187188
     189        # issue 7458
     190        self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
     191
    188192        # For a variety of combinations,
    189193        #    verify that str.find() matches __contains__
     
    206210                r1 = (loc != -1)
    207211                r2 = j in i
    208                 if r1 != r2:
    209                     self.assertEqual(r1, r2)
     212                self.assertEqual(r1, r2)
    210213                if loc != -1:
    211214                    self.assertEqual(i[loc:loc+len(j)], j)
     
    230233        self.checkraises(TypeError, 'hello', 'rfind')
    231234        self.checkraises(TypeError, 'hello', 'rfind', 42)
     235
     236        # For a variety of combinations,
     237        #    verify that str.rfind() matches __contains__
     238        #    and that the found substring is really at that location
     239        charset = ['', 'a', 'b', 'c']
     240        digits = 5
     241        base = len(charset)
     242        teststrings = set()
     243        for i in xrange(base ** digits):
     244            entry = []
     245            for j in xrange(digits):
     246                i, m = divmod(i, base)
     247                entry.append(charset[m])
     248            teststrings.add(''.join(entry))
     249        teststrings = list(teststrings)
     250        for i in teststrings:
     251            i = self.fixtype(i)
     252            for j in teststrings:
     253                loc = i.rfind(j)
     254                r1 = (loc != -1)
     255                r2 = j in i
     256                self.assertEqual(r1, r2)
     257                if loc != -1:
     258                    self.assertEqual(i[loc:loc+len(j)], self.fixtype(j))
     259
     260        # issue 7458
     261        self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
    232262
    233263    def test_index(self):
     
    687717        EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
    688718
    689         ba = buffer('a')
    690         bb = buffer('b')
     719        with test_support.check_py3k_warnings():
     720            ba = buffer('a')
     721            bb = buffer('b')
    691722        EQ("bbc", "abc", "replace", ba, bb)
    692723        EQ("aac", "abc", "replace", bb, ba)
     
    10841115        self.checkraises(ValueError, '%10', '__mod__', (42,))
    10851116
     1117        width = int(_testcapi.PY_SSIZE_T_MAX + 1)
     1118        if width <= sys.maxint:
     1119            self.checkraises(OverflowError, '%*s', '__mod__', (width, ''))
     1120        prec = int(_testcapi.INT_MAX + 1)
     1121        if prec <= sys.maxint:
     1122            self.checkraises(OverflowError, '%.*f', '__mod__', (prec, 1. / 7))
     1123        # Issue 15989
     1124        width = int(1 << (_testcapi.PY_SSIZE_T_MAX.bit_length() + 1))
     1125        if width <= sys.maxint:
     1126            self.checkraises(OverflowError, '%*s', '__mod__', (width, ''))
     1127        prec = int(_testcapi.UINT_MAX + 1)
     1128        if prec <= sys.maxint:
     1129            self.checkraises(OverflowError, '%.*f', '__mod__', (prec, 1. / 7))
     1130
     1131        class X(object): pass
     1132        self.checkraises(TypeError, 'abc', '__mod__', X())
     1133        class X(Exception):
     1134            def __getitem__(self, k):
     1135                return k
     1136        self.checkequal('melon apple', '%(melon)s %(apple)s', '__mod__', X())
     1137
    10861138    def test_floatformatting(self):
    10871139        # float formatting
     
    10901142            value = 0.01
    10911143            for x in xrange(60):
    1092                 value = value * 3.141592655 / 3.0 * 10.0
    1093                 # The formatfloat() code in stringobject.c and
    1094                 # unicodeobject.c uses a 120 byte buffer and switches from
    1095                 # 'f' formatting to 'g' at precision 50, so we expect
    1096                 # OverflowErrors for the ranges x < 50 and prec >= 67.
    1097                 if x < 50 and prec >= 67:
    1098                     self.checkraises(OverflowError, format, "__mod__", value)
    1099                 else:
    1100                     self.checkcall(format, "__mod__", value)
     1144                value = value * 3.14159265359 / 3.0 * 10.0
     1145                self.checkcall(format, "__mod__", value)
    11011146
    11021147    def test_inplace_rewrites(self):
     
    11541199        # mixed use of str and unicode
    11551200        self.assertEqual('a/b/c'.rpartition(u'/'), ('a/b', '/', 'c'))
     1201
     1202    def test_none_arguments(self):
     1203        # issue 11828
     1204        s = 'hello'
     1205        self.checkequal(2, s, 'find', 'l', None)
     1206        self.checkequal(3, s, 'find', 'l', -2, None)
     1207        self.checkequal(2, s, 'find', 'l', None, -2)
     1208        self.checkequal(0, s, 'find', 'h', None, None)
     1209
     1210        self.checkequal(3, s, 'rfind', 'l', None)
     1211        self.checkequal(3, s, 'rfind', 'l', -2, None)
     1212        self.checkequal(2, s, 'rfind', 'l', None, -2)
     1213        self.checkequal(0, s, 'rfind', 'h', None, None)
     1214
     1215        self.checkequal(2, s, 'index', 'l', None)
     1216        self.checkequal(3, s, 'index', 'l', -2, None)
     1217        self.checkequal(2, s, 'index', 'l', None, -2)
     1218        self.checkequal(0, s, 'index', 'h', None, None)
     1219
     1220        self.checkequal(3, s, 'rindex', 'l', None)
     1221        self.checkequal(3, s, 'rindex', 'l', -2, None)
     1222        self.checkequal(2, s, 'rindex', 'l', None, -2)
     1223        self.checkequal(0, s, 'rindex', 'h', None, None)
     1224
     1225        self.checkequal(2, s, 'count', 'l', None)
     1226        self.checkequal(1, s, 'count', 'l', -2, None)
     1227        self.checkequal(1, s, 'count', 'l', None, -2)
     1228        self.checkequal(0, s, 'count', 'x', None, None)
     1229
     1230        self.checkequal(True, s, 'endswith', 'o', None)
     1231        self.checkequal(True, s, 'endswith', 'lo', -2, None)
     1232        self.checkequal(True, s, 'endswith', 'l', None, -2)
     1233        self.checkequal(False, s, 'endswith', 'x', None, None)
     1234
     1235        self.checkequal(True, s, 'startswith', 'h', None)
     1236        self.checkequal(True, s, 'startswith', 'l', -2, None)
     1237        self.checkequal(True, s, 'startswith', 'h', None, -2)
     1238        self.checkequal(False, s, 'startswith', 'x', None, None)
     1239
     1240    def test_find_etc_raise_correct_error_messages(self):
     1241        # issue 11828
     1242        s = 'hello'
     1243        x = 'x'
     1244        self.assertRaisesRegexp(TypeError, r'\bfind\b', s.find,
     1245                                x, None, None, None)
     1246        self.assertRaisesRegexp(TypeError, r'\brfind\b', s.rfind,
     1247                                x, None, None, None)
     1248        self.assertRaisesRegexp(TypeError, r'\bindex\b', s.index,
     1249                                x, None, None, None)
     1250        self.assertRaisesRegexp(TypeError, r'\brindex\b', s.rindex,
     1251                                x, None, None, None)
     1252        self.assertRaisesRegexp(TypeError, r'^count\(', s.count,
     1253                                x, None, None, None)
     1254        self.assertRaisesRegexp(TypeError, r'^startswith\(', s.startswith,
     1255                                x, None, None, None)
     1256        self.assertRaisesRegexp(TypeError, r'^endswith\(', s.endswith,
     1257                                x, None, None, None)
    11561258
    11571259class MixinStrStringUserStringTest:
     
    12211323        s1 = subclass("abcd")
    12221324        s2 = t().join([s1])
    1223         self.assert_(s1 is not s2)
    1224         self.assert_(type(s2) is t)
     1325        self.assertTrue(s1 is not s2)
     1326        self.assertTrue(type(s2) is t)
    12251327
    12261328        s1 = t("abcd")
    12271329        s2 = t().join([s1])
    1228         self.assert_(s1 is s2)
     1330        self.assertTrue(s1 is s2)
    12291331
    12301332        # Should also test mixed-type join.
     
    12321334            s1 = subclass("abcd")
    12331335            s2 = "".join([s1])
    1234             self.assert_(s1 is not s2)
    1235             self.assert_(type(s2) is t)
     1336            self.assertTrue(s1 is not s2)
     1337            self.assertTrue(type(s2) is t)
    12361338
    12371339            s1 = t("abcd")
    12381340            s2 = "".join([s1])
    1239             self.assert_(s1 is s2)
     1341            self.assertTrue(s1 is s2)
    12401342
    12411343        elif t is str:
    12421344            s1 = subclass("abcd")
    12431345            s2 = u"".join([s1])
    1244             self.assert_(s1 is not s2)
    1245             self.assert_(type(s2) is unicode) # promotes!
     1346            self.assertTrue(s1 is not s2)
     1347            self.assertTrue(type(s2) is unicode) # promotes!
    12461348
    12471349            s1 = t("abcd")
    12481350            s2 = u"".join([s1])
    1249             self.assert_(s1 is not s2)
    1250             self.assert_(type(s2) is unicode) # promotes!
     1351            self.assertTrue(s1 is not s2)
     1352            self.assertTrue(type(s2) is unicode) # promotes!
    12511353
    12521354        else:
Note: See TracChangeset for help on using the changeset viewer.