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 | # |
| 37 | # Plus a C API or two, e.g. PyInt_AsLongMask() -> |
| 38 | # unsigned long and PyInt_AsLongLongMask() -> unsigned |
| 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 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 472 | class Bytes_TestCase(unittest.TestCase): |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 473 | def test_c(self): |
| 474 | from _testcapi import getargs_c |
| 475 | self.assertRaises(TypeError, getargs_c, b'abc') # len > 1 |
| 476 | self.assertEqual(getargs_c(b'a'), b'a') |
| 477 | self.assertEqual(getargs_c(bytearray(b'a')), b'a') |
| 478 | self.assertRaises(TypeError, getargs_c, memoryview(b'a')) |
| 479 | self.assertRaises(TypeError, getargs_c, 's') |
| 480 | self.assertRaises(TypeError, getargs_c, None) |
| 481 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 482 | def test_s(self): |
| 483 | from _testcapi import getargs_s |
| 484 | self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9') |
| 485 | self.assertRaises(TypeError, getargs_s, 'nul:\0') |
| 486 | self.assertRaises(TypeError, getargs_s, b'bytes') |
| 487 | self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray')) |
| 488 | self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview')) |
| 489 | self.assertRaises(TypeError, getargs_s, None) |
| 490 | |
| 491 | def test_s_star(self): |
| 492 | from _testcapi import getargs_s_star |
| 493 | self.assertEqual(getargs_s_star('abc\xe9'), b'abc\xc3\xa9') |
| 494 | self.assertEqual(getargs_s_star('nul:\0'), b'nul:\0') |
| 495 | self.assertEqual(getargs_s_star(b'bytes'), b'bytes') |
| 496 | self.assertEqual(getargs_s_star(bytearray(b'bytearray')), b'bytearray') |
| 497 | self.assertEqual(getargs_s_star(memoryview(b'memoryview')), b'memoryview') |
| 498 | self.assertRaises(TypeError, getargs_s_star, None) |
| 499 | |
| 500 | def test_s_hash(self): |
| 501 | from _testcapi import getargs_s_hash |
| 502 | self.assertEqual(getargs_s_hash('abc\xe9'), b'abc\xc3\xa9') |
| 503 | self.assertEqual(getargs_s_hash('nul:\0'), b'nul:\0') |
| 504 | self.assertEqual(getargs_s_hash(b'bytes'), b'bytes') |
| 505 | self.assertRaises(TypeError, getargs_s_hash, bytearray(b'bytearray')) |
| 506 | self.assertRaises(TypeError, getargs_s_hash, memoryview(b'memoryview')) |
| 507 | self.assertRaises(TypeError, getargs_s_hash, None) |
| 508 | |
| 509 | def test_z(self): |
| 510 | from _testcapi import getargs_z |
| 511 | self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9') |
| 512 | self.assertRaises(TypeError, getargs_z, 'nul:\0') |
Victor Stinner | 4aae1eb | 2010-06-24 22:08:25 +0000 | [diff] [blame] | 513 | self.assertRaises(TypeError, getargs_z, b'bytes') |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 514 | self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray')) |
| 515 | self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview')) |
| 516 | self.assertIsNone(getargs_z(None)) |
| 517 | |
| 518 | def test_z_star(self): |
| 519 | from _testcapi import getargs_z_star |
| 520 | self.assertEqual(getargs_z_star('abc\xe9'), b'abc\xc3\xa9') |
| 521 | self.assertEqual(getargs_z_star('nul:\0'), b'nul:\0') |
| 522 | self.assertEqual(getargs_z_star(b'bytes'), b'bytes') |
| 523 | self.assertEqual(getargs_z_star(bytearray(b'bytearray')), b'bytearray') |
| 524 | self.assertEqual(getargs_z_star(memoryview(b'memoryview')), b'memoryview') |
| 525 | self.assertIsNone(getargs_z_star(None)) |
| 526 | |
| 527 | def test_z_hash(self): |
| 528 | from _testcapi import getargs_z_hash |
| 529 | self.assertEqual(getargs_z_hash('abc\xe9'), b'abc\xc3\xa9') |
| 530 | self.assertEqual(getargs_z_hash('nul:\0'), b'nul:\0') |
| 531 | self.assertEqual(getargs_z_hash(b'bytes'), b'bytes') |
| 532 | self.assertRaises(TypeError, getargs_z_hash, bytearray(b'bytearray')) |
| 533 | self.assertRaises(TypeError, getargs_z_hash, memoryview(b'memoryview')) |
| 534 | self.assertIsNone(getargs_z_hash(None)) |
| 535 | |
| 536 | def test_y(self): |
| 537 | from _testcapi import getargs_y |
| 538 | self.assertRaises(TypeError, getargs_y, 'abc\xe9') |
| 539 | self.assertEqual(getargs_y(b'bytes'), b'bytes') |
| 540 | self.assertRaises(TypeError, getargs_y, b'nul:\0') |
| 541 | self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray')) |
| 542 | self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview')) |
| 543 | self.assertRaises(TypeError, getargs_y, None) |
| 544 | |
| 545 | def test_y_star(self): |
| 546 | from _testcapi import getargs_y_star |
| 547 | self.assertRaises(TypeError, getargs_y_star, 'abc\xe9') |
| 548 | self.assertEqual(getargs_y_star(b'bytes'), b'bytes') |
| 549 | self.assertEqual(getargs_y_star(b'nul:\0'), b'nul:\0') |
| 550 | self.assertEqual(getargs_y_star(bytearray(b'bytearray')), b'bytearray') |
| 551 | self.assertEqual(getargs_y_star(memoryview(b'memoryview')), b'memoryview') |
| 552 | self.assertRaises(TypeError, getargs_y_star, None) |
| 553 | |
| 554 | def test_y_hash(self): |
| 555 | from _testcapi import getargs_y_hash |
| 556 | self.assertRaises(TypeError, getargs_y_hash, 'abc\xe9') |
| 557 | self.assertEqual(getargs_y_hash(b'bytes'), b'bytes') |
| 558 | self.assertEqual(getargs_y_hash(b'nul:\0'), b'nul:\0') |
| 559 | self.assertRaises(TypeError, getargs_y_hash, bytearray(b'bytearray')) |
| 560 | self.assertRaises(TypeError, getargs_y_hash, memoryview(b'memoryview')) |
| 561 | self.assertRaises(TypeError, getargs_y_hash, None) |
| 562 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 563 | def test_w_star(self): |
| 564 | # getargs_w_star() modifies first and last byte |
| 565 | from _testcapi import getargs_w_star |
| 566 | self.assertRaises(TypeError, getargs_w_star, 'abc\xe9') |
| 567 | self.assertRaises(TypeError, getargs_w_star, b'bytes') |
| 568 | self.assertRaises(TypeError, getargs_w_star, b'nul:\0') |
Antoine Pitrou | ad62b03 | 2011-01-18 18:57:52 +0000 | [diff] [blame] | 569 | self.assertRaises(TypeError, getargs_w_star, memoryview(b'bytes')) |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 570 | self.assertEqual(getargs_w_star(bytearray(b'bytearray')), b'[ytearra]') |
Antoine Pitrou | ad62b03 | 2011-01-18 18:57:52 +0000 | [diff] [blame] | 571 | self.assertEqual(getargs_w_star(memoryview(bytearray(b'memoryview'))), |
| 572 | b'[emoryvie]') |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 573 | self.assertRaises(TypeError, getargs_w_star, None) |
| 574 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 575 | |
| 576 | class Unicode_TestCase(unittest.TestCase): |
| 577 | def test_u(self): |
| 578 | from _testcapi import getargs_u |
| 579 | self.assertEqual(getargs_u('abc\xe9'), 'abc\xe9') |
| 580 | self.assertRaises(TypeError, getargs_u, 'nul:\0') |
| 581 | self.assertRaises(TypeError, getargs_u, b'bytes') |
| 582 | self.assertRaises(TypeError, getargs_u, bytearray(b'bytearray')) |
| 583 | self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview')) |
| 584 | self.assertRaises(TypeError, getargs_u, None) |
| 585 | |
| 586 | def test_u_hash(self): |
| 587 | from _testcapi import getargs_u_hash |
| 588 | self.assertEqual(getargs_u_hash('abc\xe9'), 'abc\xe9') |
| 589 | self.assertEqual(getargs_u_hash('nul:\0'), 'nul:\0') |
| 590 | self.assertRaises(TypeError, getargs_u_hash, b'bytes') |
| 591 | self.assertRaises(TypeError, getargs_u_hash, bytearray(b'bytearray')) |
| 592 | self.assertRaises(TypeError, getargs_u_hash, memoryview(b'memoryview')) |
| 593 | self.assertRaises(TypeError, getargs_u_hash, None) |
| 594 | |
| 595 | def test_Z(self): |
| 596 | from _testcapi import getargs_Z |
| 597 | self.assertEqual(getargs_Z('abc\xe9'), 'abc\xe9') |
| 598 | self.assertRaises(TypeError, getargs_Z, 'nul:\0') |
| 599 | self.assertRaises(TypeError, getargs_Z, b'bytes') |
| 600 | self.assertRaises(TypeError, getargs_Z, bytearray(b'bytearray')) |
| 601 | self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview')) |
| 602 | self.assertIsNone(getargs_Z(None)) |
| 603 | |
| 604 | def test_Z_hash(self): |
| 605 | from _testcapi import getargs_Z_hash |
| 606 | self.assertEqual(getargs_Z_hash('abc\xe9'), 'abc\xe9') |
| 607 | self.assertEqual(getargs_Z_hash('nul:\0'), 'nul:\0') |
| 608 | self.assertRaises(TypeError, getargs_Z_hash, b'bytes') |
| 609 | self.assertRaises(TypeError, getargs_Z_hash, bytearray(b'bytearray')) |
| 610 | self.assertRaises(TypeError, getargs_Z_hash, memoryview(b'memoryview')) |
| 611 | self.assertIsNone(getargs_Z_hash(None)) |
| 612 | |
| 613 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 614 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 615 | unittest.main() |