Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1 | import unittest |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 2 | import math |
| 3 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 5 | # Skip this test if the _testcapi module isn't available. |
| 6 | support.import_module('_testcapi') |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 7 | from _testcapi import getargs_keywords, getargs_keyword_only |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 8 | try: |
| 9 | from _testcapi import getargs_L, getargs_K |
| 10 | except ImportError: |
| 11 | getargs_L = None # PY_LONG_LONG not available |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 12 | |
Victor Stinner | 765531d | 2013-03-26 01:11:54 +0100 | [diff] [blame] | 13 | # > How about the following counterproposal. This also changes some of |
| 14 | # > the other format codes to be a little more regular. |
| 15 | # > |
| 16 | # > Code C type Range check |
| 17 | # > |
| 18 | # > b unsigned char 0..UCHAR_MAX |
| 19 | # > h signed short SHRT_MIN..SHRT_MAX |
| 20 | # > B unsigned char none ** |
| 21 | # > H unsigned short none ** |
| 22 | # > k * unsigned long none |
| 23 | # > I * unsigned int 0..UINT_MAX |
| 24 | # |
| 25 | # |
| 26 | # > i int INT_MIN..INT_MAX |
| 27 | # > l long LONG_MIN..LONG_MAX |
| 28 | # |
| 29 | # > K * unsigned long long none |
| 30 | # > L long long LLONG_MIN..LLONG_MAX |
| 31 | # |
| 32 | # > Notes: |
| 33 | # > |
| 34 | # > * New format codes. |
| 35 | # > |
| 36 | # > ** Changed from previous "range-and-a-half" to "none"; the |
| 37 | # > range-and-a-half checking wasn't particularly useful. |
| 38 | # |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 39 | # Plus a C API or two, e.g. PyLong_AsUnsignedLongMask() -> |
| 40 | # unsigned long and PyLong_AsUnsignedLongLongMask() -> unsigned |
Victor Stinner | 765531d | 2013-03-26 01:11:54 +0100 | [diff] [blame] | 41 | # long long (if that exists). |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 42 | |
| 43 | LARGE = 0x7FFFFFFF |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 44 | VERY_LARGE = 0xFF0000121212121212121242 |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 45 | |
| 46 | from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \ |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 47 | INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, \ |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 48 | SHRT_MIN, SHRT_MAX, FLT_MIN, FLT_MAX, DBL_MIN, DBL_MAX |
| 49 | |
| 50 | DBL_MAX_EXP = sys.float_info.max_exp |
| 51 | INF = float('inf') |
| 52 | NAN = float('nan') |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 53 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 54 | # fake, they are not defined in Python's header files |
| 55 | LLONG_MAX = 2**63-1 |
| 56 | LLONG_MIN = -2**63 |
| 57 | ULLONG_MAX = 2**64-1 |
| 58 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 59 | class Int: |
| 60 | def __int__(self): |
| 61 | return 99 |
| 62 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 63 | class IntSubclass(int): |
| 64 | def __int__(self): |
| 65 | return 99 |
| 66 | |
| 67 | class BadInt: |
| 68 | def __int__(self): |
| 69 | return 1.0 |
| 70 | |
| 71 | class BadInt2: |
| 72 | def __int__(self): |
| 73 | return True |
| 74 | |
| 75 | class BadInt3(int): |
| 76 | def __int__(self): |
| 77 | return True |
| 78 | |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 79 | |
| 80 | class Float: |
| 81 | def __float__(self): |
| 82 | return 4.25 |
| 83 | |
| 84 | class FloatSubclass(float): |
| 85 | pass |
| 86 | |
| 87 | class FloatSubclass2(float): |
| 88 | def __float__(self): |
| 89 | return 4.25 |
| 90 | |
| 91 | class BadFloat: |
| 92 | def __float__(self): |
| 93 | return 687 |
| 94 | |
| 95 | class BadFloat2: |
| 96 | def __float__(self): |
| 97 | return FloatSubclass(4.25) |
| 98 | |
| 99 | class BadFloat3(float): |
| 100 | def __float__(self): |
| 101 | return FloatSubclass(4.25) |
| 102 | |
| 103 | |
| 104 | class Complex: |
| 105 | def __complex__(self): |
| 106 | return 4.25+0.5j |
| 107 | |
| 108 | class ComplexSubclass(complex): |
| 109 | pass |
| 110 | |
| 111 | class ComplexSubclass2(complex): |
| 112 | def __complex__(self): |
| 113 | return 4.25+0.5j |
| 114 | |
| 115 | class BadComplex: |
| 116 | def __complex__(self): |
| 117 | return 1.25 |
| 118 | |
| 119 | class BadComplex2: |
| 120 | def __complex__(self): |
| 121 | return ComplexSubclass(4.25+0.5j) |
| 122 | |
| 123 | class BadComplex3(complex): |
| 124 | def __complex__(self): |
| 125 | return ComplexSubclass(4.25+0.5j) |
| 126 | |
| 127 | |
Serhiy Storchaka | acef5de | 2016-05-16 09:55:32 +0300 | [diff] [blame] | 128 | class TupleSubclass(tuple): |
| 129 | pass |
| 130 | |
| 131 | class DictSubclass(dict): |
| 132 | pass |
| 133 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 134 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 135 | class Unsigned_TestCase(unittest.TestCase): |
| 136 | def test_b(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 137 | from _testcapi import getargs_b |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 138 | # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 139 | self.assertRaises(TypeError, getargs_b, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 140 | self.assertEqual(99, getargs_b(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 141 | self.assertEqual(0, getargs_b(IntSubclass())) |
| 142 | self.assertRaises(TypeError, getargs_b, BadInt()) |
| 143 | with self.assertWarns(DeprecationWarning): |
| 144 | self.assertEqual(1, getargs_b(BadInt2())) |
| 145 | self.assertEqual(0, getargs_b(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 146 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 147 | self.assertRaises(OverflowError, getargs_b, -1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 148 | self.assertEqual(0, getargs_b(0)) |
| 149 | self.assertEqual(UCHAR_MAX, getargs_b(UCHAR_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 150 | self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 151 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 152 | self.assertEqual(42, getargs_b(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 153 | self.assertRaises(OverflowError, getargs_b, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 154 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 155 | def test_B(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 156 | from _testcapi import getargs_B |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 157 | # B returns 'unsigned char', no range checking |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 158 | self.assertRaises(TypeError, getargs_B, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 159 | self.assertEqual(99, getargs_B(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 160 | self.assertEqual(0, getargs_B(IntSubclass())) |
| 161 | self.assertRaises(TypeError, getargs_B, BadInt()) |
| 162 | with self.assertWarns(DeprecationWarning): |
| 163 | self.assertEqual(1, getargs_B(BadInt2())) |
| 164 | self.assertEqual(0, getargs_B(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 165 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 166 | self.assertEqual(UCHAR_MAX, getargs_B(-1)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 167 | self.assertEqual(0, getargs_B(0)) |
| 168 | self.assertEqual(UCHAR_MAX, getargs_B(UCHAR_MAX)) |
| 169 | self.assertEqual(0, getargs_B(UCHAR_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 170 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 171 | self.assertEqual(42, getargs_B(42)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 172 | self.assertEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 173 | |
| 174 | def test_H(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 175 | from _testcapi import getargs_H |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 176 | # H returns 'unsigned short', no range checking |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 177 | self.assertRaises(TypeError, getargs_H, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 178 | self.assertEqual(99, getargs_H(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 179 | self.assertEqual(0, getargs_H(IntSubclass())) |
| 180 | self.assertRaises(TypeError, getargs_H, BadInt()) |
| 181 | with self.assertWarns(DeprecationWarning): |
| 182 | self.assertEqual(1, getargs_H(BadInt2())) |
| 183 | self.assertEqual(0, getargs_H(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 184 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 185 | self.assertEqual(USHRT_MAX, getargs_H(-1)) |
| 186 | self.assertEqual(0, getargs_H(0)) |
| 187 | self.assertEqual(USHRT_MAX, getargs_H(USHRT_MAX)) |
| 188 | self.assertEqual(0, getargs_H(USHRT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 189 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 190 | self.assertEqual(42, getargs_H(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 191 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 192 | self.assertEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 193 | |
| 194 | def test_I(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 195 | from _testcapi import getargs_I |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 196 | # I returns 'unsigned int', no range checking |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 197 | self.assertRaises(TypeError, getargs_I, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 198 | self.assertEqual(99, getargs_I(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 199 | self.assertEqual(0, getargs_I(IntSubclass())) |
| 200 | self.assertRaises(TypeError, getargs_I, BadInt()) |
| 201 | with self.assertWarns(DeprecationWarning): |
| 202 | self.assertEqual(1, getargs_I(BadInt2())) |
| 203 | self.assertEqual(0, getargs_I(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 204 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 205 | self.assertEqual(UINT_MAX, getargs_I(-1)) |
| 206 | self.assertEqual(0, getargs_I(0)) |
| 207 | self.assertEqual(UINT_MAX, getargs_I(UINT_MAX)) |
| 208 | self.assertEqual(0, getargs_I(UINT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 209 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 210 | self.assertEqual(42, getargs_I(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 211 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 212 | self.assertEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 213 | |
| 214 | def test_k(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 215 | from _testcapi import getargs_k |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 216 | # k returns 'unsigned long', no range checking |
| 217 | # it does not accept float, or instances with __int__ |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 218 | self.assertRaises(TypeError, getargs_k, 3.14) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 219 | self.assertRaises(TypeError, getargs_k, Int()) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 220 | self.assertEqual(0, getargs_k(IntSubclass())) |
| 221 | self.assertRaises(TypeError, getargs_k, BadInt()) |
| 222 | self.assertRaises(TypeError, getargs_k, BadInt2()) |
| 223 | self.assertEqual(0, getargs_k(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 224 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 225 | self.assertEqual(ULONG_MAX, getargs_k(-1)) |
| 226 | self.assertEqual(0, getargs_k(0)) |
| 227 | self.assertEqual(ULONG_MAX, getargs_k(ULONG_MAX)) |
| 228 | self.assertEqual(0, getargs_k(ULONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 229 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 230 | self.assertEqual(42, getargs_k(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 231 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 232 | self.assertEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 233 | |
| 234 | class Signed_TestCase(unittest.TestCase): |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 235 | def test_h(self): |
| 236 | from _testcapi import getargs_h |
| 237 | # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) |
| 238 | self.assertRaises(TypeError, getargs_h, 3.14) |
| 239 | self.assertEqual(99, getargs_h(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 240 | self.assertEqual(0, getargs_h(IntSubclass())) |
| 241 | self.assertRaises(TypeError, getargs_h, BadInt()) |
| 242 | with self.assertWarns(DeprecationWarning): |
| 243 | self.assertEqual(1, getargs_h(BadInt2())) |
| 244 | self.assertEqual(0, getargs_h(BadInt3())) |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 245 | |
| 246 | self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1) |
| 247 | self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN)) |
| 248 | self.assertEqual(SHRT_MAX, getargs_h(SHRT_MAX)) |
| 249 | self.assertRaises(OverflowError, getargs_h, SHRT_MAX+1) |
| 250 | |
| 251 | self.assertEqual(42, getargs_h(42)) |
| 252 | self.assertRaises(OverflowError, getargs_h, VERY_LARGE) |
| 253 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 254 | def test_i(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 255 | from _testcapi import getargs_i |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 256 | # i returns 'int', and does range checking (INT_MIN ... INT_MAX) |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 257 | self.assertRaises(TypeError, getargs_i, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 258 | self.assertEqual(99, getargs_i(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 259 | self.assertEqual(0, getargs_i(IntSubclass())) |
| 260 | self.assertRaises(TypeError, getargs_i, BadInt()) |
| 261 | with self.assertWarns(DeprecationWarning): |
| 262 | self.assertEqual(1, getargs_i(BadInt2())) |
| 263 | self.assertEqual(0, getargs_i(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 264 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 265 | self.assertRaises(OverflowError, getargs_i, INT_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 266 | self.assertEqual(INT_MIN, getargs_i(INT_MIN)) |
| 267 | self.assertEqual(INT_MAX, getargs_i(INT_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 268 | self.assertRaises(OverflowError, getargs_i, INT_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 269 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 270 | self.assertEqual(42, getargs_i(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 271 | self.assertRaises(OverflowError, getargs_i, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 272 | |
| 273 | def test_l(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 274 | from _testcapi import getargs_l |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 275 | # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 276 | self.assertRaises(TypeError, getargs_l, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 277 | self.assertEqual(99, getargs_l(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 278 | self.assertEqual(0, getargs_l(IntSubclass())) |
| 279 | self.assertRaises(TypeError, getargs_l, BadInt()) |
| 280 | with self.assertWarns(DeprecationWarning): |
| 281 | self.assertEqual(1, getargs_l(BadInt2())) |
| 282 | self.assertEqual(0, getargs_l(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 283 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 284 | self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 285 | self.assertEqual(LONG_MIN, getargs_l(LONG_MIN)) |
| 286 | self.assertEqual(LONG_MAX, getargs_l(LONG_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 287 | self.assertRaises(OverflowError, getargs_l, LONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 288 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 289 | self.assertEqual(42, getargs_l(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 290 | self.assertRaises(OverflowError, getargs_l, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 291 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 292 | def test_n(self): |
| 293 | from _testcapi import getargs_n |
| 294 | # n returns 'Py_ssize_t', and does range checking |
| 295 | # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX) |
Thomas Wouters | d807328 | 2006-04-21 11:36:13 +0000 | [diff] [blame] | 296 | self.assertRaises(TypeError, getargs_n, 3.14) |
Trent Nelson | 3513358 | 2008-04-22 19:02:40 +0000 | [diff] [blame] | 297 | self.assertRaises(TypeError, getargs_n, Int()) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 298 | self.assertEqual(0, getargs_n(IntSubclass())) |
| 299 | self.assertRaises(TypeError, getargs_n, BadInt()) |
| 300 | self.assertRaises(TypeError, getargs_n, BadInt2()) |
| 301 | self.assertEqual(0, getargs_n(BadInt3())) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 302 | |
| 303 | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 304 | self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN)) |
| 305 | self.assertEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 306 | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1) |
| 307 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 308 | self.assertEqual(42, getargs_n(42)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 309 | self.assertRaises(OverflowError, getargs_n, VERY_LARGE) |
| 310 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 311 | |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 312 | @unittest.skipIf(getargs_L is None, 'PY_LONG_LONG is not available') |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 313 | class LongLong_TestCase(unittest.TestCase): |
| 314 | def test_L(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 315 | from _testcapi import getargs_L |
Mark Dickinson | de60401 | 2010-01-01 19:27:32 +0000 | [diff] [blame] | 316 | # L returns 'long long', and does range checking (LLONG_MIN |
| 317 | # ... LLONG_MAX) |
Mark Dickinson | c730131 | 2010-06-10 16:05:10 +0000 | [diff] [blame] | 318 | self.assertRaises(TypeError, getargs_L, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 319 | self.assertRaises(TypeError, getargs_L, "Hello") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 320 | self.assertEqual(99, getargs_L(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 321 | self.assertEqual(0, getargs_L(IntSubclass())) |
| 322 | self.assertRaises(TypeError, getargs_L, BadInt()) |
| 323 | with self.assertWarns(DeprecationWarning): |
| 324 | self.assertEqual(1, getargs_L(BadInt2())) |
| 325 | self.assertEqual(0, getargs_L(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 326 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 327 | self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 328 | self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN)) |
| 329 | self.assertEqual(LLONG_MAX, getargs_L(LLONG_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 330 | self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 331 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 332 | self.assertEqual(42, getargs_L(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 333 | self.assertRaises(OverflowError, getargs_L, VERY_LARGE) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 334 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 335 | def test_K(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 336 | from _testcapi import getargs_K |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 337 | # K return 'unsigned long long', no range checking |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 338 | self.assertRaises(TypeError, getargs_K, 3.14) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 339 | self.assertRaises(TypeError, getargs_K, Int()) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 340 | self.assertEqual(0, getargs_K(IntSubclass())) |
| 341 | self.assertRaises(TypeError, getargs_K, BadInt()) |
| 342 | self.assertRaises(TypeError, getargs_K, BadInt2()) |
| 343 | self.assertEqual(0, getargs_K(BadInt3())) |
| 344 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 345 | self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) |
| 346 | self.assertEqual(0, getargs_K(0)) |
| 347 | self.assertEqual(0, getargs_K(ULLONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 348 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 349 | self.assertEqual(42, getargs_K(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 350 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 351 | self.assertEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 352 | |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 353 | |
| 354 | class Float_TestCase(unittest.TestCase): |
| 355 | def assertEqualWithSign(self, actual, expected): |
| 356 | self.assertEqual(actual, expected) |
| 357 | self.assertEqual(math.copysign(1, actual), math.copysign(1, expected)) |
| 358 | |
| 359 | def test_f(self): |
| 360 | from _testcapi import getargs_f |
| 361 | self.assertEqual(getargs_f(4.25), 4.25) |
| 362 | self.assertEqual(getargs_f(4), 4.0) |
| 363 | self.assertRaises(TypeError, getargs_f, 4.25+0j) |
| 364 | self.assertEqual(getargs_f(Float()), 4.25) |
| 365 | self.assertEqual(getargs_f(FloatSubclass(7.5)), 7.5) |
| 366 | self.assertEqual(getargs_f(FloatSubclass2(7.5)), 7.5) |
| 367 | self.assertRaises(TypeError, getargs_f, BadFloat()) |
| 368 | self.assertEqual(getargs_f(BadFloat2()), 4.25) |
| 369 | self.assertEqual(getargs_f(BadFloat3(7.5)), 7.5) |
| 370 | |
| 371 | for x in (FLT_MIN, -FLT_MIN, FLT_MAX, -FLT_MAX, INF, -INF): |
| 372 | self.assertEqual(getargs_f(x), x) |
| 373 | if FLT_MAX < DBL_MAX: |
| 374 | self.assertEqual(getargs_f(DBL_MAX), INF) |
| 375 | self.assertEqual(getargs_f(-DBL_MAX), -INF) |
| 376 | if FLT_MIN > DBL_MIN: |
| 377 | self.assertEqualWithSign(getargs_f(DBL_MIN), 0.0) |
| 378 | self.assertEqualWithSign(getargs_f(-DBL_MIN), -0.0) |
| 379 | self.assertEqualWithSign(getargs_f(0.0), 0.0) |
| 380 | self.assertEqualWithSign(getargs_f(-0.0), -0.0) |
| 381 | r = getargs_f(NAN) |
| 382 | self.assertNotEqual(r, r) |
| 383 | |
| 384 | def test_d(self): |
| 385 | from _testcapi import getargs_d |
| 386 | self.assertEqual(getargs_d(4.25), 4.25) |
| 387 | self.assertEqual(getargs_d(4), 4.0) |
| 388 | self.assertRaises(TypeError, getargs_d, 4.25+0j) |
| 389 | self.assertEqual(getargs_d(Float()), 4.25) |
| 390 | self.assertEqual(getargs_d(FloatSubclass(7.5)), 7.5) |
| 391 | self.assertEqual(getargs_d(FloatSubclass2(7.5)), 7.5) |
| 392 | self.assertRaises(TypeError, getargs_d, BadFloat()) |
| 393 | self.assertEqual(getargs_d(BadFloat2()), 4.25) |
| 394 | self.assertEqual(getargs_d(BadFloat3(7.5)), 7.5) |
| 395 | |
| 396 | for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): |
| 397 | self.assertEqual(getargs_d(x), x) |
| 398 | self.assertRaises(OverflowError, getargs_d, 1<<DBL_MAX_EXP) |
| 399 | self.assertRaises(OverflowError, getargs_d, -1<<DBL_MAX_EXP) |
| 400 | self.assertEqualWithSign(getargs_d(0.0), 0.0) |
| 401 | self.assertEqualWithSign(getargs_d(-0.0), -0.0) |
| 402 | r = getargs_d(NAN) |
| 403 | self.assertNotEqual(r, r) |
| 404 | |
| 405 | def test_D(self): |
| 406 | from _testcapi import getargs_D |
| 407 | self.assertEqual(getargs_D(4.25+0.5j), 4.25+0.5j) |
| 408 | self.assertEqual(getargs_D(4.25), 4.25+0j) |
| 409 | self.assertEqual(getargs_D(4), 4.0+0j) |
| 410 | self.assertEqual(getargs_D(Complex()), 4.25+0.5j) |
| 411 | self.assertEqual(getargs_D(ComplexSubclass(7.5+0.25j)), 7.5+0.25j) |
| 412 | self.assertEqual(getargs_D(ComplexSubclass2(7.5+0.25j)), 7.5+0.25j) |
| 413 | self.assertRaises(TypeError, getargs_D, BadComplex()) |
| 414 | self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j) |
| 415 | self.assertEqual(getargs_D(BadComplex3(7.5+0.25j)), 7.5+0.25j) |
| 416 | |
| 417 | for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): |
| 418 | c = complex(x, 1.0) |
| 419 | self.assertEqual(getargs_D(c), c) |
| 420 | c = complex(1.0, x) |
| 421 | self.assertEqual(getargs_D(c), c) |
| 422 | self.assertEqualWithSign(getargs_D(complex(0.0, 1.0)).real, 0.0) |
| 423 | self.assertEqualWithSign(getargs_D(complex(-0.0, 1.0)).real, -0.0) |
| 424 | self.assertEqualWithSign(getargs_D(complex(1.0, 0.0)).imag, 0.0) |
| 425 | self.assertEqualWithSign(getargs_D(complex(1.0, -0.0)).imag, -0.0) |
| 426 | |
| 427 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 428 | class Paradox: |
| 429 | "This statement is false." |
| 430 | def __bool__(self): |
| 431 | raise NotImplementedError |
| 432 | |
| 433 | class Boolean_TestCase(unittest.TestCase): |
| 434 | def test_p(self): |
| 435 | from _testcapi import getargs_p |
| 436 | self.assertEqual(0, getargs_p(False)) |
| 437 | self.assertEqual(0, getargs_p(None)) |
| 438 | self.assertEqual(0, getargs_p(0)) |
| 439 | self.assertEqual(0, getargs_p(0.0)) |
| 440 | self.assertEqual(0, getargs_p(0j)) |
| 441 | self.assertEqual(0, getargs_p('')) |
| 442 | self.assertEqual(0, getargs_p(())) |
| 443 | self.assertEqual(0, getargs_p([])) |
| 444 | self.assertEqual(0, getargs_p({})) |
| 445 | |
| 446 | self.assertEqual(1, getargs_p(True)) |
| 447 | self.assertEqual(1, getargs_p(1)) |
| 448 | self.assertEqual(1, getargs_p(1.0)) |
| 449 | self.assertEqual(1, getargs_p(1j)) |
| 450 | self.assertEqual(1, getargs_p('x')) |
| 451 | self.assertEqual(1, getargs_p((1,))) |
| 452 | self.assertEqual(1, getargs_p([1])) |
| 453 | self.assertEqual(1, getargs_p({1:2})) |
| 454 | self.assertEqual(1, getargs_p(unittest.TestCase)) |
| 455 | |
| 456 | self.assertRaises(NotImplementedError, getargs_p, Paradox()) |
| 457 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 458 | |
| 459 | class Tuple_TestCase(unittest.TestCase): |
Serhiy Storchaka | acef5de | 2016-05-16 09:55:32 +0300 | [diff] [blame] | 460 | def test_args(self): |
| 461 | from _testcapi import get_args |
| 462 | |
| 463 | ret = get_args(1, 2) |
| 464 | self.assertEqual(ret, (1, 2)) |
| 465 | self.assertIs(type(ret), tuple) |
| 466 | |
| 467 | ret = get_args(1, *(2, 3)) |
| 468 | self.assertEqual(ret, (1, 2, 3)) |
| 469 | self.assertIs(type(ret), tuple) |
| 470 | |
| 471 | ret = get_args(*[1, 2]) |
| 472 | self.assertEqual(ret, (1, 2)) |
| 473 | self.assertIs(type(ret), tuple) |
| 474 | |
| 475 | ret = get_args(*TupleSubclass([1, 2])) |
| 476 | self.assertEqual(ret, (1, 2)) |
| 477 | self.assertIsInstance(ret, tuple) |
| 478 | |
| 479 | ret = get_args() |
| 480 | self.assertIn(ret, ((), None)) |
| 481 | self.assertIn(type(ret), (tuple, type(None))) |
| 482 | |
| 483 | ret = get_args(*()) |
| 484 | self.assertIn(ret, ((), None)) |
| 485 | self.assertIn(type(ret), (tuple, type(None))) |
| 486 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 487 | def test_tuple(self): |
| 488 | from _testcapi import getargs_tuple |
| 489 | |
| 490 | ret = getargs_tuple(1, (2, 3)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 491 | self.assertEqual(ret, (1,2,3)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 492 | |
| 493 | # make sure invalid tuple arguments are handled correctly |
| 494 | class seq: |
| 495 | def __len__(self): |
| 496 | return 2 |
| 497 | def __getitem__(self, n): |
| 498 | raise ValueError |
| 499 | self.assertRaises(TypeError, getargs_tuple, 1, seq()) |
| 500 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 501 | class Keywords_TestCase(unittest.TestCase): |
Serhiy Storchaka | acef5de | 2016-05-16 09:55:32 +0300 | [diff] [blame] | 502 | def test_kwargs(self): |
| 503 | from _testcapi import get_kwargs |
| 504 | |
| 505 | ret = get_kwargs(a=1, b=2) |
| 506 | self.assertEqual(ret, {'a': 1, 'b': 2}) |
| 507 | self.assertIs(type(ret), dict) |
| 508 | |
| 509 | ret = get_kwargs(a=1, **{'b': 2, 'c': 3}) |
| 510 | self.assertEqual(ret, {'a': 1, 'b': 2, 'c': 3}) |
| 511 | self.assertIs(type(ret), dict) |
| 512 | |
| 513 | ret = get_kwargs(**DictSubclass({'a': 1, 'b': 2})) |
| 514 | self.assertEqual(ret, {'a': 1, 'b': 2}) |
| 515 | self.assertIsInstance(ret, dict) |
| 516 | |
| 517 | ret = get_kwargs() |
| 518 | self.assertIn(ret, ({}, None)) |
| 519 | self.assertIn(type(ret), (dict, type(None))) |
| 520 | |
| 521 | ret = get_kwargs(**{}) |
| 522 | self.assertIn(ret, ({}, None)) |
| 523 | self.assertIn(type(ret), (dict, type(None))) |
| 524 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 525 | def test_positional_args(self): |
| 526 | # using all positional args |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 527 | self.assertEqual( |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 528 | getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10), |
| 529 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 530 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 531 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 532 | def test_mixed_args(self): |
| 533 | # positional and keyword args |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 534 | self.assertEqual( |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 535 | getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10), |
| 536 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 537 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 538 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 539 | def test_keyword_args(self): |
| 540 | # all keywords |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 541 | self.assertEqual( |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 542 | getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10), |
| 543 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 544 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 545 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 546 | def test_optional_args(self): |
| 547 | # missing optional keyword args, skipping tuples |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 548 | self.assertEqual( |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 549 | getargs_keywords(arg1=(1,2), arg2=3, arg5=10), |
| 550 | (1, 2, 3, -1, -1, -1, -1, -1, -1, 10) |
| 551 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 552 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 553 | def test_required_args(self): |
| 554 | # required arg missing |
| 555 | try: |
| 556 | getargs_keywords(arg1=(1,2)) |
| 557 | except TypeError as err: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 558 | self.assertEqual(str(err), "Required argument 'arg2' (pos 2) not found") |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 559 | else: |
| 560 | self.fail('TypeError should have been raised') |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 561 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 562 | def test_too_many_args(self): |
| 563 | try: |
| 564 | getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111) |
| 565 | except TypeError as err: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 566 | self.assertEqual(str(err), "function takes at most 5 arguments (6 given)") |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 567 | else: |
| 568 | self.fail('TypeError should have been raised') |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 569 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 570 | def test_invalid_keyword(self): |
| 571 | # extraneous keyword arg |
| 572 | try: |
| 573 | getargs_keywords((1,2),3,arg5=10,arg666=666) |
| 574 | except TypeError as err: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 575 | self.assertEqual(str(err), "'arg666' is an invalid keyword argument for this function") |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 576 | else: |
| 577 | self.fail('TypeError should have been raised') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 578 | |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 579 | def test_surrogate_keyword(self): |
| 580 | try: |
| 581 | getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), **{'\uDC80': 10}) |
| 582 | except TypeError as err: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 583 | self.assertEqual(str(err), "'\udc80' is an invalid keyword argument for this function") |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 584 | else: |
| 585 | self.fail('TypeError should have been raised') |
| 586 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 587 | class KeywordOnly_TestCase(unittest.TestCase): |
| 588 | def test_positional_args(self): |
| 589 | # using all possible positional args |
| 590 | self.assertEqual( |
| 591 | getargs_keyword_only(1, 2), |
| 592 | (1, 2, -1) |
| 593 | ) |
| 594 | |
| 595 | def test_mixed_args(self): |
| 596 | # positional and keyword args |
| 597 | self.assertEqual( |
| 598 | getargs_keyword_only(1, 2, keyword_only=3), |
| 599 | (1, 2, 3) |
| 600 | ) |
| 601 | |
| 602 | def test_keyword_args(self): |
| 603 | # all keywords |
| 604 | self.assertEqual( |
| 605 | getargs_keyword_only(required=1, optional=2, keyword_only=3), |
| 606 | (1, 2, 3) |
| 607 | ) |
| 608 | |
| 609 | def test_optional_args(self): |
| 610 | # missing optional keyword args, skipping tuples |
| 611 | self.assertEqual( |
| 612 | getargs_keyword_only(required=1, optional=2), |
| 613 | (1, 2, -1) |
| 614 | ) |
| 615 | self.assertEqual( |
| 616 | getargs_keyword_only(required=1, keyword_only=3), |
| 617 | (1, -1, 3) |
| 618 | ) |
| 619 | |
| 620 | def test_required_args(self): |
| 621 | self.assertEqual( |
| 622 | getargs_keyword_only(1), |
| 623 | (1, -1, -1) |
| 624 | ) |
| 625 | self.assertEqual( |
| 626 | getargs_keyword_only(required=1), |
| 627 | (1, -1, -1) |
| 628 | ) |
| 629 | # required arg missing |
| 630 | with self.assertRaisesRegex(TypeError, |
| 631 | "Required argument 'required' \(pos 1\) not found"): |
| 632 | getargs_keyword_only(optional=2) |
| 633 | |
| 634 | with self.assertRaisesRegex(TypeError, |
| 635 | "Required argument 'required' \(pos 1\) not found"): |
| 636 | getargs_keyword_only(keyword_only=3) |
| 637 | |
| 638 | def test_too_many_args(self): |
| 639 | with self.assertRaisesRegex(TypeError, |
| 640 | "Function takes at most 2 positional arguments \(3 given\)"): |
| 641 | getargs_keyword_only(1, 2, 3) |
| 642 | |
| 643 | with self.assertRaisesRegex(TypeError, |
| 644 | "function takes at most 3 arguments \(4 given\)"): |
| 645 | getargs_keyword_only(1, 2, 3, keyword_only=5) |
| 646 | |
| 647 | def test_invalid_keyword(self): |
| 648 | # extraneous keyword arg |
| 649 | with self.assertRaisesRegex(TypeError, |
| 650 | "'monster' is an invalid keyword argument for this function"): |
| 651 | getargs_keyword_only(1, 2, monster=666) |
| 652 | |
| 653 | def test_surrogate_keyword(self): |
| 654 | with self.assertRaisesRegex(TypeError, |
| 655 | "'\udc80' is an invalid keyword argument for this function"): |
| 656 | getargs_keyword_only(1, 2, **{'\uDC80': 10}) |
| 657 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 658 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 659 | class Bytes_TestCase(unittest.TestCase): |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 660 | def test_c(self): |
| 661 | from _testcapi import getargs_c |
| 662 | self.assertRaises(TypeError, getargs_c, b'abc') # len > 1 |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 663 | self.assertEqual(getargs_c(b'a'), 97) |
| 664 | self.assertEqual(getargs_c(bytearray(b'a')), 97) |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 665 | self.assertRaises(TypeError, getargs_c, memoryview(b'a')) |
| 666 | self.assertRaises(TypeError, getargs_c, 's') |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 667 | self.assertRaises(TypeError, getargs_c, 97) |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 668 | self.assertRaises(TypeError, getargs_c, None) |
| 669 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 670 | def test_y(self): |
| 671 | from _testcapi import getargs_y |
| 672 | self.assertRaises(TypeError, getargs_y, 'abc\xe9') |
| 673 | self.assertEqual(getargs_y(b'bytes'), b'bytes') |
| 674 | self.assertRaises(ValueError, getargs_y, b'nul:\0') |
| 675 | self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray')) |
| 676 | self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview')) |
| 677 | self.assertRaises(TypeError, getargs_y, None) |
| 678 | |
| 679 | def test_y_star(self): |
| 680 | from _testcapi import getargs_y_star |
| 681 | self.assertRaises(TypeError, getargs_y_star, 'abc\xe9') |
| 682 | self.assertEqual(getargs_y_star(b'bytes'), b'bytes') |
| 683 | self.assertEqual(getargs_y_star(b'nul:\0'), b'nul:\0') |
| 684 | self.assertEqual(getargs_y_star(bytearray(b'bytearray')), b'bytearray') |
| 685 | self.assertEqual(getargs_y_star(memoryview(b'memoryview')), b'memoryview') |
| 686 | self.assertRaises(TypeError, getargs_y_star, None) |
| 687 | |
| 688 | def test_y_hash(self): |
| 689 | from _testcapi import getargs_y_hash |
| 690 | self.assertRaises(TypeError, getargs_y_hash, 'abc\xe9') |
| 691 | self.assertEqual(getargs_y_hash(b'bytes'), b'bytes') |
| 692 | self.assertEqual(getargs_y_hash(b'nul:\0'), b'nul:\0') |
| 693 | self.assertRaises(TypeError, getargs_y_hash, bytearray(b'bytearray')) |
| 694 | self.assertRaises(TypeError, getargs_y_hash, memoryview(b'memoryview')) |
| 695 | self.assertRaises(TypeError, getargs_y_hash, None) |
| 696 | |
| 697 | def test_w_star(self): |
| 698 | # getargs_w_star() modifies first and last byte |
| 699 | from _testcapi import getargs_w_star |
| 700 | self.assertRaises(TypeError, getargs_w_star, 'abc\xe9') |
| 701 | self.assertRaises(TypeError, getargs_w_star, b'bytes') |
| 702 | self.assertRaises(TypeError, getargs_w_star, b'nul:\0') |
| 703 | self.assertRaises(TypeError, getargs_w_star, memoryview(b'bytes')) |
| 704 | buf = bytearray(b'bytearray') |
| 705 | self.assertEqual(getargs_w_star(buf), b'[ytearra]') |
| 706 | self.assertEqual(buf, bytearray(b'[ytearra]')) |
| 707 | buf = bytearray(b'memoryview') |
| 708 | self.assertEqual(getargs_w_star(memoryview(buf)), b'[emoryvie]') |
| 709 | self.assertEqual(buf, bytearray(b'[emoryvie]')) |
| 710 | self.assertRaises(TypeError, getargs_w_star, None) |
| 711 | |
| 712 | |
| 713 | class String_TestCase(unittest.TestCase): |
| 714 | def test_C(self): |
| 715 | from _testcapi import getargs_C |
| 716 | self.assertRaises(TypeError, getargs_C, 'abc') # len > 1 |
| 717 | self.assertEqual(getargs_C('a'), 97) |
| 718 | self.assertEqual(getargs_C('\u20ac'), 0x20ac) |
| 719 | self.assertEqual(getargs_C('\U0001f40d'), 0x1f40d) |
| 720 | self.assertRaises(TypeError, getargs_C, b'a') |
| 721 | self.assertRaises(TypeError, getargs_C, bytearray(b'a')) |
| 722 | self.assertRaises(TypeError, getargs_C, memoryview(b'a')) |
| 723 | self.assertRaises(TypeError, getargs_C, 97) |
| 724 | self.assertRaises(TypeError, getargs_C, None) |
| 725 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 726 | def test_s(self): |
| 727 | from _testcapi import getargs_s |
| 728 | self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9') |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 729 | self.assertRaises(ValueError, getargs_s, 'nul:\0') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 730 | self.assertRaises(TypeError, getargs_s, b'bytes') |
| 731 | self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray')) |
| 732 | self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview')) |
| 733 | self.assertRaises(TypeError, getargs_s, None) |
| 734 | |
| 735 | def test_s_star(self): |
| 736 | from _testcapi import getargs_s_star |
| 737 | self.assertEqual(getargs_s_star('abc\xe9'), b'abc\xc3\xa9') |
| 738 | self.assertEqual(getargs_s_star('nul:\0'), b'nul:\0') |
| 739 | self.assertEqual(getargs_s_star(b'bytes'), b'bytes') |
| 740 | self.assertEqual(getargs_s_star(bytearray(b'bytearray')), b'bytearray') |
| 741 | self.assertEqual(getargs_s_star(memoryview(b'memoryview')), b'memoryview') |
| 742 | self.assertRaises(TypeError, getargs_s_star, None) |
| 743 | |
| 744 | def test_s_hash(self): |
| 745 | from _testcapi import getargs_s_hash |
| 746 | self.assertEqual(getargs_s_hash('abc\xe9'), b'abc\xc3\xa9') |
| 747 | self.assertEqual(getargs_s_hash('nul:\0'), b'nul:\0') |
| 748 | self.assertEqual(getargs_s_hash(b'bytes'), b'bytes') |
| 749 | self.assertRaises(TypeError, getargs_s_hash, bytearray(b'bytearray')) |
| 750 | self.assertRaises(TypeError, getargs_s_hash, memoryview(b'memoryview')) |
| 751 | self.assertRaises(TypeError, getargs_s_hash, None) |
| 752 | |
| 753 | def test_z(self): |
| 754 | from _testcapi import getargs_z |
| 755 | self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9') |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 756 | self.assertRaises(ValueError, getargs_z, 'nul:\0') |
Victor Stinner | 4aae1eb | 2010-06-24 22:08:25 +0000 | [diff] [blame] | 757 | self.assertRaises(TypeError, getargs_z, b'bytes') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 758 | self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray')) |
| 759 | self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview')) |
| 760 | self.assertIsNone(getargs_z(None)) |
| 761 | |
| 762 | def test_z_star(self): |
| 763 | from _testcapi import getargs_z_star |
| 764 | self.assertEqual(getargs_z_star('abc\xe9'), b'abc\xc3\xa9') |
| 765 | self.assertEqual(getargs_z_star('nul:\0'), b'nul:\0') |
| 766 | self.assertEqual(getargs_z_star(b'bytes'), b'bytes') |
| 767 | self.assertEqual(getargs_z_star(bytearray(b'bytearray')), b'bytearray') |
| 768 | self.assertEqual(getargs_z_star(memoryview(b'memoryview')), b'memoryview') |
| 769 | self.assertIsNone(getargs_z_star(None)) |
| 770 | |
| 771 | def test_z_hash(self): |
| 772 | from _testcapi import getargs_z_hash |
| 773 | self.assertEqual(getargs_z_hash('abc\xe9'), b'abc\xc3\xa9') |
| 774 | self.assertEqual(getargs_z_hash('nul:\0'), b'nul:\0') |
| 775 | self.assertEqual(getargs_z_hash(b'bytes'), b'bytes') |
| 776 | self.assertRaises(TypeError, getargs_z_hash, bytearray(b'bytearray')) |
| 777 | self.assertRaises(TypeError, getargs_z_hash, memoryview(b'memoryview')) |
| 778 | self.assertIsNone(getargs_z_hash(None)) |
| 779 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 780 | def test_es(self): |
| 781 | from _testcapi import getargs_es |
| 782 | self.assertEqual(getargs_es('abc\xe9'), b'abc\xc3\xa9') |
| 783 | self.assertEqual(getargs_es('abc\xe9', 'latin1'), b'abc\xe9') |
| 784 | self.assertRaises(UnicodeEncodeError, getargs_es, 'abc\xe9', 'ascii') |
| 785 | self.assertRaises(LookupError, getargs_es, 'abc\xe9', 'spam') |
| 786 | self.assertRaises(TypeError, getargs_es, b'bytes', 'latin1') |
| 787 | self.assertRaises(TypeError, getargs_es, bytearray(b'bytearray'), 'latin1') |
| 788 | self.assertRaises(TypeError, getargs_es, memoryview(b'memoryview'), 'latin1') |
| 789 | self.assertRaises(TypeError, getargs_es, None, 'latin1') |
| 790 | self.assertRaises(TypeError, getargs_es, 'nul:\0', 'latin1') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 791 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 792 | def test_et(self): |
| 793 | from _testcapi import getargs_et |
| 794 | self.assertEqual(getargs_et('abc\xe9'), b'abc\xc3\xa9') |
| 795 | self.assertEqual(getargs_et('abc\xe9', 'latin1'), b'abc\xe9') |
| 796 | self.assertRaises(UnicodeEncodeError, getargs_et, 'abc\xe9', 'ascii') |
| 797 | self.assertRaises(LookupError, getargs_et, 'abc\xe9', 'spam') |
| 798 | self.assertEqual(getargs_et(b'bytes', 'latin1'), b'bytes') |
| 799 | self.assertEqual(getargs_et(bytearray(b'bytearray'), 'latin1'), b'bytearray') |
| 800 | self.assertRaises(TypeError, getargs_et, memoryview(b'memoryview'), 'latin1') |
| 801 | self.assertRaises(TypeError, getargs_et, None, 'latin1') |
| 802 | self.assertRaises(TypeError, getargs_et, 'nul:\0', 'latin1') |
| 803 | self.assertRaises(TypeError, getargs_et, b'nul:\0', 'latin1') |
| 804 | self.assertRaises(TypeError, getargs_et, bytearray(b'nul:\0'), 'latin1') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 805 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 806 | def test_es_hash(self): |
| 807 | from _testcapi import getargs_es_hash |
| 808 | self.assertEqual(getargs_es_hash('abc\xe9'), b'abc\xc3\xa9') |
| 809 | self.assertEqual(getargs_es_hash('abc\xe9', 'latin1'), b'abc\xe9') |
| 810 | self.assertRaises(UnicodeEncodeError, getargs_es_hash, 'abc\xe9', 'ascii') |
| 811 | self.assertRaises(LookupError, getargs_es_hash, 'abc\xe9', 'spam') |
| 812 | self.assertRaises(TypeError, getargs_es_hash, b'bytes', 'latin1') |
| 813 | self.assertRaises(TypeError, getargs_es_hash, bytearray(b'bytearray'), 'latin1') |
| 814 | self.assertRaises(TypeError, getargs_es_hash, memoryview(b'memoryview'), 'latin1') |
| 815 | self.assertRaises(TypeError, getargs_es_hash, None, 'latin1') |
| 816 | self.assertEqual(getargs_es_hash('nul:\0', 'latin1'), b'nul:\0') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 817 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 818 | buf = bytearray(b'x'*8) |
| 819 | self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 820 | self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx')) |
| 821 | buf = bytearray(b'x'*5) |
| 822 | self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 823 | self.assertEqual(buf, bytearray(b'abc\xe9\x00')) |
| 824 | buf = bytearray(b'x'*4) |
| 825 | self.assertRaises(TypeError, getargs_es_hash, 'abc\xe9', 'latin1', buf) |
| 826 | self.assertEqual(buf, bytearray(b'x'*4)) |
| 827 | buf = bytearray() |
| 828 | self.assertRaises(TypeError, getargs_es_hash, 'abc\xe9', 'latin1', buf) |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 829 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 830 | def test_et_hash(self): |
| 831 | from _testcapi import getargs_et_hash |
| 832 | self.assertEqual(getargs_et_hash('abc\xe9'), b'abc\xc3\xa9') |
| 833 | self.assertEqual(getargs_et_hash('abc\xe9', 'latin1'), b'abc\xe9') |
| 834 | self.assertRaises(UnicodeEncodeError, getargs_et_hash, 'abc\xe9', 'ascii') |
| 835 | self.assertRaises(LookupError, getargs_et_hash, 'abc\xe9', 'spam') |
| 836 | self.assertEqual(getargs_et_hash(b'bytes', 'latin1'), b'bytes') |
| 837 | self.assertEqual(getargs_et_hash(bytearray(b'bytearray'), 'latin1'), b'bytearray') |
| 838 | self.assertRaises(TypeError, getargs_et_hash, memoryview(b'memoryview'), 'latin1') |
| 839 | self.assertRaises(TypeError, getargs_et_hash, None, 'latin1') |
| 840 | self.assertEqual(getargs_et_hash('nul:\0', 'latin1'), b'nul:\0') |
| 841 | self.assertEqual(getargs_et_hash(b'nul:\0', 'latin1'), b'nul:\0') |
| 842 | self.assertEqual(getargs_et_hash(bytearray(b'nul:\0'), 'latin1'), b'nul:\0') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 843 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 844 | buf = bytearray(b'x'*8) |
| 845 | self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 846 | self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx')) |
| 847 | buf = bytearray(b'x'*5) |
| 848 | self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 849 | self.assertEqual(buf, bytearray(b'abc\xe9\x00')) |
| 850 | buf = bytearray(b'x'*4) |
| 851 | self.assertRaises(TypeError, getargs_et_hash, 'abc\xe9', 'latin1', buf) |
| 852 | self.assertEqual(buf, bytearray(b'x'*4)) |
| 853 | buf = bytearray() |
| 854 | self.assertRaises(TypeError, getargs_et_hash, 'abc\xe9', 'latin1', buf) |
| 855 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 856 | def test_u(self): |
| 857 | from _testcapi import getargs_u |
| 858 | self.assertEqual(getargs_u('abc\xe9'), 'abc\xe9') |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 859 | self.assertRaises(ValueError, getargs_u, 'nul:\0') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 860 | self.assertRaises(TypeError, getargs_u, b'bytes') |
| 861 | self.assertRaises(TypeError, getargs_u, bytearray(b'bytearray')) |
| 862 | self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview')) |
| 863 | self.assertRaises(TypeError, getargs_u, None) |
| 864 | |
| 865 | def test_u_hash(self): |
| 866 | from _testcapi import getargs_u_hash |
| 867 | self.assertEqual(getargs_u_hash('abc\xe9'), 'abc\xe9') |
| 868 | self.assertEqual(getargs_u_hash('nul:\0'), 'nul:\0') |
| 869 | self.assertRaises(TypeError, getargs_u_hash, b'bytes') |
| 870 | self.assertRaises(TypeError, getargs_u_hash, bytearray(b'bytearray')) |
| 871 | self.assertRaises(TypeError, getargs_u_hash, memoryview(b'memoryview')) |
| 872 | self.assertRaises(TypeError, getargs_u_hash, None) |
| 873 | |
| 874 | def test_Z(self): |
| 875 | from _testcapi import getargs_Z |
| 876 | self.assertEqual(getargs_Z('abc\xe9'), 'abc\xe9') |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 877 | self.assertRaises(ValueError, getargs_Z, 'nul:\0') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 878 | self.assertRaises(TypeError, getargs_Z, b'bytes') |
| 879 | self.assertRaises(TypeError, getargs_Z, bytearray(b'bytearray')) |
| 880 | self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview')) |
| 881 | self.assertIsNone(getargs_Z(None)) |
| 882 | |
| 883 | def test_Z_hash(self): |
| 884 | from _testcapi import getargs_Z_hash |
| 885 | self.assertEqual(getargs_Z_hash('abc\xe9'), 'abc\xe9') |
| 886 | self.assertEqual(getargs_Z_hash('nul:\0'), 'nul:\0') |
| 887 | self.assertRaises(TypeError, getargs_Z_hash, b'bytes') |
| 888 | self.assertRaises(TypeError, getargs_Z_hash, bytearray(b'bytearray')) |
| 889 | self.assertRaises(TypeError, getargs_Z_hash, memoryview(b'memoryview')) |
| 890 | self.assertIsNone(getargs_Z_hash(None)) |
| 891 | |
| 892 | |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 893 | class Object_TestCase(unittest.TestCase): |
| 894 | def test_S(self): |
| 895 | from _testcapi import getargs_S |
| 896 | obj = b'bytes' |
| 897 | self.assertIs(getargs_S(obj), obj) |
| 898 | self.assertRaises(TypeError, getargs_S, bytearray(b'bytearray')) |
| 899 | self.assertRaises(TypeError, getargs_S, 'str') |
| 900 | self.assertRaises(TypeError, getargs_S, None) |
| 901 | self.assertRaises(TypeError, getargs_S, memoryview(obj)) |
| 902 | |
| 903 | def test_Y(self): |
| 904 | from _testcapi import getargs_Y |
| 905 | obj = bytearray(b'bytearray') |
| 906 | self.assertIs(getargs_Y(obj), obj) |
| 907 | self.assertRaises(TypeError, getargs_Y, b'bytes') |
| 908 | self.assertRaises(TypeError, getargs_Y, 'str') |
| 909 | self.assertRaises(TypeError, getargs_Y, None) |
| 910 | self.assertRaises(TypeError, getargs_Y, memoryview(obj)) |
| 911 | |
| 912 | def test_U(self): |
| 913 | from _testcapi import getargs_U |
| 914 | obj = 'str' |
| 915 | self.assertIs(getargs_U(obj), obj) |
| 916 | self.assertRaises(TypeError, getargs_U, b'bytes') |
| 917 | self.assertRaises(TypeError, getargs_U, bytearray(b'bytearray')) |
| 918 | self.assertRaises(TypeError, getargs_U, None) |
| 919 | |
| 920 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 921 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 922 | unittest.main() |