Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 1 | # Python test set -- part 6, built-in types |
| 2 | |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 3 | from test.support import run_with_locale |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4 | import collections.abc |
| 5 | import inspect |
Eric Snow | b5c8f92 | 2013-02-16 16:32:39 -0700 | [diff] [blame] | 6 | import pickle |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 7 | import locale |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 8 | import sys |
| 9 | import types |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 10 | import unittest.mock |
| 11 | import weakref |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 12 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 13 | class TypesTests(unittest.TestCase): |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 14 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 15 | def test_truth_values(self): |
| 16 | if None: self.fail('None is true instead of false') |
| 17 | if 0: self.fail('0 is true instead of false') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 18 | if 0.0: self.fail('0.0 is true instead of false') |
| 19 | if '': self.fail('\'\' is true instead of false') |
| 20 | if not 1: self.fail('1 is false instead of true') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 21 | if not 1.0: self.fail('1.0 is false instead of true') |
| 22 | if not 'x': self.fail('\'x\' is false instead of true') |
| 23 | if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true') |
| 24 | def f(): pass |
| 25 | class C: pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 26 | x = C() |
| 27 | if not f: self.fail('f is false instead of true') |
| 28 | if not C: self.fail('C is false instead of true') |
| 29 | if not sys: self.fail('sys is false instead of true') |
| 30 | if not x: self.fail('x is false instead of true') |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 31 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 32 | def test_boolean_ops(self): |
| 33 | if 0 or 0: self.fail('0 or 0 is true instead of false') |
| 34 | if 1 and 1: pass |
| 35 | else: self.fail('1 and 1 is false instead of true') |
| 36 | if not 1: self.fail('not 1 is true instead of false') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 37 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 38 | def test_comparisons(self): |
| 39 | if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass |
| 40 | else: self.fail('int comparisons failed') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 41 | if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass |
| 42 | else: self.fail('float comparisons failed') |
| 43 | if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass |
| 44 | else: self.fail('string comparisons failed') |
| 45 | if None is None: pass |
| 46 | else: self.fail('identity test failed') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 47 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 48 | def test_float_constructor(self): |
| 49 | self.assertRaises(ValueError, float, '') |
| 50 | self.assertRaises(ValueError, float, '5\0') |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 51 | self.assertRaises(ValueError, float, '5_5\0') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 52 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 53 | def test_zero_division(self): |
| 54 | try: 5.0 / 0.0 |
| 55 | except ZeroDivisionError: pass |
| 56 | else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 58 | try: 5.0 // 0.0 |
| 59 | except ZeroDivisionError: pass |
| 60 | else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 62 | try: 5.0 % 0.0 |
| 63 | except ZeroDivisionError: pass |
| 64 | else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 66 | try: 5 / 0 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 67 | except ZeroDivisionError: pass |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 68 | else: self.fail("5 / 0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 70 | try: 5 // 0 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 71 | except ZeroDivisionError: pass |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 72 | else: self.fail("5 // 0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 74 | try: 5 % 0 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 75 | except ZeroDivisionError: pass |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 76 | else: self.fail("5 % 0 didn't raise ZeroDivisionError") |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 78 | def test_numeric_types(self): |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 79 | if 0 != 0.0 or 1 != 1.0 or -1 != -1.0: |
| 80 | self.fail('int/float value not equal') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 81 | # calling built-in types without argument must return 0 |
| 82 | if int() != 0: self.fail('int() does not return 0') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 83 | if float() != 0.0: self.fail('float() does not return 0.0') |
| 84 | if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass |
| 85 | else: self.fail('int() does not round properly') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 86 | if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass |
| 87 | else: self.fail('float() does not work properly') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 88 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 89 | def test_float_to_string(self): |
| 90 | def test(f, result): |
| 91 | self.assertEqual(f.__format__('e'), result) |
| 92 | self.assertEqual('%e' % f, result) |
| 93 | |
| 94 | # test all 2 digit exponents, both with __format__ and with |
| 95 | # '%' formatting |
| 96 | for i in range(-99, 100): |
| 97 | test(float('1.5e'+str(i)), '1.500000e{0:+03d}'.format(i)) |
| 98 | |
| 99 | # test some 3 digit exponents |
| 100 | self.assertEqual(1.5e100.__format__('e'), '1.500000e+100') |
| 101 | self.assertEqual('%e' % 1.5e100, '1.500000e+100') |
| 102 | |
| 103 | self.assertEqual(1.5e101.__format__('e'), '1.500000e+101') |
| 104 | self.assertEqual('%e' % 1.5e101, '1.500000e+101') |
| 105 | |
| 106 | self.assertEqual(1.5e-100.__format__('e'), '1.500000e-100') |
| 107 | self.assertEqual('%e' % 1.5e-100, '1.500000e-100') |
| 108 | |
| 109 | self.assertEqual(1.5e-101.__format__('e'), '1.500000e-101') |
| 110 | self.assertEqual('%e' % 1.5e-101, '1.500000e-101') |
| 111 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 112 | self.assertEqual('%g' % 1.0, '1') |
| 113 | self.assertEqual('%#g' % 1.0, '1.00000') |
| 114 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 115 | def test_normal_integers(self): |
| 116 | # Ensure the first 256 integers are shared |
| 117 | a = 256 |
| 118 | b = 128*2 |
| 119 | if a is not b: self.fail('256 is not shared') |
| 120 | if 12 + 24 != 36: self.fail('int op') |
| 121 | if 12 + (-24) != -12: self.fail('int op') |
| 122 | if (-12) + 24 != 12: self.fail('int op') |
| 123 | if (-12) + (-24) != -36: self.fail('int op') |
| 124 | if not 12 < 24: self.fail('int op') |
| 125 | if not -24 < -12: self.fail('int op') |
| 126 | # Test for a particular bug in integer multiply |
| 127 | xsize, ysize, zsize = 238, 356, 4 |
| 128 | if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912): |
| 129 | self.fail('int mul commutativity') |
| 130 | # And another. |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 131 | m = -sys.maxsize - 1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 132 | for divisor in 1, 2, 4, 8, 16, 32: |
| 133 | j = m // divisor |
| 134 | prod = divisor * j |
| 135 | if prod != m: |
| 136 | self.fail("%r * %r == %r != %r" % (divisor, j, prod, m)) |
| 137 | if type(prod) is not int: |
| 138 | self.fail("expected type(prod) to be int, not %r" % |
| 139 | type(prod)) |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 140 | # Check for unified integral type |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 141 | for divisor in 1, 2, 4, 8, 16, 32: |
| 142 | j = m // divisor - 1 |
| 143 | prod = divisor * j |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 144 | if type(prod) is not int: |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 145 | self.fail("expected type(%r) to be int, not %r" % |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 146 | (prod, type(prod))) |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 147 | # Check for unified integral type |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 148 | m = sys.maxsize |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 149 | for divisor in 1, 2, 4, 8, 16, 32: |
| 150 | j = m // divisor + 1 |
| 151 | prod = divisor * j |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 152 | if type(prod) is not int: |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 153 | self.fail("expected type(%r) to be int, not %r" % |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 154 | (prod, type(prod))) |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 155 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 156 | x = sys.maxsize |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 157 | self.assertIsInstance(x + 1, int, |
| 158 | "(sys.maxsize + 1) should have returned int") |
| 159 | self.assertIsInstance(-x - 1, int, |
| 160 | "(-sys.maxsize - 1) should have returned int") |
| 161 | self.assertIsInstance(-x - 2, int, |
| 162 | "(-sys.maxsize - 2) should have returned int") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 163 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 164 | try: 5 << -5 |
| 165 | except ValueError: pass |
| 166 | else: self.fail('int negative shift <<') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 167 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 168 | try: 5 >> -5 |
| 169 | except ValueError: pass |
| 170 | else: self.fail('int negative shift >>') |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 171 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 172 | def test_floats(self): |
| 173 | if 12.0 + 24.0 != 36.0: self.fail('float op') |
| 174 | if 12.0 + (-24.0) != -12.0: self.fail('float op') |
| 175 | if (-12.0) + 24.0 != 12.0: self.fail('float op') |
| 176 | if (-12.0) + (-24.0) != -36.0: self.fail('float op') |
| 177 | if not 12.0 < 24.0: self.fail('float op') |
| 178 | if not -24.0 < -12.0: self.fail('float op') |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 179 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 180 | def test_strings(self): |
| 181 | if len('') != 0: self.fail('len(\'\')') |
| 182 | if len('a') != 1: self.fail('len(\'a\')') |
| 183 | if len('abcdef') != 6: self.fail('len(\'abcdef\')') |
| 184 | if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation') |
| 185 | if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3') |
| 186 | if 0*'abcde' != '': self.fail('string repetition 0*') |
| 187 | if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string') |
| 188 | if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass |
| 189 | else: self.fail('in/not in string') |
| 190 | x = 'x'*103 |
| 191 | if '%s!'%x != x+'!': self.fail('nasty string formatting bug') |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 192 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 193 | #extended slices for strings |
| 194 | a = '0123456789' |
| 195 | self.assertEqual(a[::], a) |
| 196 | self.assertEqual(a[::2], '02468') |
| 197 | self.assertEqual(a[1::2], '13579') |
| 198 | self.assertEqual(a[::-1],'9876543210') |
| 199 | self.assertEqual(a[::-2], '97531') |
| 200 | self.assertEqual(a[3::-2], '31') |
| 201 | self.assertEqual(a[-100:100:], a) |
| 202 | self.assertEqual(a[100:-100:-1], a[::-1]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 203 | self.assertEqual(a[-100:100:2], '02468') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 204 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 205 | def test_type_function(self): |
| 206 | self.assertRaises(TypeError, type, 1, 2) |
| 207 | self.assertRaises(TypeError, type, 1, 2, 3, 4) |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 208 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 209 | def test_int__format__(self): |
| 210 | def test(i, format_spec, result): |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 211 | # just make sure we have the unified type for integers |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 212 | assert type(i) == int |
| 213 | assert type(format_spec) == str |
| 214 | self.assertEqual(i.__format__(format_spec), result) |
| 215 | |
| 216 | test(123456789, 'd', '123456789') |
| 217 | test(123456789, 'd', '123456789') |
| 218 | |
| 219 | test(1, 'c', '\01') |
| 220 | |
| 221 | # sign and aligning are interdependent |
| 222 | test(1, "-", '1') |
| 223 | test(-1, "-", '-1') |
| 224 | test(1, "-3", ' 1') |
| 225 | test(-1, "-3", ' -1') |
| 226 | test(1, "+3", ' +1') |
| 227 | test(-1, "+3", ' -1') |
| 228 | test(1, " 3", ' 1') |
| 229 | test(-1, " 3", ' -1') |
| 230 | test(1, " ", ' 1') |
| 231 | test(-1, " ", '-1') |
| 232 | |
| 233 | # hex |
| 234 | test(3, "x", "3") |
| 235 | test(3, "X", "3") |
| 236 | test(1234, "x", "4d2") |
| 237 | test(-1234, "x", "-4d2") |
| 238 | test(1234, "8x", " 4d2") |
| 239 | test(-1234, "8x", " -4d2") |
| 240 | test(1234, "x", "4d2") |
| 241 | test(-1234, "x", "-4d2") |
| 242 | test(-3, "x", "-3") |
| 243 | test(-3, "X", "-3") |
| 244 | test(int('be', 16), "x", "be") |
| 245 | test(int('be', 16), "X", "BE") |
| 246 | test(-int('be', 16), "x", "-be") |
| 247 | test(-int('be', 16), "X", "-BE") |
| 248 | |
| 249 | # octal |
| 250 | test(3, "o", "3") |
| 251 | test(-3, "o", "-3") |
| 252 | test(65, "o", "101") |
| 253 | test(-65, "o", "-101") |
| 254 | test(1234, "o", "2322") |
| 255 | test(-1234, "o", "-2322") |
| 256 | test(1234, "-o", "2322") |
| 257 | test(-1234, "-o", "-2322") |
| 258 | test(1234, " o", " 2322") |
| 259 | test(-1234, " o", "-2322") |
| 260 | test(1234, "+o", "+2322") |
| 261 | test(-1234, "+o", "-2322") |
| 262 | |
| 263 | # binary |
| 264 | test(3, "b", "11") |
| 265 | test(-3, "b", "-11") |
| 266 | test(1234, "b", "10011010010") |
| 267 | test(-1234, "b", "-10011010010") |
| 268 | test(1234, "-b", "10011010010") |
| 269 | test(-1234, "-b", "-10011010010") |
| 270 | test(1234, " b", " 10011010010") |
| 271 | test(-1234, " b", "-10011010010") |
| 272 | test(1234, "+b", "+10011010010") |
| 273 | test(-1234, "+b", "-10011010010") |
| 274 | |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 275 | # alternate (#) formatting |
| 276 | test(0, "#b", '0b0') |
| 277 | test(0, "-#b", '0b0') |
| 278 | test(1, "-#b", '0b1') |
| 279 | test(-1, "-#b", '-0b1') |
| 280 | test(-1, "-#5b", ' -0b1') |
| 281 | test(1, "+#5b", ' +0b1') |
| 282 | test(100, "+#b", '+0b1100100') |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 283 | test(100, "#012b", '0b0001100100') |
| 284 | test(-100, "#012b", '-0b001100100') |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 285 | |
| 286 | test(0, "#o", '0o0') |
| 287 | test(0, "-#o", '0o0') |
| 288 | test(1, "-#o", '0o1') |
| 289 | test(-1, "-#o", '-0o1') |
| 290 | test(-1, "-#5o", ' -0o1') |
| 291 | test(1, "+#5o", ' +0o1') |
| 292 | test(100, "+#o", '+0o144') |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 293 | test(100, "#012o", '0o0000000144') |
| 294 | test(-100, "#012o", '-0o000000144') |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 295 | |
| 296 | test(0, "#x", '0x0') |
| 297 | test(0, "-#x", '0x0') |
| 298 | test(1, "-#x", '0x1') |
| 299 | test(-1, "-#x", '-0x1') |
| 300 | test(-1, "-#5x", ' -0x1') |
| 301 | test(1, "+#5x", ' +0x1') |
| 302 | test(100, "+#x", '+0x64') |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 303 | test(100, "#012x", '0x0000000064') |
| 304 | test(-100, "#012x", '-0x000000064') |
| 305 | test(123456, "#012x", '0x000001e240') |
| 306 | test(-123456, "#012x", '-0x00001e240') |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 307 | |
| 308 | test(0, "#X", '0X0') |
| 309 | test(0, "-#X", '0X0') |
| 310 | test(1, "-#X", '0X1') |
| 311 | test(-1, "-#X", '-0X1') |
| 312 | test(-1, "-#5X", ' -0X1') |
| 313 | test(1, "+#5X", ' +0X1') |
| 314 | test(100, "+#X", '+0X64') |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 315 | test(100, "#012X", '0X0000000064') |
| 316 | test(-100, "#012X", '-0X000000064') |
| 317 | test(123456, "#012X", '0X000001E240') |
| 318 | test(-123456, "#012X", '-0X00001E240') |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 319 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 320 | test(123, ',', '123') |
| 321 | test(-123, ',', '-123') |
| 322 | test(1234, ',', '1,234') |
| 323 | test(-1234, ',', '-1,234') |
| 324 | test(123456, ',', '123,456') |
| 325 | test(-123456, ',', '-123,456') |
| 326 | test(1234567, ',', '1,234,567') |
| 327 | test(-1234567, ',', '-1,234,567') |
| 328 | |
Eric Smith | 937491d | 2009-04-22 17:04:27 +0000 | [diff] [blame] | 329 | # issue 5782, commas with no specifier type |
| 330 | test(1234, '010,', '00,001,234') |
| 331 | |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 332 | # Unified type for integers |
| 333 | test(10**100, 'd', '1' + '0' * 100) |
| 334 | test(10**100+100, 'd', '1' + '0' * 97 + '100') |
| 335 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 336 | # make sure these are errors |
| 337 | |
| 338 | # precision disallowed |
| 339 | self.assertRaises(ValueError, 3 .__format__, "1.3") |
| 340 | # sign not allowed with 'c' |
| 341 | self.assertRaises(ValueError, 3 .__format__, "+c") |
| 342 | # format spec must be string |
| 343 | self.assertRaises(TypeError, 3 .__format__, None) |
| 344 | self.assertRaises(TypeError, 3 .__format__, 0) |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 345 | # can't have ',' with 'n' |
| 346 | self.assertRaises(ValueError, 3 .__format__, ",n") |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 347 | # can't have ',' with 'c' |
| 348 | self.assertRaises(ValueError, 3 .__format__, ",c") |
Eric V. Smith | a12572f | 2014-04-15 22:37:55 -0400 | [diff] [blame] | 349 | # can't have '#' with 'c' |
| 350 | self.assertRaises(ValueError, 3 .__format__, "#c") |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 351 | |
| 352 | # ensure that only int and float type specifiers work |
| 353 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 354 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
| 355 | if not format_spec in 'bcdoxXeEfFgGn%': |
| 356 | self.assertRaises(ValueError, 0 .__format__, format_spec) |
| 357 | self.assertRaises(ValueError, 1 .__format__, format_spec) |
| 358 | self.assertRaises(ValueError, (-1) .__format__, format_spec) |
| 359 | |
| 360 | # ensure that float type specifiers work; format converts |
| 361 | # the int to a float |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 362 | for format_spec in 'eEfFgG%': |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 363 | for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]: |
| 364 | self.assertEqual(value.__format__(format_spec), |
| 365 | float(value).__format__(format_spec)) |
| 366 | |
Eric Smith | abb28c6 | 2010-02-23 00:22:24 +0000 | [diff] [blame] | 367 | # Issue 6902 |
| 368 | test(123456, "0<20", '12345600000000000000') |
| 369 | test(123456, "1<20", '12345611111111111111') |
| 370 | test(123456, "*<20", '123456**************') |
| 371 | test(123456, "0>20", '00000000000000123456') |
| 372 | test(123456, "1>20", '11111111111111123456') |
| 373 | test(123456, "*>20", '**************123456') |
| 374 | test(123456, "0=20", '00000000000000123456') |
| 375 | test(123456, "1=20", '11111111111111123456') |
| 376 | test(123456, "*=20", '**************123456') |
| 377 | |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 378 | @run_with_locale('LC_NUMERIC', 'en_US.UTF8') |
| 379 | def test_float__format__locale(self): |
| 380 | # test locale support for __format__ code 'n' |
| 381 | |
| 382 | for i in range(-10, 10): |
| 383 | x = 1234567890.0 * (10.0 ** i) |
Garvit Khatri | 1cf93a7 | 2017-03-28 23:43:38 +0800 | [diff] [blame] | 384 | self.assertEqual(locale.format_string('%g', x, grouping=True), format(x, 'n')) |
| 385 | self.assertEqual(locale.format_string('%.10g', x, grouping=True), format(x, '.10n')) |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 386 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 387 | @run_with_locale('LC_NUMERIC', 'en_US.UTF8') |
| 388 | def test_int__format__locale(self): |
| 389 | # test locale support for __format__ code 'n' for integers |
| 390 | |
| 391 | x = 123456789012345678901234567890 |
| 392 | for i in range(0, 30): |
Garvit Khatri | 1cf93a7 | 2017-03-28 23:43:38 +0800 | [diff] [blame] | 393 | self.assertEqual(locale.format_string('%d', x, grouping=True), format(x, 'n')) |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 394 | |
| 395 | # move to the next integer to test |
| 396 | x = x // 10 |
| 397 | |
Eric Smith | b151a45 | 2008-06-24 11:21:04 +0000 | [diff] [blame] | 398 | rfmt = ">20n" |
| 399 | lfmt = "<20n" |
| 400 | cfmt = "^20n" |
| 401 | for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900): |
| 402 | self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt))) |
| 403 | self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt))) |
| 404 | self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt))) |
| 405 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 406 | def test_float__format__(self): |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 407 | def test(f, format_spec, result): |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 408 | self.assertEqual(f.__format__(format_spec), result) |
Eric Smith | 984bb58 | 2010-11-25 16:08:06 +0000 | [diff] [blame] | 409 | self.assertEqual(format(f, format_spec), result) |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 410 | |
| 411 | test(0.0, 'f', '0.000000') |
| 412 | |
| 413 | # the default is 'g', except for empty format spec |
| 414 | test(0.0, '', '0.0') |
| 415 | test(0.01, '', '0.01') |
| 416 | test(0.01, 'g', '0.01') |
| 417 | |
Eric Smith | 2ad79e8 | 2008-07-19 00:33:23 +0000 | [diff] [blame] | 418 | # test for issue 3411 |
| 419 | test(1.23, '1', '1.23') |
| 420 | test(-1.23, '1', '-1.23') |
| 421 | test(1.23, '1g', '1.23') |
| 422 | test(-1.23, '1g', '-1.23') |
| 423 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 424 | test( 1.0, ' g', ' 1') |
| 425 | test(-1.0, ' g', '-1') |
| 426 | test( 1.0, '+g', '+1') |
| 427 | test(-1.0, '+g', '-1') |
| 428 | test(1.1234e200, 'g', '1.1234e+200') |
| 429 | test(1.1234e200, 'G', '1.1234E+200') |
| 430 | |
| 431 | |
| 432 | test(1.0, 'f', '1.000000') |
| 433 | |
| 434 | test(-1.0, 'f', '-1.000000') |
| 435 | |
| 436 | test( 1.0, ' f', ' 1.000000') |
| 437 | test(-1.0, ' f', '-1.000000') |
| 438 | test( 1.0, '+f', '+1.000000') |
| 439 | test(-1.0, '+f', '-1.000000') |
Mark Dickinson | 33841c3 | 2009-05-01 15:37:04 +0000 | [diff] [blame] | 440 | |
| 441 | # Python versions <= 3.0 switched from 'f' to 'g' formatting for |
| 442 | # values larger than 1e50. No longer. |
| 443 | f = 1.1234e90 |
| 444 | for fmt in 'f', 'F': |
| 445 | # don't do a direct equality check, since on some |
| 446 | # platforms only the first few digits of dtoa |
| 447 | # will be reliable |
| 448 | result = f.__format__(fmt) |
| 449 | self.assertEqual(len(result), 98) |
| 450 | self.assertEqual(result[-7], '.') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 451 | self.assertIn(result[:12], ('112340000000', '112339999999')) |
Mark Dickinson | 33841c3 | 2009-05-01 15:37:04 +0000 | [diff] [blame] | 452 | f = 1.1234e200 |
| 453 | for fmt in 'f', 'F': |
| 454 | result = f.__format__(fmt) |
| 455 | self.assertEqual(len(result), 208) |
| 456 | self.assertEqual(result[-7], '.') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 457 | self.assertIn(result[:12], ('112340000000', '112339999999')) |
Mark Dickinson | 33841c3 | 2009-05-01 15:37:04 +0000 | [diff] [blame] | 458 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 459 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 460 | test( 1.0, 'e', '1.000000e+00') |
| 461 | test(-1.0, 'e', '-1.000000e+00') |
| 462 | test( 1.0, 'E', '1.000000E+00') |
| 463 | test(-1.0, 'E', '-1.000000E+00') |
| 464 | test(1.1234e20, 'e', '1.123400e+20') |
| 465 | test(1.1234e20, 'E', '1.123400E+20') |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 466 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 467 | # No format code means use g, but must have a decimal |
| 468 | # and a number after the decimal. This is tricky, because |
| 469 | # a totaly empty format specifier means something else. |
| 470 | # So, just use a sign flag |
| 471 | test(1e200, '+g', '+1e+200') |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 472 | test(1e200, '+', '+1e+200') |
| 473 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 474 | test(1.1e200, '+g', '+1.1e+200') |
| 475 | test(1.1e200, '+', '+1.1e+200') |
| 476 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 477 | # 0 padding |
| 478 | test(1234., '010f', '1234.000000') |
| 479 | test(1234., '011f', '1234.000000') |
| 480 | test(1234., '012f', '01234.000000') |
| 481 | test(-1234., '011f', '-1234.000000') |
| 482 | test(-1234., '012f', '-1234.000000') |
| 483 | test(-1234., '013f', '-01234.000000') |
| 484 | test(-1234.12341234, '013f', '-01234.123412') |
| 485 | test(-123456.12341234, '011.2f', '-0123456.12') |
| 486 | |
Eric Smith | 937491d | 2009-04-22 17:04:27 +0000 | [diff] [blame] | 487 | # issue 5782, commas with no specifier type |
| 488 | test(1.2, '010,.2', '0,000,001.2') |
| 489 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 490 | # 0 padding with commas |
| 491 | test(1234., '011,f', '1,234.000000') |
| 492 | test(1234., '012,f', '1,234.000000') |
| 493 | test(1234., '013,f', '01,234.000000') |
| 494 | test(-1234., '012,f', '-1,234.000000') |
| 495 | test(-1234., '013,f', '-1,234.000000') |
| 496 | test(-1234., '014,f', '-01,234.000000') |
| 497 | test(-12345., '015,f', '-012,345.000000') |
| 498 | test(-123456., '016,f', '-0,123,456.000000') |
| 499 | test(-123456., '017,f', '-0,123,456.000000') |
| 500 | test(-123456.12341234, '017,f', '-0,123,456.123412') |
| 501 | test(-123456.12341234, '013,.2f', '-0,123,456.12') |
| 502 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 503 | # % formatting |
| 504 | test(-1.0, '%', '-100.000000%') |
| 505 | |
| 506 | # format spec must be string |
| 507 | self.assertRaises(TypeError, 3.0.__format__, None) |
| 508 | self.assertRaises(TypeError, 3.0.__format__, 0) |
| 509 | |
| 510 | # other format specifiers shouldn't work on floats, |
| 511 | # in particular int specifiers |
| 512 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 513 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
| 514 | if not format_spec in 'eEfFgGn%': |
| 515 | self.assertRaises(ValueError, format, 0.0, format_spec) |
| 516 | self.assertRaises(ValueError, format, 1.0, format_spec) |
| 517 | self.assertRaises(ValueError, format, -1.0, format_spec) |
| 518 | self.assertRaises(ValueError, format, 1e100, format_spec) |
| 519 | self.assertRaises(ValueError, format, -1e100, format_spec) |
| 520 | self.assertRaises(ValueError, format, 1e-100, format_spec) |
| 521 | self.assertRaises(ValueError, format, -1e-100, format_spec) |
| 522 | |
Eric Smith | 984bb58 | 2010-11-25 16:08:06 +0000 | [diff] [blame] | 523 | # Alternate float formatting |
| 524 | test(1.0, '.0e', '1e+00') |
| 525 | test(1.0, '#.0e', '1.e+00') |
| 526 | test(1.0, '.0f', '1') |
| 527 | test(1.0, '#.0f', '1.') |
| 528 | test(1.1, 'g', '1.1') |
| 529 | test(1.1, '#g', '1.10000') |
| 530 | test(1.0, '.0%', '100%') |
| 531 | test(1.0, '#.0%', '100.%') |
| 532 | |
| 533 | # Issue 7094: Alternate formatting (specified by #) |
| 534 | test(1.0, '0e', '1.000000e+00') |
| 535 | test(1.0, '#0e', '1.000000e+00') |
| 536 | test(1.0, '0f', '1.000000' ) |
| 537 | test(1.0, '#0f', '1.000000') |
| 538 | test(1.0, '.1e', '1.0e+00') |
| 539 | test(1.0, '#.1e', '1.0e+00') |
| 540 | test(1.0, '.1f', '1.0') |
| 541 | test(1.0, '#.1f', '1.0') |
| 542 | test(1.0, '.1%', '100.0%') |
| 543 | test(1.0, '#.1%', '100.0%') |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 544 | |
Eric Smith | abb28c6 | 2010-02-23 00:22:24 +0000 | [diff] [blame] | 545 | # Issue 6902 |
| 546 | test(12345.6, "0<20", '12345.60000000000000') |
| 547 | test(12345.6, "1<20", '12345.61111111111111') |
| 548 | test(12345.6, "*<20", '12345.6*************') |
| 549 | test(12345.6, "0>20", '000000000000012345.6') |
| 550 | test(12345.6, "1>20", '111111111111112345.6') |
| 551 | test(12345.6, "*>20", '*************12345.6') |
| 552 | test(12345.6, "0=20", '000000000000012345.6') |
| 553 | test(12345.6, "1=20", '111111111111112345.6') |
| 554 | test(12345.6, "*=20", '*************12345.6') |
| 555 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 556 | def test_format_spec_errors(self): |
| 557 | # int, float, and string all share the same format spec |
| 558 | # mini-language parser. |
| 559 | |
| 560 | # Check that we can't ask for too many digits. This is |
| 561 | # probably a CPython specific test. It tries to put the width |
| 562 | # into a C long. |
| 563 | self.assertRaises(ValueError, format, 0, '1'*10000 + 'd') |
| 564 | |
| 565 | # Similar with the precision. |
| 566 | self.assertRaises(ValueError, format, 0, '.' + '1'*10000 + 'd') |
| 567 | |
| 568 | # And may as well test both. |
| 569 | self.assertRaises(ValueError, format, 0, '1'*1000 + '.' + '1'*10000 + 'd') |
| 570 | |
| 571 | # Make sure commas aren't allowed with various type codes |
| 572 | for code in 'xXobns': |
| 573 | self.assertRaises(ValueError, format, 0, ',' + code) |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 574 | |
Benjamin Peterson | 0e10206 | 2010-08-25 23:13:17 +0000 | [diff] [blame] | 575 | def test_internal_sizes(self): |
| 576 | self.assertGreater(object.__basicsize__, 0) |
| 577 | self.assertGreater(tuple.__itemsize__, 0) |
| 578 | |
Guido van Rossum | 934aba6 | 2017-02-01 10:55:58 -0800 | [diff] [blame] | 579 | def test_slot_wrapper_types(self): |
Jim Fasarakis-Hilliard | 08c1601 | 2017-04-25 21:26:36 +0300 | [diff] [blame^] | 580 | self.assertIsInstance(object.__init__, types.WrapperDescriptorType) |
| 581 | self.assertIsInstance(object.__str__, types.WrapperDescriptorType) |
| 582 | self.assertIsInstance(object.__lt__, types.WrapperDescriptorType) |
| 583 | self.assertIsInstance(int.__lt__, types.WrapperDescriptorType) |
Guido van Rossum | 934aba6 | 2017-02-01 10:55:58 -0800 | [diff] [blame] | 584 | |
| 585 | def test_method_wrapper_types(self): |
| 586 | self.assertIsInstance(object().__init__, types.MethodWrapperType) |
| 587 | self.assertIsInstance(object().__str__, types.MethodWrapperType) |
| 588 | self.assertIsInstance(object().__lt__, types.MethodWrapperType) |
| 589 | self.assertIsInstance((42).__lt__, types.MethodWrapperType) |
| 590 | |
| 591 | def test_method_descriptor_types(self): |
| 592 | self.assertIsInstance(str.join, types.MethodDescriptorType) |
| 593 | self.assertIsInstance(list.append, types.MethodDescriptorType) |
| 594 | self.assertIsInstance(''.join, types.BuiltinMethodType) |
| 595 | self.assertIsInstance([].append, types.BuiltinMethodType) |
| 596 | |
Benjamin Peterson | 0e10206 | 2010-08-25 23:13:17 +0000 | [diff] [blame] | 597 | |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 598 | class MappingProxyTests(unittest.TestCase): |
| 599 | mappingproxy = types.MappingProxyType |
| 600 | |
| 601 | def test_constructor(self): |
| 602 | class userdict(dict): |
| 603 | pass |
| 604 | |
| 605 | mapping = {'x': 1, 'y': 2} |
| 606 | self.assertEqual(self.mappingproxy(mapping), mapping) |
| 607 | mapping = userdict(x=1, y=2) |
| 608 | self.assertEqual(self.mappingproxy(mapping), mapping) |
| 609 | mapping = collections.ChainMap({'x': 1}, {'y': 2}) |
| 610 | self.assertEqual(self.mappingproxy(mapping), mapping) |
| 611 | |
| 612 | self.assertRaises(TypeError, self.mappingproxy, 10) |
| 613 | self.assertRaises(TypeError, self.mappingproxy, ("a", "tuple")) |
| 614 | self.assertRaises(TypeError, self.mappingproxy, ["a", "list"]) |
| 615 | |
| 616 | def test_methods(self): |
| 617 | attrs = set(dir(self.mappingproxy({}))) - set(dir(object())) |
| 618 | self.assertEqual(attrs, { |
| 619 | '__contains__', |
| 620 | '__getitem__', |
| 621 | '__iter__', |
| 622 | '__len__', |
| 623 | 'copy', |
| 624 | 'get', |
| 625 | 'items', |
| 626 | 'keys', |
| 627 | 'values', |
| 628 | }) |
| 629 | |
| 630 | def test_get(self): |
| 631 | view = self.mappingproxy({'a': 'A', 'b': 'B'}) |
| 632 | self.assertEqual(view['a'], 'A') |
| 633 | self.assertEqual(view['b'], 'B') |
| 634 | self.assertRaises(KeyError, view.__getitem__, 'xxx') |
| 635 | self.assertEqual(view.get('a'), 'A') |
| 636 | self.assertIsNone(view.get('xxx')) |
| 637 | self.assertEqual(view.get('xxx', 42), 42) |
| 638 | |
| 639 | def test_missing(self): |
| 640 | class dictmissing(dict): |
| 641 | def __missing__(self, key): |
| 642 | return "missing=%s" % key |
| 643 | |
| 644 | view = self.mappingproxy(dictmissing(x=1)) |
| 645 | self.assertEqual(view['x'], 1) |
| 646 | self.assertEqual(view['y'], 'missing=y') |
| 647 | self.assertEqual(view.get('x'), 1) |
| 648 | self.assertEqual(view.get('y'), None) |
| 649 | self.assertEqual(view.get('y', 42), 42) |
| 650 | self.assertTrue('x' in view) |
| 651 | self.assertFalse('y' in view) |
| 652 | |
| 653 | def test_customdict(self): |
| 654 | class customdict(dict): |
| 655 | def __contains__(self, key): |
| 656 | if key == 'magic': |
| 657 | return True |
| 658 | else: |
| 659 | return dict.__contains__(self, key) |
| 660 | |
| 661 | def __iter__(self): |
| 662 | return iter(('iter',)) |
| 663 | |
| 664 | def __len__(self): |
| 665 | return 500 |
| 666 | |
| 667 | def copy(self): |
| 668 | return 'copy' |
| 669 | |
| 670 | def keys(self): |
| 671 | return 'keys' |
| 672 | |
| 673 | def items(self): |
| 674 | return 'items' |
| 675 | |
| 676 | def values(self): |
| 677 | return 'values' |
| 678 | |
| 679 | def __getitem__(self, key): |
| 680 | return "getitem=%s" % dict.__getitem__(self, key) |
| 681 | |
| 682 | def get(self, key, default=None): |
| 683 | return "get=%s" % dict.get(self, key, 'default=%r' % default) |
| 684 | |
| 685 | custom = customdict({'key': 'value'}) |
| 686 | view = self.mappingproxy(custom) |
| 687 | self.assertTrue('key' in view) |
| 688 | self.assertTrue('magic' in view) |
| 689 | self.assertFalse('xxx' in view) |
| 690 | self.assertEqual(view['key'], 'getitem=value') |
| 691 | self.assertRaises(KeyError, view.__getitem__, 'xxx') |
| 692 | self.assertEqual(tuple(view), ('iter',)) |
| 693 | self.assertEqual(len(view), 500) |
| 694 | self.assertEqual(view.copy(), 'copy') |
| 695 | self.assertEqual(view.get('key'), 'get=value') |
| 696 | self.assertEqual(view.get('xxx'), 'get=default=None') |
| 697 | self.assertEqual(view.items(), 'items') |
| 698 | self.assertEqual(view.keys(), 'keys') |
| 699 | self.assertEqual(view.values(), 'values') |
| 700 | |
| 701 | def test_chainmap(self): |
| 702 | d1 = {'x': 1} |
| 703 | d2 = {'y': 2} |
| 704 | mapping = collections.ChainMap(d1, d2) |
| 705 | view = self.mappingproxy(mapping) |
| 706 | self.assertTrue('x' in view) |
| 707 | self.assertTrue('y' in view) |
| 708 | self.assertFalse('z' in view) |
| 709 | self.assertEqual(view['x'], 1) |
| 710 | self.assertEqual(view['y'], 2) |
| 711 | self.assertRaises(KeyError, view.__getitem__, 'z') |
| 712 | self.assertEqual(tuple(sorted(view)), ('x', 'y')) |
| 713 | self.assertEqual(len(view), 2) |
| 714 | copy = view.copy() |
| 715 | self.assertIsNot(copy, mapping) |
| 716 | self.assertIsInstance(copy, collections.ChainMap) |
| 717 | self.assertEqual(copy, mapping) |
| 718 | self.assertEqual(view.get('x'), 1) |
| 719 | self.assertEqual(view.get('y'), 2) |
| 720 | self.assertIsNone(view.get('z')) |
| 721 | self.assertEqual(tuple(sorted(view.items())), (('x', 1), ('y', 2))) |
| 722 | self.assertEqual(tuple(sorted(view.keys())), ('x', 'y')) |
| 723 | self.assertEqual(tuple(sorted(view.values())), (1, 2)) |
| 724 | |
| 725 | def test_contains(self): |
| 726 | view = self.mappingproxy(dict.fromkeys('abc')) |
| 727 | self.assertTrue('a' in view) |
| 728 | self.assertTrue('b' in view) |
| 729 | self.assertTrue('c' in view) |
| 730 | self.assertFalse('xxx' in view) |
| 731 | |
| 732 | def test_views(self): |
| 733 | mapping = {} |
| 734 | view = self.mappingproxy(mapping) |
| 735 | keys = view.keys() |
| 736 | values = view.values() |
| 737 | items = view.items() |
| 738 | self.assertEqual(list(keys), []) |
| 739 | self.assertEqual(list(values), []) |
| 740 | self.assertEqual(list(items), []) |
| 741 | mapping['key'] = 'value' |
| 742 | self.assertEqual(list(keys), ['key']) |
| 743 | self.assertEqual(list(values), ['value']) |
| 744 | self.assertEqual(list(items), [('key', 'value')]) |
| 745 | |
| 746 | def test_len(self): |
| 747 | for expected in range(6): |
| 748 | data = dict.fromkeys('abcde'[:expected]) |
| 749 | self.assertEqual(len(data), expected) |
| 750 | view = self.mappingproxy(data) |
| 751 | self.assertEqual(len(view), expected) |
| 752 | |
| 753 | def test_iterators(self): |
| 754 | keys = ('x', 'y') |
| 755 | values = (1, 2) |
| 756 | items = tuple(zip(keys, values)) |
| 757 | view = self.mappingproxy(dict(items)) |
| 758 | self.assertEqual(set(view), set(keys)) |
| 759 | self.assertEqual(set(view.keys()), set(keys)) |
| 760 | self.assertEqual(set(view.values()), set(values)) |
| 761 | self.assertEqual(set(view.items()), set(items)) |
| 762 | |
| 763 | def test_copy(self): |
| 764 | original = {'key1': 27, 'key2': 51, 'key3': 93} |
| 765 | view = self.mappingproxy(original) |
| 766 | copy = view.copy() |
| 767 | self.assertEqual(type(copy), dict) |
| 768 | self.assertEqual(copy, original) |
| 769 | original['key1'] = 70 |
| 770 | self.assertEqual(view['key1'], 70) |
| 771 | self.assertEqual(copy['key1'], 27) |
| 772 | |
| 773 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 774 | class ClassCreationTests(unittest.TestCase): |
| 775 | |
| 776 | class Meta(type): |
| 777 | def __init__(cls, name, bases, ns, **kw): |
| 778 | super().__init__(name, bases, ns) |
| 779 | @staticmethod |
| 780 | def __new__(mcls, name, bases, ns, **kw): |
| 781 | return super().__new__(mcls, name, bases, ns) |
| 782 | @classmethod |
| 783 | def __prepare__(mcls, name, bases, **kw): |
| 784 | ns = super().__prepare__(name, bases) |
| 785 | ns["y"] = 1 |
| 786 | ns.update(kw) |
| 787 | return ns |
| 788 | |
| 789 | def test_new_class_basics(self): |
| 790 | C = types.new_class("C") |
| 791 | self.assertEqual(C.__name__, "C") |
| 792 | self.assertEqual(C.__bases__, (object,)) |
| 793 | |
| 794 | def test_new_class_subclass(self): |
| 795 | C = types.new_class("C", (int,)) |
| 796 | self.assertTrue(issubclass(C, int)) |
| 797 | |
| 798 | def test_new_class_meta(self): |
| 799 | Meta = self.Meta |
| 800 | settings = {"metaclass": Meta, "z": 2} |
| 801 | # We do this twice to make sure the passed in dict isn't mutated |
| 802 | for i in range(2): |
| 803 | C = types.new_class("C" + str(i), (), settings) |
| 804 | self.assertIsInstance(C, Meta) |
| 805 | self.assertEqual(C.y, 1) |
| 806 | self.assertEqual(C.z, 2) |
| 807 | |
| 808 | def test_new_class_exec_body(self): |
| 809 | Meta = self.Meta |
| 810 | def func(ns): |
| 811 | ns["x"] = 0 |
| 812 | C = types.new_class("C", (), {"metaclass": Meta, "z": 2}, func) |
| 813 | self.assertIsInstance(C, Meta) |
| 814 | self.assertEqual(C.x, 0) |
| 815 | self.assertEqual(C.y, 1) |
| 816 | self.assertEqual(C.z, 2) |
| 817 | |
Benjamin Peterson | 43f8f4c | 2012-09-27 18:10:17 -0400 | [diff] [blame] | 818 | def test_new_class_metaclass_keywords(self): |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 819 | #Test that keywords are passed to the metaclass: |
| 820 | def meta_func(name, bases, ns, **kw): |
| 821 | return name, bases, ns, kw |
| 822 | res = types.new_class("X", |
| 823 | (int, object), |
| 824 | dict(metaclass=meta_func, x=0)) |
| 825 | self.assertEqual(res, ("X", (int, object), {}, {"x": 0})) |
| 826 | |
| 827 | def test_new_class_defaults(self): |
| 828 | # Test defaults/keywords: |
| 829 | C = types.new_class("C", (), {}, None) |
| 830 | self.assertEqual(C.__name__, "C") |
| 831 | self.assertEqual(C.__bases__, (object,)) |
| 832 | |
| 833 | def test_new_class_meta_with_base(self): |
| 834 | Meta = self.Meta |
| 835 | def func(ns): |
| 836 | ns["x"] = 0 |
| 837 | C = types.new_class(name="C", |
| 838 | bases=(int,), |
| 839 | kwds=dict(metaclass=Meta, z=2), |
| 840 | exec_body=func) |
| 841 | self.assertTrue(issubclass(C, int)) |
| 842 | self.assertIsInstance(C, Meta) |
| 843 | self.assertEqual(C.x, 0) |
| 844 | self.assertEqual(C.y, 1) |
| 845 | self.assertEqual(C.z, 2) |
| 846 | |
| 847 | # Many of the following tests are derived from test_descr.py |
| 848 | def test_prepare_class(self): |
| 849 | # Basic test of metaclass derivation |
| 850 | expected_ns = {} |
| 851 | class A(type): |
| 852 | def __new__(*args, **kwargs): |
| 853 | return type.__new__(*args, **kwargs) |
| 854 | |
| 855 | def __prepare__(*args): |
| 856 | return expected_ns |
| 857 | |
| 858 | B = types.new_class("B", (object,)) |
| 859 | C = types.new_class("C", (object,), {"metaclass": A}) |
| 860 | |
| 861 | # The most derived metaclass of D is A rather than type. |
| 862 | meta, ns, kwds = types.prepare_class("D", (B, C), {"metaclass": type}) |
| 863 | self.assertIs(meta, A) |
| 864 | self.assertIs(ns, expected_ns) |
| 865 | self.assertEqual(len(kwds), 0) |
| 866 | |
| 867 | def test_metaclass_derivation(self): |
| 868 | # issue1294232: correct metaclass calculation |
| 869 | new_calls = [] # to check the order of __new__ calls |
| 870 | class AMeta(type): |
| 871 | def __new__(mcls, name, bases, ns): |
| 872 | new_calls.append('AMeta') |
| 873 | return super().__new__(mcls, name, bases, ns) |
| 874 | @classmethod |
| 875 | def __prepare__(mcls, name, bases): |
| 876 | return {} |
| 877 | |
| 878 | class BMeta(AMeta): |
| 879 | def __new__(mcls, name, bases, ns): |
| 880 | new_calls.append('BMeta') |
| 881 | return super().__new__(mcls, name, bases, ns) |
| 882 | @classmethod |
| 883 | def __prepare__(mcls, name, bases): |
| 884 | ns = super().__prepare__(name, bases) |
| 885 | ns['BMeta_was_here'] = True |
| 886 | return ns |
| 887 | |
| 888 | A = types.new_class("A", (), {"metaclass": AMeta}) |
| 889 | self.assertEqual(new_calls, ['AMeta']) |
| 890 | new_calls.clear() |
| 891 | |
| 892 | B = types.new_class("B", (), {"metaclass": BMeta}) |
| 893 | # BMeta.__new__ calls AMeta.__new__ with super: |
| 894 | self.assertEqual(new_calls, ['BMeta', 'AMeta']) |
| 895 | new_calls.clear() |
| 896 | |
| 897 | C = types.new_class("C", (A, B)) |
| 898 | # The most derived metaclass is BMeta: |
| 899 | self.assertEqual(new_calls, ['BMeta', 'AMeta']) |
| 900 | new_calls.clear() |
| 901 | # BMeta.__prepare__ should've been called: |
| 902 | self.assertIn('BMeta_was_here', C.__dict__) |
| 903 | |
| 904 | # The order of the bases shouldn't matter: |
| 905 | C2 = types.new_class("C2", (B, A)) |
| 906 | self.assertEqual(new_calls, ['BMeta', 'AMeta']) |
| 907 | new_calls.clear() |
| 908 | self.assertIn('BMeta_was_here', C2.__dict__) |
| 909 | |
| 910 | # Check correct metaclass calculation when a metaclass is declared: |
| 911 | D = types.new_class("D", (C,), {"metaclass": type}) |
| 912 | self.assertEqual(new_calls, ['BMeta', 'AMeta']) |
| 913 | new_calls.clear() |
| 914 | self.assertIn('BMeta_was_here', D.__dict__) |
| 915 | |
| 916 | E = types.new_class("E", (C,), {"metaclass": AMeta}) |
| 917 | self.assertEqual(new_calls, ['BMeta', 'AMeta']) |
| 918 | new_calls.clear() |
| 919 | self.assertIn('BMeta_was_here', E.__dict__) |
| 920 | |
| 921 | def test_metaclass_override_function(self): |
| 922 | # Special case: the given metaclass isn't a class, |
| 923 | # so there is no metaclass calculation. |
| 924 | class A(metaclass=self.Meta): |
| 925 | pass |
| 926 | |
| 927 | marker = object() |
| 928 | def func(*args, **kwargs): |
| 929 | return marker |
| 930 | |
| 931 | X = types.new_class("X", (), {"metaclass": func}) |
| 932 | Y = types.new_class("Y", (object,), {"metaclass": func}) |
| 933 | Z = types.new_class("Z", (A,), {"metaclass": func}) |
| 934 | self.assertIs(marker, X) |
| 935 | self.assertIs(marker, Y) |
| 936 | self.assertIs(marker, Z) |
| 937 | |
| 938 | def test_metaclass_override_callable(self): |
| 939 | # The given metaclass is a class, |
| 940 | # but not a descendant of type. |
| 941 | new_calls = [] # to check the order of __new__ calls |
| 942 | prepare_calls = [] # to track __prepare__ calls |
| 943 | class ANotMeta: |
| 944 | def __new__(mcls, *args, **kwargs): |
| 945 | new_calls.append('ANotMeta') |
| 946 | return super().__new__(mcls) |
| 947 | @classmethod |
| 948 | def __prepare__(mcls, name, bases): |
| 949 | prepare_calls.append('ANotMeta') |
| 950 | return {} |
| 951 | |
| 952 | class BNotMeta(ANotMeta): |
| 953 | def __new__(mcls, *args, **kwargs): |
| 954 | new_calls.append('BNotMeta') |
| 955 | return super().__new__(mcls) |
| 956 | @classmethod |
| 957 | def __prepare__(mcls, name, bases): |
| 958 | prepare_calls.append('BNotMeta') |
| 959 | return super().__prepare__(name, bases) |
| 960 | |
| 961 | A = types.new_class("A", (), {"metaclass": ANotMeta}) |
| 962 | self.assertIs(ANotMeta, type(A)) |
| 963 | self.assertEqual(prepare_calls, ['ANotMeta']) |
| 964 | prepare_calls.clear() |
| 965 | self.assertEqual(new_calls, ['ANotMeta']) |
| 966 | new_calls.clear() |
| 967 | |
| 968 | B = types.new_class("B", (), {"metaclass": BNotMeta}) |
| 969 | self.assertIs(BNotMeta, type(B)) |
| 970 | self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) |
| 971 | prepare_calls.clear() |
| 972 | self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) |
| 973 | new_calls.clear() |
| 974 | |
| 975 | C = types.new_class("C", (A, B)) |
| 976 | self.assertIs(BNotMeta, type(C)) |
| 977 | self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) |
| 978 | prepare_calls.clear() |
| 979 | self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) |
| 980 | new_calls.clear() |
| 981 | |
| 982 | C2 = types.new_class("C2", (B, A)) |
| 983 | self.assertIs(BNotMeta, type(C2)) |
| 984 | self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) |
| 985 | prepare_calls.clear() |
| 986 | self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) |
| 987 | new_calls.clear() |
| 988 | |
| 989 | # This is a TypeError, because of a metaclass conflict: |
| 990 | # BNotMeta is neither a subclass, nor a superclass of type |
| 991 | with self.assertRaises(TypeError): |
| 992 | D = types.new_class("D", (C,), {"metaclass": type}) |
| 993 | |
| 994 | E = types.new_class("E", (C,), {"metaclass": ANotMeta}) |
| 995 | self.assertIs(BNotMeta, type(E)) |
| 996 | self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) |
| 997 | prepare_calls.clear() |
| 998 | self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) |
| 999 | new_calls.clear() |
| 1000 | |
| 1001 | F = types.new_class("F", (object(), C)) |
| 1002 | self.assertIs(BNotMeta, type(F)) |
| 1003 | self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) |
| 1004 | prepare_calls.clear() |
| 1005 | self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) |
| 1006 | new_calls.clear() |
| 1007 | |
| 1008 | F2 = types.new_class("F2", (C, object())) |
| 1009 | self.assertIs(BNotMeta, type(F2)) |
| 1010 | self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) |
| 1011 | prepare_calls.clear() |
| 1012 | self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) |
| 1013 | new_calls.clear() |
| 1014 | |
| 1015 | # TypeError: BNotMeta is neither a |
| 1016 | # subclass, nor a superclass of int |
| 1017 | with self.assertRaises(TypeError): |
| 1018 | X = types.new_class("X", (C, int())) |
| 1019 | with self.assertRaises(TypeError): |
| 1020 | X = types.new_class("X", (int(), C)) |
| 1021 | |
Berker Peksag | 3f015a6 | 2016-08-19 11:04:07 +0300 | [diff] [blame] | 1022 | def test_one_argument_type(self): |
| 1023 | expected_message = 'type.__new__() takes exactly 3 arguments (1 given)' |
| 1024 | |
| 1025 | # Only type itself can use the one-argument form (#27157) |
| 1026 | self.assertIs(type(5), int) |
| 1027 | |
| 1028 | class M(type): |
| 1029 | pass |
| 1030 | with self.assertRaises(TypeError) as cm: |
| 1031 | M(5) |
| 1032 | self.assertEqual(str(cm.exception), expected_message) |
| 1033 | |
| 1034 | class N(type, metaclass=M): |
| 1035 | pass |
| 1036 | with self.assertRaises(TypeError) as cm: |
| 1037 | N(5) |
| 1038 | self.assertEqual(str(cm.exception), expected_message) |
| 1039 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 1040 | |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 1041 | class SimpleNamespaceTests(unittest.TestCase): |
| 1042 | |
| 1043 | def test_constructor(self): |
| 1044 | ns1 = types.SimpleNamespace() |
| 1045 | ns2 = types.SimpleNamespace(x=1, y=2) |
| 1046 | ns3 = types.SimpleNamespace(**dict(x=1, y=2)) |
| 1047 | |
| 1048 | with self.assertRaises(TypeError): |
| 1049 | types.SimpleNamespace(1, 2, 3) |
| 1050 | |
| 1051 | self.assertEqual(len(ns1.__dict__), 0) |
| 1052 | self.assertEqual(vars(ns1), {}) |
| 1053 | self.assertEqual(len(ns2.__dict__), 2) |
| 1054 | self.assertEqual(vars(ns2), {'y': 2, 'x': 1}) |
| 1055 | self.assertEqual(len(ns3.__dict__), 2) |
| 1056 | self.assertEqual(vars(ns3), {'y': 2, 'x': 1}) |
| 1057 | |
| 1058 | def test_unbound(self): |
| 1059 | ns1 = vars(types.SimpleNamespace()) |
| 1060 | ns2 = vars(types.SimpleNamespace(x=1, y=2)) |
| 1061 | |
| 1062 | self.assertEqual(ns1, {}) |
| 1063 | self.assertEqual(ns2, {'y': 2, 'x': 1}) |
| 1064 | |
| 1065 | def test_underlying_dict(self): |
| 1066 | ns1 = types.SimpleNamespace() |
| 1067 | ns2 = types.SimpleNamespace(x=1, y=2) |
| 1068 | ns3 = types.SimpleNamespace(a=True, b=False) |
| 1069 | mapping = ns3.__dict__ |
| 1070 | del ns3 |
| 1071 | |
| 1072 | self.assertEqual(ns1.__dict__, {}) |
| 1073 | self.assertEqual(ns2.__dict__, {'y': 2, 'x': 1}) |
| 1074 | self.assertEqual(mapping, dict(a=True, b=False)) |
| 1075 | |
| 1076 | def test_attrget(self): |
| 1077 | ns = types.SimpleNamespace(x=1, y=2, w=3) |
| 1078 | |
| 1079 | self.assertEqual(ns.x, 1) |
| 1080 | self.assertEqual(ns.y, 2) |
| 1081 | self.assertEqual(ns.w, 3) |
| 1082 | with self.assertRaises(AttributeError): |
| 1083 | ns.z |
| 1084 | |
| 1085 | def test_attrset(self): |
| 1086 | ns1 = types.SimpleNamespace() |
| 1087 | ns2 = types.SimpleNamespace(x=1, y=2, w=3) |
| 1088 | ns1.a = 'spam' |
| 1089 | ns1.b = 'ham' |
| 1090 | ns2.z = 4 |
| 1091 | ns2.theta = None |
| 1092 | |
| 1093 | self.assertEqual(ns1.__dict__, dict(a='spam', b='ham')) |
| 1094 | self.assertEqual(ns2.__dict__, dict(x=1, y=2, w=3, z=4, theta=None)) |
| 1095 | |
| 1096 | def test_attrdel(self): |
| 1097 | ns1 = types.SimpleNamespace() |
| 1098 | ns2 = types.SimpleNamespace(x=1, y=2, w=3) |
| 1099 | |
| 1100 | with self.assertRaises(AttributeError): |
| 1101 | del ns1.spam |
| 1102 | with self.assertRaises(AttributeError): |
| 1103 | del ns2.spam |
| 1104 | |
| 1105 | del ns2.y |
| 1106 | self.assertEqual(vars(ns2), dict(w=3, x=1)) |
| 1107 | ns2.y = 'spam' |
| 1108 | self.assertEqual(vars(ns2), dict(w=3, x=1, y='spam')) |
| 1109 | del ns2.y |
| 1110 | self.assertEqual(vars(ns2), dict(w=3, x=1)) |
| 1111 | |
| 1112 | ns1.spam = 5 |
| 1113 | self.assertEqual(vars(ns1), dict(spam=5)) |
| 1114 | del ns1.spam |
| 1115 | self.assertEqual(vars(ns1), {}) |
| 1116 | |
| 1117 | def test_repr(self): |
| 1118 | ns1 = types.SimpleNamespace(x=1, y=2, w=3) |
| 1119 | ns2 = types.SimpleNamespace() |
| 1120 | ns2.x = "spam" |
| 1121 | ns2._y = 5 |
Eric Snow | b5c8f92 | 2013-02-16 16:32:39 -0700 | [diff] [blame] | 1122 | name = "namespace" |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 1123 | |
Eric Snow | b5c8f92 | 2013-02-16 16:32:39 -0700 | [diff] [blame] | 1124 | self.assertEqual(repr(ns1), "{name}(w=3, x=1, y=2)".format(name=name)) |
| 1125 | self.assertEqual(repr(ns2), "{name}(_y=5, x='spam')".format(name=name)) |
| 1126 | |
| 1127 | def test_equal(self): |
| 1128 | ns1 = types.SimpleNamespace(x=1) |
| 1129 | ns2 = types.SimpleNamespace() |
| 1130 | ns2.x = 1 |
| 1131 | |
| 1132 | self.assertEqual(types.SimpleNamespace(), types.SimpleNamespace()) |
| 1133 | self.assertEqual(ns1, ns2) |
| 1134 | self.assertNotEqual(ns2, types.SimpleNamespace()) |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 1135 | |
| 1136 | def test_nested(self): |
| 1137 | ns1 = types.SimpleNamespace(a=1, b=2) |
| 1138 | ns2 = types.SimpleNamespace() |
| 1139 | ns3 = types.SimpleNamespace(x=ns1) |
| 1140 | ns2.spam = ns1 |
| 1141 | ns2.ham = '?' |
| 1142 | ns2.spam = ns3 |
| 1143 | |
| 1144 | self.assertEqual(vars(ns1), dict(a=1, b=2)) |
| 1145 | self.assertEqual(vars(ns2), dict(spam=ns3, ham='?')) |
| 1146 | self.assertEqual(ns2.spam, ns3) |
| 1147 | self.assertEqual(vars(ns3), dict(x=ns1)) |
| 1148 | self.assertEqual(ns3.x.a, 1) |
| 1149 | |
| 1150 | def test_recursive(self): |
| 1151 | ns1 = types.SimpleNamespace(c='cookie') |
| 1152 | ns2 = types.SimpleNamespace() |
| 1153 | ns3 = types.SimpleNamespace(x=1) |
| 1154 | ns1.spam = ns1 |
| 1155 | ns2.spam = ns3 |
| 1156 | ns3.spam = ns2 |
| 1157 | |
| 1158 | self.assertEqual(ns1.spam, ns1) |
| 1159 | self.assertEqual(ns1.spam.spam, ns1) |
| 1160 | self.assertEqual(ns1.spam.spam, ns1.spam) |
| 1161 | self.assertEqual(ns2.spam, ns3) |
| 1162 | self.assertEqual(ns3.spam, ns2) |
| 1163 | self.assertEqual(ns2.spam.spam, ns2) |
| 1164 | |
| 1165 | def test_recursive_repr(self): |
| 1166 | ns1 = types.SimpleNamespace(c='cookie') |
| 1167 | ns2 = types.SimpleNamespace() |
| 1168 | ns3 = types.SimpleNamespace(x=1) |
| 1169 | ns1.spam = ns1 |
| 1170 | ns2.spam = ns3 |
| 1171 | ns3.spam = ns2 |
Eric Snow | b5c8f92 | 2013-02-16 16:32:39 -0700 | [diff] [blame] | 1172 | name = "namespace" |
| 1173 | repr1 = "{name}(c='cookie', spam={name}(...))".format(name=name) |
| 1174 | repr2 = "{name}(spam={name}(spam={name}(...), x=1))".format(name=name) |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 1175 | |
Eric Snow | b5c8f92 | 2013-02-16 16:32:39 -0700 | [diff] [blame] | 1176 | self.assertEqual(repr(ns1), repr1) |
| 1177 | self.assertEqual(repr(ns2), repr2) |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 1178 | |
| 1179 | def test_as_dict(self): |
| 1180 | ns = types.SimpleNamespace(spam='spamspamspam') |
| 1181 | |
| 1182 | with self.assertRaises(TypeError): |
| 1183 | len(ns) |
| 1184 | with self.assertRaises(TypeError): |
| 1185 | iter(ns) |
| 1186 | with self.assertRaises(TypeError): |
| 1187 | 'spam' in ns |
| 1188 | with self.assertRaises(TypeError): |
| 1189 | ns['spam'] |
| 1190 | |
Eric Snow | 547298c | 2012-10-16 22:35:38 -0700 | [diff] [blame] | 1191 | def test_subclass(self): |
| 1192 | class Spam(types.SimpleNamespace): |
| 1193 | pass |
| 1194 | |
| 1195 | spam = Spam(ham=8, eggs=9) |
| 1196 | |
| 1197 | self.assertIs(type(spam), Spam) |
| 1198 | self.assertEqual(vars(spam), {'ham': 8, 'eggs': 9}) |
| 1199 | |
Eric Snow | b5c8f92 | 2013-02-16 16:32:39 -0700 | [diff] [blame] | 1200 | def test_pickle(self): |
| 1201 | ns = types.SimpleNamespace(breakfast="spam", lunch="spam") |
| 1202 | |
Eric Snow | 9d05c8c | 2013-02-16 18:20:32 -0700 | [diff] [blame] | 1203 | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1204 | pname = "protocol {}".format(protocol) |
| 1205 | try: |
| 1206 | ns_pickled = pickle.dumps(ns, protocol) |
| 1207 | except TypeError as e: |
| 1208 | raise TypeError(pname) from e |
| 1209 | ns_roundtrip = pickle.loads(ns_pickled) |
Eric Snow | b5c8f92 | 2013-02-16 16:32:39 -0700 | [diff] [blame] | 1210 | |
Eric Snow | 9d05c8c | 2013-02-16 18:20:32 -0700 | [diff] [blame] | 1211 | self.assertEqual(ns, ns_roundtrip, pname) |
Eric Snow | b5c8f92 | 2013-02-16 16:32:39 -0700 | [diff] [blame] | 1212 | |
Serhiy Storchaka | 08d230a | 2015-05-22 11:02:49 +0300 | [diff] [blame] | 1213 | def test_fake_namespace_compare(self): |
| 1214 | # Issue #24257: Incorrect use of PyObject_IsInstance() caused |
| 1215 | # SystemError. |
| 1216 | class FakeSimpleNamespace(str): |
| 1217 | __class__ = types.SimpleNamespace |
| 1218 | self.assertFalse(types.SimpleNamespace() == FakeSimpleNamespace()) |
| 1219 | self.assertTrue(types.SimpleNamespace() != FakeSimpleNamespace()) |
| 1220 | with self.assertRaises(TypeError): |
| 1221 | types.SimpleNamespace() < FakeSimpleNamespace() |
| 1222 | with self.assertRaises(TypeError): |
| 1223 | types.SimpleNamespace() <= FakeSimpleNamespace() |
| 1224 | with self.assertRaises(TypeError): |
| 1225 | types.SimpleNamespace() > FakeSimpleNamespace() |
| 1226 | with self.assertRaises(TypeError): |
| 1227 | types.SimpleNamespace() >= FakeSimpleNamespace() |
| 1228 | |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 1229 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1230 | class CoroutineTests(unittest.TestCase): |
| 1231 | def test_wrong_args(self): |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 1232 | samples = [None, 1, object()] |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1233 | for sample in samples: |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 1234 | with self.assertRaisesRegex(TypeError, |
| 1235 | 'types.coroutine.*expects a callable'): |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1236 | types.coroutine(sample) |
| 1237 | |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1238 | def test_non_gen_values(self): |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 1239 | @types.coroutine |
| 1240 | def foo(): |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1241 | return 'spam' |
| 1242 | self.assertEqual(foo(), 'spam') |
| 1243 | |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1244 | class Awaitable: |
| 1245 | def __await__(self): |
| 1246 | return () |
| 1247 | aw = Awaitable() |
| 1248 | @types.coroutine |
| 1249 | def foo(): |
| 1250 | return aw |
| 1251 | self.assertIs(aw, foo()) |
| 1252 | |
Yury Selivanov | 4887523c | 2015-07-23 15:58:37 +0300 | [diff] [blame] | 1253 | # decorate foo second time |
| 1254 | foo = types.coroutine(foo) |
| 1255 | self.assertIs(aw, foo()) |
| 1256 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1257 | def test_async_def(self): |
| 1258 | # Test that types.coroutine passes 'async def' coroutines |
| 1259 | # without modification |
| 1260 | |
| 1261 | async def foo(): pass |
| 1262 | foo_code = foo.__code__ |
| 1263 | foo_flags = foo.__code__.co_flags |
| 1264 | decorated_foo = types.coroutine(foo) |
| 1265 | self.assertIs(foo, decorated_foo) |
| 1266 | self.assertEqual(foo.__code__.co_flags, foo_flags) |
| 1267 | self.assertIs(decorated_foo.__code__, foo_code) |
| 1268 | |
| 1269 | foo_coro = foo() |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1270 | def bar(): return foo_coro |
Yury Selivanov | 4887523c | 2015-07-23 15:58:37 +0300 | [diff] [blame] | 1271 | for _ in range(2): |
| 1272 | bar = types.coroutine(bar) |
| 1273 | coro = bar() |
| 1274 | self.assertIs(foo_coro, coro) |
| 1275 | self.assertEqual(coro.cr_code.co_flags, foo_flags) |
| 1276 | coro.close() |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 1277 | |
| 1278 | def test_duck_coro(self): |
| 1279 | class CoroLike: |
| 1280 | def send(self): pass |
| 1281 | def throw(self): pass |
| 1282 | def close(self): pass |
Yury Selivanov | 13f7723 | 2015-05-29 16:19:18 -0400 | [diff] [blame] | 1283 | def __await__(self): return self |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 1284 | |
| 1285 | coro = CoroLike() |
| 1286 | @types.coroutine |
| 1287 | def foo(): |
| 1288 | return coro |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1289 | self.assertIs(foo(), coro) |
| 1290 | self.assertIs(foo().__await__(), coro) |
| 1291 | |
| 1292 | def test_duck_corogen(self): |
| 1293 | class CoroGenLike: |
| 1294 | def send(self): pass |
| 1295 | def throw(self): pass |
| 1296 | def close(self): pass |
| 1297 | def __await__(self): return self |
| 1298 | def __iter__(self): return self |
| 1299 | def __next__(self): pass |
| 1300 | |
| 1301 | coro = CoroGenLike() |
| 1302 | @types.coroutine |
| 1303 | def foo(): |
| 1304 | return coro |
| 1305 | self.assertIs(foo(), coro) |
Yury Selivanov | 13f7723 | 2015-05-29 16:19:18 -0400 | [diff] [blame] | 1306 | self.assertIs(foo().__await__(), coro) |
| 1307 | |
| 1308 | def test_duck_gen(self): |
| 1309 | class GenLike: |
| 1310 | def send(self): pass |
| 1311 | def throw(self): pass |
| 1312 | def close(self): pass |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1313 | def __iter__(self): pass |
Yury Selivanov | 13f7723 | 2015-05-29 16:19:18 -0400 | [diff] [blame] | 1314 | def __next__(self): pass |
| 1315 | |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1316 | # Setup generator mock object |
| 1317 | gen = unittest.mock.MagicMock(GenLike) |
| 1318 | gen.__iter__ = lambda gen: gen |
| 1319 | gen.__name__ = 'gen' |
| 1320 | gen.__qualname__ = 'test.gen' |
| 1321 | self.assertIsInstance(gen, collections.abc.Generator) |
| 1322 | self.assertIs(gen, iter(gen)) |
| 1323 | |
Yury Selivanov | 13f7723 | 2015-05-29 16:19:18 -0400 | [diff] [blame] | 1324 | @types.coroutine |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1325 | def foo(): return gen |
| 1326 | |
| 1327 | wrapper = foo() |
| 1328 | self.assertIsInstance(wrapper, types._GeneratorWrapper) |
| 1329 | self.assertIs(wrapper.__await__(), wrapper) |
| 1330 | # Wrapper proxies duck generators completely: |
| 1331 | self.assertIs(iter(wrapper), wrapper) |
| 1332 | |
| 1333 | self.assertIsInstance(wrapper, collections.abc.Coroutine) |
| 1334 | self.assertIsInstance(wrapper, collections.abc.Awaitable) |
| 1335 | |
| 1336 | self.assertIs(wrapper.__qualname__, gen.__qualname__) |
| 1337 | self.assertIs(wrapper.__name__, gen.__name__) |
| 1338 | |
| 1339 | # Test AttributeErrors |
Yury Selivanov | 53e6230 | 2015-07-03 00:35:02 -0400 | [diff] [blame] | 1340 | for name in {'gi_running', 'gi_frame', 'gi_code', 'gi_yieldfrom', |
| 1341 | 'cr_running', 'cr_frame', 'cr_code', 'cr_await'}: |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1342 | with self.assertRaises(AttributeError): |
| 1343 | getattr(wrapper, name) |
| 1344 | |
| 1345 | # Test attributes pass-through |
| 1346 | gen.gi_running = object() |
| 1347 | gen.gi_frame = object() |
| 1348 | gen.gi_code = object() |
Yury Selivanov | 53e6230 | 2015-07-03 00:35:02 -0400 | [diff] [blame] | 1349 | gen.gi_yieldfrom = object() |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1350 | self.assertIs(wrapper.gi_running, gen.gi_running) |
| 1351 | self.assertIs(wrapper.gi_frame, gen.gi_frame) |
| 1352 | self.assertIs(wrapper.gi_code, gen.gi_code) |
Yury Selivanov | 53e6230 | 2015-07-03 00:35:02 -0400 | [diff] [blame] | 1353 | self.assertIs(wrapper.gi_yieldfrom, gen.gi_yieldfrom) |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1354 | self.assertIs(wrapper.cr_running, gen.gi_running) |
| 1355 | self.assertIs(wrapper.cr_frame, gen.gi_frame) |
| 1356 | self.assertIs(wrapper.cr_code, gen.gi_code) |
Yury Selivanov | 53e6230 | 2015-07-03 00:35:02 -0400 | [diff] [blame] | 1357 | self.assertIs(wrapper.cr_await, gen.gi_yieldfrom) |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1358 | |
| 1359 | wrapper.close() |
| 1360 | gen.close.assert_called_once_with() |
| 1361 | |
| 1362 | wrapper.send(1) |
| 1363 | gen.send.assert_called_once_with(1) |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 1364 | gen.reset_mock() |
| 1365 | |
| 1366 | next(wrapper) |
| 1367 | gen.__next__.assert_called_once_with() |
| 1368 | gen.reset_mock() |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1369 | |
| 1370 | wrapper.throw(1, 2, 3) |
| 1371 | gen.throw.assert_called_once_with(1, 2, 3) |
| 1372 | gen.reset_mock() |
| 1373 | |
| 1374 | wrapper.throw(1, 2) |
| 1375 | gen.throw.assert_called_once_with(1, 2) |
| 1376 | gen.reset_mock() |
| 1377 | |
| 1378 | wrapper.throw(1) |
| 1379 | gen.throw.assert_called_once_with(1) |
| 1380 | gen.reset_mock() |
| 1381 | |
| 1382 | # Test exceptions propagation |
| 1383 | error = Exception() |
| 1384 | gen.throw.side_effect = error |
| 1385 | try: |
| 1386 | wrapper.throw(1) |
| 1387 | except Exception as ex: |
| 1388 | self.assertIs(ex, error) |
| 1389 | else: |
| 1390 | self.fail('wrapper did not propagate an exception') |
| 1391 | |
| 1392 | # Test invalid args |
| 1393 | gen.reset_mock() |
| 1394 | with self.assertRaises(TypeError): |
| 1395 | wrapper.throw() |
| 1396 | self.assertFalse(gen.throw.called) |
| 1397 | with self.assertRaises(TypeError): |
| 1398 | wrapper.close(1) |
| 1399 | self.assertFalse(gen.close.called) |
| 1400 | with self.assertRaises(TypeError): |
| 1401 | wrapper.send() |
| 1402 | self.assertFalse(gen.send.called) |
| 1403 | |
| 1404 | # Test that we do not double wrap |
| 1405 | @types.coroutine |
| 1406 | def bar(): return wrapper |
| 1407 | self.assertIs(wrapper, bar()) |
| 1408 | |
| 1409 | # Test weakrefs support |
| 1410 | ref = weakref.ref(wrapper) |
| 1411 | self.assertIs(ref(), wrapper) |
| 1412 | |
| 1413 | def test_duck_functional_gen(self): |
| 1414 | class Generator: |
| 1415 | """Emulates the following generator (very clumsy): |
| 1416 | |
| 1417 | def gen(fut): |
| 1418 | result = yield fut |
| 1419 | return result * 2 |
| 1420 | """ |
| 1421 | def __init__(self, fut): |
| 1422 | self._i = 0 |
| 1423 | self._fut = fut |
| 1424 | def __iter__(self): |
| 1425 | return self |
| 1426 | def __next__(self): |
| 1427 | return self.send(None) |
| 1428 | def send(self, v): |
| 1429 | try: |
| 1430 | if self._i == 0: |
| 1431 | assert v is None |
| 1432 | return self._fut |
| 1433 | if self._i == 1: |
| 1434 | raise StopIteration(v * 2) |
| 1435 | if self._i > 1: |
| 1436 | raise StopIteration |
| 1437 | finally: |
| 1438 | self._i += 1 |
| 1439 | def throw(self, tp, *exc): |
| 1440 | self._i = 100 |
| 1441 | if tp is not GeneratorExit: |
| 1442 | raise tp |
| 1443 | def close(self): |
| 1444 | self.throw(GeneratorExit) |
| 1445 | |
| 1446 | @types.coroutine |
| 1447 | def foo(): return Generator('spam') |
| 1448 | |
| 1449 | wrapper = foo() |
| 1450 | self.assertIsInstance(wrapper, types._GeneratorWrapper) |
| 1451 | |
| 1452 | async def corofunc(): |
| 1453 | return await foo() + 100 |
| 1454 | coro = corofunc() |
| 1455 | |
| 1456 | self.assertEqual(coro.send(None), 'spam') |
| 1457 | try: |
| 1458 | coro.send(20) |
| 1459 | except StopIteration as ex: |
| 1460 | self.assertEqual(ex.args[0], 140) |
| 1461 | else: |
| 1462 | self.fail('StopIteration was expected') |
Yury Selivanov | 13f7723 | 2015-05-29 16:19:18 -0400 | [diff] [blame] | 1463 | |
| 1464 | def test_gen(self): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 1465 | def gen_func(): |
| 1466 | yield 1 |
| 1467 | return (yield 2) |
| 1468 | gen = gen_func() |
Yury Selivanov | 13f7723 | 2015-05-29 16:19:18 -0400 | [diff] [blame] | 1469 | @types.coroutine |
| 1470 | def foo(): return gen |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1471 | wrapper = foo() |
| 1472 | self.assertIsInstance(wrapper, types._GeneratorWrapper) |
| 1473 | self.assertIs(wrapper.__await__(), gen) |
Yury Selivanov | 13f7723 | 2015-05-29 16:19:18 -0400 | [diff] [blame] | 1474 | |
| 1475 | for name in ('__name__', '__qualname__', 'gi_code', |
| 1476 | 'gi_running', 'gi_frame'): |
| 1477 | self.assertIs(getattr(foo(), name), |
| 1478 | getattr(gen, name)) |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1479 | self.assertIs(foo().cr_code, gen.gi_code) |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 1480 | |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 1481 | self.assertEqual(next(wrapper), 1) |
| 1482 | self.assertEqual(wrapper.send(None), 2) |
| 1483 | with self.assertRaisesRegex(StopIteration, 'spam'): |
| 1484 | wrapper.send('spam') |
| 1485 | |
| 1486 | gen = gen_func() |
| 1487 | wrapper = foo() |
| 1488 | wrapper.send(None) |
| 1489 | with self.assertRaisesRegex(Exception, 'ham'): |
| 1490 | wrapper.throw(Exception, Exception('ham')) |
| 1491 | |
Yury Selivanov | 4887523c | 2015-07-23 15:58:37 +0300 | [diff] [blame] | 1492 | # decorate foo second time |
| 1493 | foo = types.coroutine(foo) |
| 1494 | self.assertIs(foo().__await__(), gen) |
| 1495 | |
Yury Selivanov | fdbeb2b | 2015-07-03 13:11:35 -0400 | [diff] [blame] | 1496 | def test_returning_itercoro(self): |
| 1497 | @types.coroutine |
| 1498 | def gen(): |
| 1499 | yield |
| 1500 | |
| 1501 | gencoro = gen() |
| 1502 | |
| 1503 | @types.coroutine |
| 1504 | def foo(): |
| 1505 | return gencoro |
| 1506 | |
| 1507 | self.assertIs(foo(), gencoro) |
| 1508 | |
Yury Selivanov | 4887523c | 2015-07-23 15:58:37 +0300 | [diff] [blame] | 1509 | # decorate foo second time |
| 1510 | foo = types.coroutine(foo) |
| 1511 | self.assertIs(foo(), gencoro) |
| 1512 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1513 | def test_genfunc(self): |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1514 | def gen(): yield |
| 1515 | self.assertIs(types.coroutine(gen), gen) |
Yury Selivanov | 4887523c | 2015-07-23 15:58:37 +0300 | [diff] [blame] | 1516 | self.assertIs(types.coroutine(types.coroutine(gen)), gen) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1517 | |
| 1518 | self.assertTrue(gen.__code__.co_flags & inspect.CO_ITERABLE_COROUTINE) |
| 1519 | self.assertFalse(gen.__code__.co_flags & inspect.CO_COROUTINE) |
| 1520 | |
| 1521 | g = gen() |
| 1522 | self.assertTrue(g.gi_code.co_flags & inspect.CO_ITERABLE_COROUTINE) |
| 1523 | self.assertFalse(g.gi_code.co_flags & inspect.CO_COROUTINE) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1524 | |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 1525 | self.assertIs(types.coroutine(gen), gen) |
| 1526 | |
| 1527 | def test_wrapper_object(self): |
| 1528 | def gen(): |
| 1529 | yield |
| 1530 | @types.coroutine |
| 1531 | def coro(): |
| 1532 | return gen() |
| 1533 | |
| 1534 | wrapper = coro() |
| 1535 | self.assertIn('GeneratorWrapper', repr(wrapper)) |
| 1536 | self.assertEqual(repr(wrapper), str(wrapper)) |
| 1537 | self.assertTrue(set(dir(wrapper)).issuperset({ |
| 1538 | '__await__', '__iter__', '__next__', 'cr_code', 'cr_running', |
| 1539 | 'cr_frame', 'gi_code', 'gi_frame', 'gi_running', 'send', |
| 1540 | 'close', 'throw'})) |
| 1541 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1542 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1543 | if __name__ == '__main__': |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 1544 | unittest.main() |