Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 3 | # Skip this test if the _testcapi module isn't available. |
| 4 | support.import_module('_testcapi') |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 5 | from _testcapi import getargs_keywords, getargs_keyword_only |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 6 | try: |
| 7 | from _testcapi import getargs_L, getargs_K |
| 8 | except ImportError: |
| 9 | getargs_L = None # PY_LONG_LONG not available |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 10 | |
Victor Stinner | 765531d | 2013-03-26 01:11:54 +0100 | [diff] [blame] | 11 | # > How about the following counterproposal. This also changes some of |
| 12 | # > the other format codes to be a little more regular. |
| 13 | # > |
| 14 | # > Code C type Range check |
| 15 | # > |
| 16 | # > b unsigned char 0..UCHAR_MAX |
| 17 | # > h signed short SHRT_MIN..SHRT_MAX |
| 18 | # > B unsigned char none ** |
| 19 | # > H unsigned short none ** |
| 20 | # > k * unsigned long none |
| 21 | # > I * unsigned int 0..UINT_MAX |
| 22 | # |
| 23 | # |
| 24 | # > i int INT_MIN..INT_MAX |
| 25 | # > l long LONG_MIN..LONG_MAX |
| 26 | # |
| 27 | # > K * unsigned long long none |
| 28 | # > L long long LLONG_MIN..LLONG_MAX |
| 29 | # |
| 30 | # > Notes: |
| 31 | # > |
| 32 | # > * New format codes. |
| 33 | # > |
| 34 | # > ** Changed from previous "range-and-a-half" to "none"; the |
| 35 | # > range-and-a-half checking wasn't particularly useful. |
| 36 | # |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 37 | # Plus a C API or two, e.g. PyLong_AsUnsignedLongMask() -> |
| 38 | # unsigned long and PyLong_AsUnsignedLongLongMask() -> unsigned |
Victor Stinner | 765531d | 2013-03-26 01:11:54 +0100 | [diff] [blame] | 39 | # long long (if that exists). |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 40 | |
| 41 | LARGE = 0x7FFFFFFF |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 42 | VERY_LARGE = 0xFF0000121212121212121242 |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 43 | |
| 44 | 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] | 45 | INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, \ |
| 46 | SHRT_MIN, SHRT_MAX |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 47 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 48 | # fake, they are not defined in Python's header files |
| 49 | LLONG_MAX = 2**63-1 |
| 50 | LLONG_MIN = -2**63 |
| 51 | ULLONG_MAX = 2**64-1 |
| 52 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 53 | class Int: |
| 54 | def __int__(self): |
| 55 | return 99 |
| 56 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 57 | class IntSubclass(int): |
| 58 | def __int__(self): |
| 59 | return 99 |
| 60 | |
| 61 | class BadInt: |
| 62 | def __int__(self): |
| 63 | return 1.0 |
| 64 | |
| 65 | class BadInt2: |
| 66 | def __int__(self): |
| 67 | return True |
| 68 | |
| 69 | class BadInt3(int): |
| 70 | def __int__(self): |
| 71 | return True |
| 72 | |
| 73 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 74 | class Unsigned_TestCase(unittest.TestCase): |
| 75 | def test_b(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 76 | from _testcapi import getargs_b |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 77 | # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 78 | self.assertRaises(TypeError, getargs_b, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 79 | self.assertEqual(99, getargs_b(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 80 | self.assertEqual(0, getargs_b(IntSubclass())) |
| 81 | self.assertRaises(TypeError, getargs_b, BadInt()) |
| 82 | with self.assertWarns(DeprecationWarning): |
| 83 | self.assertEqual(1, getargs_b(BadInt2())) |
| 84 | self.assertEqual(0, getargs_b(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 85 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 86 | self.assertRaises(OverflowError, getargs_b, -1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 87 | self.assertEqual(0, getargs_b(0)) |
| 88 | self.assertEqual(UCHAR_MAX, getargs_b(UCHAR_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 89 | self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 90 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 91 | self.assertEqual(42, getargs_b(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 92 | self.assertRaises(OverflowError, getargs_b, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 93 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 94 | def test_B(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 95 | from _testcapi import getargs_B |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 96 | # B returns 'unsigned char', no range checking |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 97 | self.assertRaises(TypeError, getargs_B, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 98 | self.assertEqual(99, getargs_B(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 99 | self.assertEqual(0, getargs_B(IntSubclass())) |
| 100 | self.assertRaises(TypeError, getargs_B, BadInt()) |
| 101 | with self.assertWarns(DeprecationWarning): |
| 102 | self.assertEqual(1, getargs_B(BadInt2())) |
| 103 | self.assertEqual(0, getargs_B(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 104 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 105 | self.assertEqual(UCHAR_MAX, getargs_B(-1)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 106 | self.assertEqual(0, getargs_B(0)) |
| 107 | self.assertEqual(UCHAR_MAX, getargs_B(UCHAR_MAX)) |
| 108 | self.assertEqual(0, getargs_B(UCHAR_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 109 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 110 | self.assertEqual(42, getargs_B(42)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 111 | self.assertEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 112 | |
| 113 | def test_H(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 114 | from _testcapi import getargs_H |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 115 | # H returns 'unsigned short', no range checking |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 116 | self.assertRaises(TypeError, getargs_H, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 117 | self.assertEqual(99, getargs_H(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 118 | self.assertEqual(0, getargs_H(IntSubclass())) |
| 119 | self.assertRaises(TypeError, getargs_H, BadInt()) |
| 120 | with self.assertWarns(DeprecationWarning): |
| 121 | self.assertEqual(1, getargs_H(BadInt2())) |
| 122 | self.assertEqual(0, getargs_H(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 123 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 124 | self.assertEqual(USHRT_MAX, getargs_H(-1)) |
| 125 | self.assertEqual(0, getargs_H(0)) |
| 126 | self.assertEqual(USHRT_MAX, getargs_H(USHRT_MAX)) |
| 127 | self.assertEqual(0, getargs_H(USHRT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 128 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 129 | self.assertEqual(42, getargs_H(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 130 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 131 | self.assertEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 132 | |
| 133 | def test_I(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 134 | from _testcapi import getargs_I |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 135 | # I returns 'unsigned int', no range checking |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 136 | self.assertRaises(TypeError, getargs_I, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 137 | self.assertEqual(99, getargs_I(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 138 | self.assertEqual(0, getargs_I(IntSubclass())) |
| 139 | self.assertRaises(TypeError, getargs_I, BadInt()) |
| 140 | with self.assertWarns(DeprecationWarning): |
| 141 | self.assertEqual(1, getargs_I(BadInt2())) |
| 142 | self.assertEqual(0, getargs_I(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 143 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 144 | self.assertEqual(UINT_MAX, getargs_I(-1)) |
| 145 | self.assertEqual(0, getargs_I(0)) |
| 146 | self.assertEqual(UINT_MAX, getargs_I(UINT_MAX)) |
| 147 | self.assertEqual(0, getargs_I(UINT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 148 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 149 | self.assertEqual(42, getargs_I(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 150 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 151 | self.assertEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 152 | |
| 153 | def test_k(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 154 | from _testcapi import getargs_k |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 155 | # k returns 'unsigned long', no range checking |
| 156 | # it does not accept float, or instances with __int__ |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 157 | self.assertRaises(TypeError, getargs_k, 3.14) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 158 | self.assertRaises(TypeError, getargs_k, Int()) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 159 | self.assertEqual(0, getargs_k(IntSubclass())) |
| 160 | self.assertRaises(TypeError, getargs_k, BadInt()) |
| 161 | self.assertRaises(TypeError, getargs_k, BadInt2()) |
| 162 | self.assertEqual(0, getargs_k(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 163 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 164 | self.assertEqual(ULONG_MAX, getargs_k(-1)) |
| 165 | self.assertEqual(0, getargs_k(0)) |
| 166 | self.assertEqual(ULONG_MAX, getargs_k(ULONG_MAX)) |
| 167 | self.assertEqual(0, getargs_k(ULONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 168 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 169 | self.assertEqual(42, getargs_k(42)) |
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(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 172 | |
| 173 | class Signed_TestCase(unittest.TestCase): |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 174 | def test_h(self): |
| 175 | from _testcapi import getargs_h |
| 176 | # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) |
| 177 | self.assertRaises(TypeError, getargs_h, 3.14) |
| 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())) |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 184 | |
| 185 | self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1) |
| 186 | self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN)) |
| 187 | self.assertEqual(SHRT_MAX, getargs_h(SHRT_MAX)) |
| 188 | self.assertRaises(OverflowError, getargs_h, SHRT_MAX+1) |
| 189 | |
| 190 | self.assertEqual(42, getargs_h(42)) |
| 191 | self.assertRaises(OverflowError, getargs_h, VERY_LARGE) |
| 192 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 193 | def test_i(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 194 | from _testcapi import getargs_i |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 195 | # i returns 'int', and does range checking (INT_MIN ... INT_MAX) |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 196 | self.assertRaises(TypeError, getargs_i, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 197 | self.assertEqual(99, getargs_i(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 198 | self.assertEqual(0, getargs_i(IntSubclass())) |
| 199 | self.assertRaises(TypeError, getargs_i, BadInt()) |
| 200 | with self.assertWarns(DeprecationWarning): |
| 201 | self.assertEqual(1, getargs_i(BadInt2())) |
| 202 | self.assertEqual(0, getargs_i(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 203 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 204 | self.assertRaises(OverflowError, getargs_i, INT_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 205 | self.assertEqual(INT_MIN, getargs_i(INT_MIN)) |
| 206 | self.assertEqual(INT_MAX, getargs_i(INT_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 207 | self.assertRaises(OverflowError, getargs_i, INT_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 208 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 209 | self.assertEqual(42, getargs_i(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 210 | self.assertRaises(OverflowError, getargs_i, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 211 | |
| 212 | def test_l(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 213 | from _testcapi import getargs_l |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 214 | # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 215 | self.assertRaises(TypeError, getargs_l, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 216 | self.assertEqual(99, getargs_l(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 217 | self.assertEqual(0, getargs_l(IntSubclass())) |
| 218 | self.assertRaises(TypeError, getargs_l, BadInt()) |
| 219 | with self.assertWarns(DeprecationWarning): |
| 220 | self.assertEqual(1, getargs_l(BadInt2())) |
| 221 | self.assertEqual(0, getargs_l(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 222 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 223 | self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 224 | self.assertEqual(LONG_MIN, getargs_l(LONG_MIN)) |
| 225 | self.assertEqual(LONG_MAX, getargs_l(LONG_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 226 | self.assertRaises(OverflowError, getargs_l, LONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 227 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 228 | self.assertEqual(42, getargs_l(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 229 | self.assertRaises(OverflowError, getargs_l, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 230 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 231 | def test_n(self): |
| 232 | from _testcapi import getargs_n |
| 233 | # n returns 'Py_ssize_t', and does range checking |
| 234 | # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX) |
Thomas Wouters | d807328 | 2006-04-21 11:36:13 +0000 | [diff] [blame] | 235 | self.assertRaises(TypeError, getargs_n, 3.14) |
Trent Nelson | 3513358 | 2008-04-22 19:02:40 +0000 | [diff] [blame] | 236 | self.assertRaises(TypeError, getargs_n, Int()) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 237 | self.assertEqual(0, getargs_n(IntSubclass())) |
| 238 | self.assertRaises(TypeError, getargs_n, BadInt()) |
| 239 | self.assertRaises(TypeError, getargs_n, BadInt2()) |
| 240 | self.assertEqual(0, getargs_n(BadInt3())) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 241 | |
| 242 | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 243 | self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN)) |
| 244 | self.assertEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 245 | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1) |
| 246 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 247 | self.assertEqual(42, getargs_n(42)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 248 | self.assertRaises(OverflowError, getargs_n, VERY_LARGE) |
| 249 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 250 | |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 251 | @unittest.skipIf(getargs_L is None, 'PY_LONG_LONG is not available') |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 252 | class LongLong_TestCase(unittest.TestCase): |
| 253 | def test_L(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 254 | from _testcapi import getargs_L |
Mark Dickinson | de60401 | 2010-01-01 19:27:32 +0000 | [diff] [blame] | 255 | # L returns 'long long', and does range checking (LLONG_MIN |
| 256 | # ... LLONG_MAX) |
Mark Dickinson | c730131 | 2010-06-10 16:05:10 +0000 | [diff] [blame] | 257 | self.assertRaises(TypeError, getargs_L, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 258 | self.assertRaises(TypeError, getargs_L, "Hello") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 259 | self.assertEqual(99, getargs_L(Int())) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 260 | self.assertEqual(0, getargs_L(IntSubclass())) |
| 261 | self.assertRaises(TypeError, getargs_L, BadInt()) |
| 262 | with self.assertWarns(DeprecationWarning): |
| 263 | self.assertEqual(1, getargs_L(BadInt2())) |
| 264 | self.assertEqual(0, getargs_L(BadInt3())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 265 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 266 | self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 267 | self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN)) |
| 268 | self.assertEqual(LLONG_MAX, getargs_L(LLONG_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 269 | self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 270 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 271 | self.assertEqual(42, getargs_L(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 272 | self.assertRaises(OverflowError, getargs_L, VERY_LARGE) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 273 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 274 | def test_K(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 275 | from _testcapi import getargs_K |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 276 | # K return 'unsigned long long', no range checking |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 277 | self.assertRaises(TypeError, getargs_K, 3.14) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 278 | self.assertRaises(TypeError, getargs_K, Int()) |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 279 | self.assertEqual(0, getargs_K(IntSubclass())) |
| 280 | self.assertRaises(TypeError, getargs_K, BadInt()) |
| 281 | self.assertRaises(TypeError, getargs_K, BadInt2()) |
| 282 | self.assertEqual(0, getargs_K(BadInt3())) |
| 283 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 284 | self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) |
| 285 | self.assertEqual(0, getargs_K(0)) |
| 286 | self.assertEqual(0, getargs_K(ULLONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 287 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 288 | self.assertEqual(42, getargs_K(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 289 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 290 | self.assertEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 291 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 292 | class Paradox: |
| 293 | "This statement is false." |
| 294 | def __bool__(self): |
| 295 | raise NotImplementedError |
| 296 | |
| 297 | class Boolean_TestCase(unittest.TestCase): |
| 298 | def test_p(self): |
| 299 | from _testcapi import getargs_p |
| 300 | self.assertEqual(0, getargs_p(False)) |
| 301 | self.assertEqual(0, getargs_p(None)) |
| 302 | self.assertEqual(0, getargs_p(0)) |
| 303 | self.assertEqual(0, getargs_p(0.0)) |
| 304 | self.assertEqual(0, getargs_p(0j)) |
| 305 | self.assertEqual(0, getargs_p('')) |
| 306 | self.assertEqual(0, getargs_p(())) |
| 307 | self.assertEqual(0, getargs_p([])) |
| 308 | self.assertEqual(0, getargs_p({})) |
| 309 | |
| 310 | self.assertEqual(1, getargs_p(True)) |
| 311 | self.assertEqual(1, getargs_p(1)) |
| 312 | self.assertEqual(1, getargs_p(1.0)) |
| 313 | self.assertEqual(1, getargs_p(1j)) |
| 314 | self.assertEqual(1, getargs_p('x')) |
| 315 | self.assertEqual(1, getargs_p((1,))) |
| 316 | self.assertEqual(1, getargs_p([1])) |
| 317 | self.assertEqual(1, getargs_p({1:2})) |
| 318 | self.assertEqual(1, getargs_p(unittest.TestCase)) |
| 319 | |
| 320 | self.assertRaises(NotImplementedError, getargs_p, Paradox()) |
| 321 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 322 | |
| 323 | class Tuple_TestCase(unittest.TestCase): |
| 324 | def test_tuple(self): |
| 325 | from _testcapi import getargs_tuple |
| 326 | |
| 327 | ret = getargs_tuple(1, (2, 3)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 328 | self.assertEqual(ret, (1,2,3)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 329 | |
| 330 | # make sure invalid tuple arguments are handled correctly |
| 331 | class seq: |
| 332 | def __len__(self): |
| 333 | return 2 |
| 334 | def __getitem__(self, n): |
| 335 | raise ValueError |
| 336 | self.assertRaises(TypeError, getargs_tuple, 1, seq()) |
| 337 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 338 | class Keywords_TestCase(unittest.TestCase): |
| 339 | def test_positional_args(self): |
| 340 | # using all positional args |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 341 | self.assertEqual( |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 342 | getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10), |
| 343 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 344 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 345 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 346 | def test_mixed_args(self): |
| 347 | # positional and keyword args |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 348 | self.assertEqual( |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 349 | getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10), |
| 350 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 351 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 352 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 353 | def test_keyword_args(self): |
| 354 | # all keywords |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 355 | self.assertEqual( |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 356 | getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10), |
| 357 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 358 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 359 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 360 | def test_optional_args(self): |
| 361 | # missing optional keyword args, skipping tuples |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 362 | self.assertEqual( |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 363 | getargs_keywords(arg1=(1,2), arg2=3, arg5=10), |
| 364 | (1, 2, 3, -1, -1, -1, -1, -1, -1, 10) |
| 365 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 366 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 367 | def test_required_args(self): |
| 368 | # required arg missing |
| 369 | try: |
| 370 | getargs_keywords(arg1=(1,2)) |
| 371 | except TypeError as err: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 372 | self.assertEqual(str(err), "Required argument 'arg2' (pos 2) not found") |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 373 | else: |
| 374 | self.fail('TypeError should have been raised') |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 375 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 376 | def test_too_many_args(self): |
| 377 | try: |
| 378 | getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111) |
| 379 | except TypeError as err: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 380 | self.assertEqual(str(err), "function takes at most 5 arguments (6 given)") |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 381 | else: |
| 382 | self.fail('TypeError should have been raised') |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 383 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 384 | def test_invalid_keyword(self): |
| 385 | # extraneous keyword arg |
| 386 | try: |
| 387 | getargs_keywords((1,2),3,arg5=10,arg666=666) |
| 388 | except TypeError as err: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 389 | 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] | 390 | else: |
| 391 | self.fail('TypeError should have been raised') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 392 | |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 393 | def test_surrogate_keyword(self): |
| 394 | try: |
| 395 | getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), **{'\uDC80': 10}) |
| 396 | except TypeError as err: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 397 | 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] | 398 | else: |
| 399 | self.fail('TypeError should have been raised') |
| 400 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 401 | class KeywordOnly_TestCase(unittest.TestCase): |
| 402 | def test_positional_args(self): |
| 403 | # using all possible positional args |
| 404 | self.assertEqual( |
| 405 | getargs_keyword_only(1, 2), |
| 406 | (1, 2, -1) |
| 407 | ) |
| 408 | |
| 409 | def test_mixed_args(self): |
| 410 | # positional and keyword args |
| 411 | self.assertEqual( |
| 412 | getargs_keyword_only(1, 2, keyword_only=3), |
| 413 | (1, 2, 3) |
| 414 | ) |
| 415 | |
| 416 | def test_keyword_args(self): |
| 417 | # all keywords |
| 418 | self.assertEqual( |
| 419 | getargs_keyword_only(required=1, optional=2, keyword_only=3), |
| 420 | (1, 2, 3) |
| 421 | ) |
| 422 | |
| 423 | def test_optional_args(self): |
| 424 | # missing optional keyword args, skipping tuples |
| 425 | self.assertEqual( |
| 426 | getargs_keyword_only(required=1, optional=2), |
| 427 | (1, 2, -1) |
| 428 | ) |
| 429 | self.assertEqual( |
| 430 | getargs_keyword_only(required=1, keyword_only=3), |
| 431 | (1, -1, 3) |
| 432 | ) |
| 433 | |
| 434 | def test_required_args(self): |
| 435 | self.assertEqual( |
| 436 | getargs_keyword_only(1), |
| 437 | (1, -1, -1) |
| 438 | ) |
| 439 | self.assertEqual( |
| 440 | getargs_keyword_only(required=1), |
| 441 | (1, -1, -1) |
| 442 | ) |
| 443 | # required arg missing |
| 444 | with self.assertRaisesRegex(TypeError, |
| 445 | "Required argument 'required' \(pos 1\) not found"): |
| 446 | getargs_keyword_only(optional=2) |
| 447 | |
| 448 | with self.assertRaisesRegex(TypeError, |
| 449 | "Required argument 'required' \(pos 1\) not found"): |
| 450 | getargs_keyword_only(keyword_only=3) |
| 451 | |
| 452 | def test_too_many_args(self): |
| 453 | with self.assertRaisesRegex(TypeError, |
| 454 | "Function takes at most 2 positional arguments \(3 given\)"): |
| 455 | getargs_keyword_only(1, 2, 3) |
| 456 | |
| 457 | with self.assertRaisesRegex(TypeError, |
| 458 | "function takes at most 3 arguments \(4 given\)"): |
| 459 | getargs_keyword_only(1, 2, 3, keyword_only=5) |
| 460 | |
| 461 | def test_invalid_keyword(self): |
| 462 | # extraneous keyword arg |
| 463 | with self.assertRaisesRegex(TypeError, |
| 464 | "'monster' is an invalid keyword argument for this function"): |
| 465 | getargs_keyword_only(1, 2, monster=666) |
| 466 | |
| 467 | def test_surrogate_keyword(self): |
| 468 | with self.assertRaisesRegex(TypeError, |
| 469 | "'\udc80' is an invalid keyword argument for this function"): |
| 470 | getargs_keyword_only(1, 2, **{'\uDC80': 10}) |
| 471 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 472 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 473 | class Bytes_TestCase(unittest.TestCase): |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 474 | def test_c(self): |
| 475 | from _testcapi import getargs_c |
| 476 | self.assertRaises(TypeError, getargs_c, b'abc') # len > 1 |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 477 | self.assertEqual(getargs_c(b'a'), 97) |
| 478 | self.assertEqual(getargs_c(bytearray(b'a')), 97) |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 479 | self.assertRaises(TypeError, getargs_c, memoryview(b'a')) |
| 480 | self.assertRaises(TypeError, getargs_c, 's') |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 481 | self.assertRaises(TypeError, getargs_c, 97) |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 482 | self.assertRaises(TypeError, getargs_c, None) |
| 483 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 484 | def test_y(self): |
| 485 | from _testcapi import getargs_y |
| 486 | self.assertRaises(TypeError, getargs_y, 'abc\xe9') |
| 487 | self.assertEqual(getargs_y(b'bytes'), b'bytes') |
| 488 | self.assertRaises(ValueError, getargs_y, b'nul:\0') |
| 489 | self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray')) |
| 490 | self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview')) |
| 491 | self.assertRaises(TypeError, getargs_y, None) |
| 492 | |
| 493 | def test_y_star(self): |
| 494 | from _testcapi import getargs_y_star |
| 495 | self.assertRaises(TypeError, getargs_y_star, 'abc\xe9') |
| 496 | self.assertEqual(getargs_y_star(b'bytes'), b'bytes') |
| 497 | self.assertEqual(getargs_y_star(b'nul:\0'), b'nul:\0') |
| 498 | self.assertEqual(getargs_y_star(bytearray(b'bytearray')), b'bytearray') |
| 499 | self.assertEqual(getargs_y_star(memoryview(b'memoryview')), b'memoryview') |
| 500 | self.assertRaises(TypeError, getargs_y_star, None) |
| 501 | |
| 502 | def test_y_hash(self): |
| 503 | from _testcapi import getargs_y_hash |
| 504 | self.assertRaises(TypeError, getargs_y_hash, 'abc\xe9') |
| 505 | self.assertEqual(getargs_y_hash(b'bytes'), b'bytes') |
| 506 | self.assertEqual(getargs_y_hash(b'nul:\0'), b'nul:\0') |
| 507 | self.assertRaises(TypeError, getargs_y_hash, bytearray(b'bytearray')) |
| 508 | self.assertRaises(TypeError, getargs_y_hash, memoryview(b'memoryview')) |
| 509 | self.assertRaises(TypeError, getargs_y_hash, None) |
| 510 | |
| 511 | def test_w_star(self): |
| 512 | # getargs_w_star() modifies first and last byte |
| 513 | from _testcapi import getargs_w_star |
| 514 | self.assertRaises(TypeError, getargs_w_star, 'abc\xe9') |
| 515 | self.assertRaises(TypeError, getargs_w_star, b'bytes') |
| 516 | self.assertRaises(TypeError, getargs_w_star, b'nul:\0') |
| 517 | self.assertRaises(TypeError, getargs_w_star, memoryview(b'bytes')) |
| 518 | buf = bytearray(b'bytearray') |
| 519 | self.assertEqual(getargs_w_star(buf), b'[ytearra]') |
| 520 | self.assertEqual(buf, bytearray(b'[ytearra]')) |
| 521 | buf = bytearray(b'memoryview') |
| 522 | self.assertEqual(getargs_w_star(memoryview(buf)), b'[emoryvie]') |
| 523 | self.assertEqual(buf, bytearray(b'[emoryvie]')) |
| 524 | self.assertRaises(TypeError, getargs_w_star, None) |
| 525 | |
| 526 | |
| 527 | class String_TestCase(unittest.TestCase): |
| 528 | def test_C(self): |
| 529 | from _testcapi import getargs_C |
| 530 | self.assertRaises(TypeError, getargs_C, 'abc') # len > 1 |
| 531 | self.assertEqual(getargs_C('a'), 97) |
| 532 | self.assertEqual(getargs_C('\u20ac'), 0x20ac) |
| 533 | self.assertEqual(getargs_C('\U0001f40d'), 0x1f40d) |
| 534 | self.assertRaises(TypeError, getargs_C, b'a') |
| 535 | self.assertRaises(TypeError, getargs_C, bytearray(b'a')) |
| 536 | self.assertRaises(TypeError, getargs_C, memoryview(b'a')) |
| 537 | self.assertRaises(TypeError, getargs_C, 97) |
| 538 | self.assertRaises(TypeError, getargs_C, None) |
| 539 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 540 | def test_s(self): |
| 541 | from _testcapi import getargs_s |
| 542 | self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9') |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 543 | self.assertRaises(ValueError, getargs_s, 'nul:\0') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 544 | self.assertRaises(TypeError, getargs_s, b'bytes') |
| 545 | self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray')) |
| 546 | self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview')) |
| 547 | self.assertRaises(TypeError, getargs_s, None) |
| 548 | |
| 549 | def test_s_star(self): |
| 550 | from _testcapi import getargs_s_star |
| 551 | self.assertEqual(getargs_s_star('abc\xe9'), b'abc\xc3\xa9') |
| 552 | self.assertEqual(getargs_s_star('nul:\0'), b'nul:\0') |
| 553 | self.assertEqual(getargs_s_star(b'bytes'), b'bytes') |
| 554 | self.assertEqual(getargs_s_star(bytearray(b'bytearray')), b'bytearray') |
| 555 | self.assertEqual(getargs_s_star(memoryview(b'memoryview')), b'memoryview') |
| 556 | self.assertRaises(TypeError, getargs_s_star, None) |
| 557 | |
| 558 | def test_s_hash(self): |
| 559 | from _testcapi import getargs_s_hash |
| 560 | self.assertEqual(getargs_s_hash('abc\xe9'), b'abc\xc3\xa9') |
| 561 | self.assertEqual(getargs_s_hash('nul:\0'), b'nul:\0') |
| 562 | self.assertEqual(getargs_s_hash(b'bytes'), b'bytes') |
| 563 | self.assertRaises(TypeError, getargs_s_hash, bytearray(b'bytearray')) |
| 564 | self.assertRaises(TypeError, getargs_s_hash, memoryview(b'memoryview')) |
| 565 | self.assertRaises(TypeError, getargs_s_hash, None) |
| 566 | |
| 567 | def test_z(self): |
| 568 | from _testcapi import getargs_z |
| 569 | self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9') |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 570 | self.assertRaises(ValueError, getargs_z, 'nul:\0') |
Victor Stinner | 4aae1eb | 2010-06-24 22:08:25 +0000 | [diff] [blame] | 571 | self.assertRaises(TypeError, getargs_z, b'bytes') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 572 | self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray')) |
| 573 | self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview')) |
| 574 | self.assertIsNone(getargs_z(None)) |
| 575 | |
| 576 | def test_z_star(self): |
| 577 | from _testcapi import getargs_z_star |
| 578 | self.assertEqual(getargs_z_star('abc\xe9'), b'abc\xc3\xa9') |
| 579 | self.assertEqual(getargs_z_star('nul:\0'), b'nul:\0') |
| 580 | self.assertEqual(getargs_z_star(b'bytes'), b'bytes') |
| 581 | self.assertEqual(getargs_z_star(bytearray(b'bytearray')), b'bytearray') |
| 582 | self.assertEqual(getargs_z_star(memoryview(b'memoryview')), b'memoryview') |
| 583 | self.assertIsNone(getargs_z_star(None)) |
| 584 | |
| 585 | def test_z_hash(self): |
| 586 | from _testcapi import getargs_z_hash |
| 587 | self.assertEqual(getargs_z_hash('abc\xe9'), b'abc\xc3\xa9') |
| 588 | self.assertEqual(getargs_z_hash('nul:\0'), b'nul:\0') |
| 589 | self.assertEqual(getargs_z_hash(b'bytes'), b'bytes') |
| 590 | self.assertRaises(TypeError, getargs_z_hash, bytearray(b'bytearray')) |
| 591 | self.assertRaises(TypeError, getargs_z_hash, memoryview(b'memoryview')) |
| 592 | self.assertIsNone(getargs_z_hash(None)) |
| 593 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 594 | def test_es(self): |
| 595 | from _testcapi import getargs_es |
| 596 | self.assertEqual(getargs_es('abc\xe9'), b'abc\xc3\xa9') |
| 597 | self.assertEqual(getargs_es('abc\xe9', 'latin1'), b'abc\xe9') |
| 598 | self.assertRaises(UnicodeEncodeError, getargs_es, 'abc\xe9', 'ascii') |
| 599 | self.assertRaises(LookupError, getargs_es, 'abc\xe9', 'spam') |
| 600 | self.assertRaises(TypeError, getargs_es, b'bytes', 'latin1') |
| 601 | self.assertRaises(TypeError, getargs_es, bytearray(b'bytearray'), 'latin1') |
| 602 | self.assertRaises(TypeError, getargs_es, memoryview(b'memoryview'), 'latin1') |
| 603 | self.assertRaises(TypeError, getargs_es, None, 'latin1') |
| 604 | self.assertRaises(TypeError, getargs_es, 'nul:\0', 'latin1') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 605 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 606 | def test_et(self): |
| 607 | from _testcapi import getargs_et |
| 608 | self.assertEqual(getargs_et('abc\xe9'), b'abc\xc3\xa9') |
| 609 | self.assertEqual(getargs_et('abc\xe9', 'latin1'), b'abc\xe9') |
| 610 | self.assertRaises(UnicodeEncodeError, getargs_et, 'abc\xe9', 'ascii') |
| 611 | self.assertRaises(LookupError, getargs_et, 'abc\xe9', 'spam') |
| 612 | self.assertEqual(getargs_et(b'bytes', 'latin1'), b'bytes') |
| 613 | self.assertEqual(getargs_et(bytearray(b'bytearray'), 'latin1'), b'bytearray') |
| 614 | self.assertRaises(TypeError, getargs_et, memoryview(b'memoryview'), 'latin1') |
| 615 | self.assertRaises(TypeError, getargs_et, None, 'latin1') |
| 616 | self.assertRaises(TypeError, getargs_et, 'nul:\0', 'latin1') |
| 617 | self.assertRaises(TypeError, getargs_et, b'nul:\0', 'latin1') |
| 618 | self.assertRaises(TypeError, getargs_et, bytearray(b'nul:\0'), 'latin1') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 619 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 620 | def test_es_hash(self): |
| 621 | from _testcapi import getargs_es_hash |
| 622 | self.assertEqual(getargs_es_hash('abc\xe9'), b'abc\xc3\xa9') |
| 623 | self.assertEqual(getargs_es_hash('abc\xe9', 'latin1'), b'abc\xe9') |
| 624 | self.assertRaises(UnicodeEncodeError, getargs_es_hash, 'abc\xe9', 'ascii') |
| 625 | self.assertRaises(LookupError, getargs_es_hash, 'abc\xe9', 'spam') |
| 626 | self.assertRaises(TypeError, getargs_es_hash, b'bytes', 'latin1') |
| 627 | self.assertRaises(TypeError, getargs_es_hash, bytearray(b'bytearray'), 'latin1') |
| 628 | self.assertRaises(TypeError, getargs_es_hash, memoryview(b'memoryview'), 'latin1') |
| 629 | self.assertRaises(TypeError, getargs_es_hash, None, 'latin1') |
| 630 | self.assertEqual(getargs_es_hash('nul:\0', 'latin1'), b'nul:\0') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 631 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 632 | buf = bytearray(b'x'*8) |
| 633 | self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 634 | self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx')) |
| 635 | buf = bytearray(b'x'*5) |
| 636 | self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 637 | self.assertEqual(buf, bytearray(b'abc\xe9\x00')) |
| 638 | buf = bytearray(b'x'*4) |
| 639 | self.assertRaises(TypeError, getargs_es_hash, 'abc\xe9', 'latin1', buf) |
| 640 | self.assertEqual(buf, bytearray(b'x'*4)) |
| 641 | buf = bytearray() |
| 642 | self.assertRaises(TypeError, getargs_es_hash, 'abc\xe9', 'latin1', buf) |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 643 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 644 | def test_et_hash(self): |
| 645 | from _testcapi import getargs_et_hash |
| 646 | self.assertEqual(getargs_et_hash('abc\xe9'), b'abc\xc3\xa9') |
| 647 | self.assertEqual(getargs_et_hash('abc\xe9', 'latin1'), b'abc\xe9') |
| 648 | self.assertRaises(UnicodeEncodeError, getargs_et_hash, 'abc\xe9', 'ascii') |
| 649 | self.assertRaises(LookupError, getargs_et_hash, 'abc\xe9', 'spam') |
| 650 | self.assertEqual(getargs_et_hash(b'bytes', 'latin1'), b'bytes') |
| 651 | self.assertEqual(getargs_et_hash(bytearray(b'bytearray'), 'latin1'), b'bytearray') |
| 652 | self.assertRaises(TypeError, getargs_et_hash, memoryview(b'memoryview'), 'latin1') |
| 653 | self.assertRaises(TypeError, getargs_et_hash, None, 'latin1') |
| 654 | self.assertEqual(getargs_et_hash('nul:\0', 'latin1'), b'nul:\0') |
| 655 | self.assertEqual(getargs_et_hash(b'nul:\0', 'latin1'), b'nul:\0') |
| 656 | 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] | 657 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame^] | 658 | buf = bytearray(b'x'*8) |
| 659 | self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 660 | self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx')) |
| 661 | buf = bytearray(b'x'*5) |
| 662 | self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 663 | self.assertEqual(buf, bytearray(b'abc\xe9\x00')) |
| 664 | buf = bytearray(b'x'*4) |
| 665 | self.assertRaises(TypeError, getargs_et_hash, 'abc\xe9', 'latin1', buf) |
| 666 | self.assertEqual(buf, bytearray(b'x'*4)) |
| 667 | buf = bytearray() |
| 668 | self.assertRaises(TypeError, getargs_et_hash, 'abc\xe9', 'latin1', buf) |
| 669 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 670 | def test_u(self): |
| 671 | from _testcapi import getargs_u |
| 672 | self.assertEqual(getargs_u('abc\xe9'), 'abc\xe9') |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 673 | self.assertRaises(ValueError, getargs_u, 'nul:\0') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 674 | self.assertRaises(TypeError, getargs_u, b'bytes') |
| 675 | self.assertRaises(TypeError, getargs_u, bytearray(b'bytearray')) |
| 676 | self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview')) |
| 677 | self.assertRaises(TypeError, getargs_u, None) |
| 678 | |
| 679 | def test_u_hash(self): |
| 680 | from _testcapi import getargs_u_hash |
| 681 | self.assertEqual(getargs_u_hash('abc\xe9'), 'abc\xe9') |
| 682 | self.assertEqual(getargs_u_hash('nul:\0'), 'nul:\0') |
| 683 | self.assertRaises(TypeError, getargs_u_hash, b'bytes') |
| 684 | self.assertRaises(TypeError, getargs_u_hash, bytearray(b'bytearray')) |
| 685 | self.assertRaises(TypeError, getargs_u_hash, memoryview(b'memoryview')) |
| 686 | self.assertRaises(TypeError, getargs_u_hash, None) |
| 687 | |
| 688 | def test_Z(self): |
| 689 | from _testcapi import getargs_Z |
| 690 | self.assertEqual(getargs_Z('abc\xe9'), 'abc\xe9') |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 691 | self.assertRaises(ValueError, getargs_Z, 'nul:\0') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 692 | self.assertRaises(TypeError, getargs_Z, b'bytes') |
| 693 | self.assertRaises(TypeError, getargs_Z, bytearray(b'bytearray')) |
| 694 | self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview')) |
| 695 | self.assertIsNone(getargs_Z(None)) |
| 696 | |
| 697 | def test_Z_hash(self): |
| 698 | from _testcapi import getargs_Z_hash |
| 699 | self.assertEqual(getargs_Z_hash('abc\xe9'), 'abc\xe9') |
| 700 | self.assertEqual(getargs_Z_hash('nul:\0'), 'nul:\0') |
| 701 | self.assertRaises(TypeError, getargs_Z_hash, b'bytes') |
| 702 | self.assertRaises(TypeError, getargs_Z_hash, bytearray(b'bytearray')) |
| 703 | self.assertRaises(TypeError, getargs_Z_hash, memoryview(b'memoryview')) |
| 704 | self.assertIsNone(getargs_Z_hash(None)) |
| 705 | |
| 706 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 707 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 708 | unittest.main() |