Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
Serhiy Storchaka | 76249ea | 2014-02-07 10:06:05 +0200 | [diff] [blame] | 3 | # Skip this test if the _testcapi module isn't available. |
| 4 | test_support.import_module('_testcapi') |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 5 | from _testcapi import getargs_keywords |
Christian Heimes | c5f05e4 | 2008-02-23 17:40:11 +0000 | [diff] [blame] | 6 | import warnings |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 7 | |
| 8 | """ |
| 9 | > How about the following counterproposal. This also changes some of |
| 10 | > the other format codes to be a little more regular. |
| 11 | > |
| 12 | > Code C type Range check |
| 13 | > |
| 14 | > b unsigned char 0..UCHAR_MAX |
Guido van Rossum | bbb931b | 2003-04-18 00:13:53 +0000 | [diff] [blame] | 15 | > h signed short SHRT_MIN..SHRT_MAX |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 16 | > B unsigned char none ** |
| 17 | > H unsigned short none ** |
| 18 | > k * unsigned long none |
| 19 | > I * unsigned int 0..UINT_MAX |
| 20 | |
| 21 | |
| 22 | > i int INT_MIN..INT_MAX |
| 23 | > l long LONG_MIN..LONG_MAX |
| 24 | |
| 25 | > K * unsigned long long none |
| 26 | > L long long LLONG_MIN..LLONG_MAX |
| 27 | |
| 28 | > Notes: |
| 29 | > |
| 30 | > * New format codes. |
| 31 | > |
| 32 | > ** Changed from previous "range-and-a-half" to "none"; the |
| 33 | > range-and-a-half checking wasn't particularly useful. |
| 34 | |
| 35 | Plus a C API or two, e.g. PyInt_AsLongMask() -> |
| 36 | unsigned long and PyInt_AsLongLongMask() -> unsigned |
| 37 | long long (if that exists). |
| 38 | """ |
| 39 | |
| 40 | LARGE = 0x7FFFFFFF |
| 41 | VERY_LARGE = 0xFF0000121212121212121242L |
| 42 | |
| 43 | from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \ |
Mark Dickinson | b5e348b | 2009-12-20 15:57:56 +0000 | [diff] [blame] | 44 | INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, \ |
| 45 | SHRT_MIN, SHRT_MAX |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 46 | |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 47 | try: |
| 48 | from _testcapi import getargs_L, getargs_K |
| 49 | except ImportError: |
| 50 | _PY_LONG_LONG_available = False |
| 51 | else: |
| 52 | _PY_LONG_LONG_available = True |
| 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 | |
| 59 | class Long: |
| 60 | def __int__(self): |
| 61 | return 99L |
| 62 | |
| 63 | class Int: |
| 64 | def __int__(self): |
| 65 | return 99 |
| 66 | |
| 67 | class Unsigned_TestCase(unittest.TestCase): |
| 68 | def test_b(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 69 | from _testcapi import getargs_b |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 70 | # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) |
Mark Dickinson | 1b34d25 | 2010-01-01 17:27:30 +0000 | [diff] [blame] | 71 | self.assertRaises(TypeError, getargs_b, 3.14) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 72 | self.assertEqual(99, getargs_b(Long())) |
| 73 | self.assertEqual(99, getargs_b(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 74 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 75 | self.assertRaises(OverflowError, getargs_b, -1) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 76 | self.assertEqual(0, getargs_b(0)) |
| 77 | self.assertEqual(UCHAR_MAX, getargs_b(UCHAR_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 78 | self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 79 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 80 | self.assertEqual(42, getargs_b(42)) |
| 81 | self.assertEqual(42, getargs_b(42L)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 82 | self.assertRaises(OverflowError, getargs_b, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 83 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 84 | def test_B(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 85 | from _testcapi import getargs_B |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 86 | # B returns 'unsigned char', no range checking |
Mark Dickinson | 1b34d25 | 2010-01-01 17:27:30 +0000 | [diff] [blame] | 87 | self.assertRaises(TypeError, getargs_B, 3.14) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 88 | self.assertEqual(99, getargs_B(Long())) |
| 89 | self.assertEqual(99, getargs_B(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 90 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 91 | self.assertEqual(UCHAR_MAX, getargs_B(-1)) |
| 92 | self.assertEqual(UCHAR_MAX, getargs_B(-1L)) |
| 93 | self.assertEqual(0, getargs_B(0)) |
| 94 | self.assertEqual(UCHAR_MAX, getargs_B(UCHAR_MAX)) |
| 95 | self.assertEqual(0, getargs_B(UCHAR_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 96 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 97 | self.assertEqual(42, getargs_B(42)) |
| 98 | self.assertEqual(42, getargs_B(42L)) |
| 99 | self.assertEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 100 | |
| 101 | def test_H(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 102 | from _testcapi import getargs_H |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 103 | # H returns 'unsigned short', no range checking |
Mark Dickinson | 1b34d25 | 2010-01-01 17:27:30 +0000 | [diff] [blame] | 104 | self.assertRaises(TypeError, getargs_H, 3.14) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 105 | self.assertEqual(99, getargs_H(Long())) |
| 106 | self.assertEqual(99, getargs_H(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 107 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 108 | self.assertEqual(USHRT_MAX, getargs_H(-1)) |
| 109 | self.assertEqual(0, getargs_H(0)) |
| 110 | self.assertEqual(USHRT_MAX, getargs_H(USHRT_MAX)) |
| 111 | self.assertEqual(0, getargs_H(USHRT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 112 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 113 | self.assertEqual(42, getargs_H(42)) |
| 114 | self.assertEqual(42, getargs_H(42L)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 115 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 116 | self.assertEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 117 | |
| 118 | def test_I(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 119 | from _testcapi import getargs_I |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 120 | # I returns 'unsigned int', no range checking |
Mark Dickinson | 1b34d25 | 2010-01-01 17:27:30 +0000 | [diff] [blame] | 121 | self.assertRaises(TypeError, getargs_I, 3.14) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 122 | self.assertEqual(99, getargs_I(Long())) |
| 123 | self.assertEqual(99, getargs_I(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 124 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 125 | self.assertEqual(UINT_MAX, getargs_I(-1)) |
| 126 | self.assertEqual(0, getargs_I(0)) |
| 127 | self.assertEqual(UINT_MAX, getargs_I(UINT_MAX)) |
| 128 | self.assertEqual(0, getargs_I(UINT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 129 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 130 | self.assertEqual(42, getargs_I(42)) |
| 131 | self.assertEqual(42, getargs_I(42L)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 132 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 133 | self.assertEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 134 | |
| 135 | def test_k(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 136 | from _testcapi import getargs_k |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 137 | # k returns 'unsigned long', no range checking |
| 138 | # it does not accept float, or instances with __int__ |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 139 | self.assertRaises(TypeError, getargs_k, 3.14) |
| 140 | self.assertRaises(TypeError, getargs_k, Long()) |
| 141 | self.assertRaises(TypeError, getargs_k, Int()) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 142 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 143 | self.assertEqual(ULONG_MAX, getargs_k(-1)) |
| 144 | self.assertEqual(0, getargs_k(0)) |
| 145 | self.assertEqual(ULONG_MAX, getargs_k(ULONG_MAX)) |
| 146 | self.assertEqual(0, getargs_k(ULONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 147 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 148 | self.assertEqual(42, getargs_k(42)) |
| 149 | self.assertEqual(42, getargs_k(42L)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 150 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 151 | self.assertEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 152 | |
| 153 | class Signed_TestCase(unittest.TestCase): |
Mark Dickinson | b5e348b | 2009-12-20 15:57:56 +0000 | [diff] [blame] | 154 | def test_h(self): |
| 155 | from _testcapi import getargs_h |
| 156 | # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) |
Mark Dickinson | 1b34d25 | 2010-01-01 17:27:30 +0000 | [diff] [blame] | 157 | self.assertRaises(TypeError, getargs_h, 3.14) |
Mark Dickinson | b5e348b | 2009-12-20 15:57:56 +0000 | [diff] [blame] | 158 | self.assertEqual(99, getargs_h(Long())) |
| 159 | self.assertEqual(99, getargs_h(Int())) |
| 160 | |
| 161 | self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1) |
| 162 | self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN)) |
| 163 | self.assertEqual(SHRT_MAX, getargs_h(SHRT_MAX)) |
| 164 | self.assertRaises(OverflowError, getargs_h, SHRT_MAX+1) |
| 165 | |
| 166 | self.assertEqual(42, getargs_h(42)) |
| 167 | self.assertEqual(42, getargs_h(42L)) |
| 168 | self.assertRaises(OverflowError, getargs_h, VERY_LARGE) |
| 169 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 170 | def test_i(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 171 | from _testcapi import getargs_i |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 172 | # i returns 'int', and does range checking (INT_MIN ... INT_MAX) |
Mark Dickinson | 1b34d25 | 2010-01-01 17:27:30 +0000 | [diff] [blame] | 173 | self.assertRaises(TypeError, getargs_i, 3.14) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 174 | self.assertEqual(99, getargs_i(Long())) |
| 175 | self.assertEqual(99, getargs_i(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 176 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 177 | self.assertRaises(OverflowError, getargs_i, INT_MIN-1) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 178 | self.assertEqual(INT_MIN, getargs_i(INT_MIN)) |
| 179 | self.assertEqual(INT_MAX, getargs_i(INT_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 180 | self.assertRaises(OverflowError, getargs_i, INT_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 181 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 182 | self.assertEqual(42, getargs_i(42)) |
| 183 | self.assertEqual(42, getargs_i(42L)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 184 | self.assertRaises(OverflowError, getargs_i, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 185 | |
| 186 | def test_l(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 187 | from _testcapi import getargs_l |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 188 | # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) |
Mark Dickinson | 1b34d25 | 2010-01-01 17:27:30 +0000 | [diff] [blame] | 189 | self.assertRaises(TypeError, getargs_l, 3.14) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 190 | self.assertEqual(99, getargs_l(Long())) |
| 191 | self.assertEqual(99, getargs_l(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 192 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 193 | self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 194 | self.assertEqual(LONG_MIN, getargs_l(LONG_MIN)) |
| 195 | self.assertEqual(LONG_MAX, getargs_l(LONG_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 196 | self.assertRaises(OverflowError, getargs_l, LONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 197 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 198 | self.assertEqual(42, getargs_l(42)) |
| 199 | self.assertEqual(42, getargs_l(42L)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 200 | self.assertRaises(OverflowError, getargs_l, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 201 | |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 202 | def test_n(self): |
| 203 | from _testcapi import getargs_n |
Tim Peters | 8e6480c | 2006-04-13 22:58:42 +0000 | [diff] [blame] | 204 | # n returns 'Py_ssize_t', and does range checking |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 205 | # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX) |
Mark Dickinson | 1b34d25 | 2010-01-01 17:27:30 +0000 | [diff] [blame] | 206 | self.assertRaises(TypeError, getargs_n, 3.14) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 207 | self.assertEqual(99, getargs_n(Long())) |
| 208 | self.assertEqual(99, getargs_n(Int())) |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 209 | |
| 210 | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 211 | self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN)) |
| 212 | self.assertEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX)) |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 213 | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1) |
| 214 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 215 | self.assertEqual(42, getargs_n(42)) |
| 216 | self.assertEqual(42, getargs_n(42L)) |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 217 | self.assertRaises(OverflowError, getargs_n, VERY_LARGE) |
| 218 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 219 | |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 220 | @unittest.skipUnless(_PY_LONG_LONG_available, 'PY_LONG_LONG not available') |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 221 | class LongLong_TestCase(unittest.TestCase): |
| 222 | def test_L(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 223 | from _testcapi import getargs_L |
Mark Dickinson | 1b34d25 | 2010-01-01 17:27:30 +0000 | [diff] [blame] | 224 | # L returns 'long long', and does range checking (LLONG_MIN |
| 225 | # ... LLONG_MAX) |
| 226 | with warnings.catch_warnings(): |
| 227 | warnings.filterwarnings( |
| 228 | "ignore", |
| 229 | category=DeprecationWarning, |
| 230 | message=".*integer argument expected, got float", |
| 231 | module=__name__) |
| 232 | self.assertEqual(3, getargs_L(3.14)) |
| 233 | with warnings.catch_warnings(): |
| 234 | warnings.filterwarnings( |
| 235 | "error", |
| 236 | category=DeprecationWarning, |
| 237 | message=".*integer argument expected, got float", |
| 238 | module="unittest") |
| 239 | self.assertRaises(DeprecationWarning, getargs_L, 3.14) |
| 240 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 241 | self.assertRaises(TypeError, getargs_L, "Hello") |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 242 | self.assertEqual(99, getargs_L(Long())) |
| 243 | self.assertEqual(99, getargs_L(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 244 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 245 | self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 246 | self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN)) |
| 247 | self.assertEqual(LLONG_MAX, getargs_L(LLONG_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 248 | self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 249 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 250 | self.assertEqual(42, getargs_L(42)) |
| 251 | self.assertEqual(42, getargs_L(42L)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 252 | self.assertRaises(OverflowError, getargs_L, VERY_LARGE) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 253 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 254 | def test_K(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 255 | from _testcapi import getargs_K |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 256 | # K return 'unsigned long long', no range checking |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 257 | self.assertRaises(TypeError, getargs_K, 3.14) |
| 258 | self.assertRaises(TypeError, getargs_K, Long()) |
| 259 | self.assertRaises(TypeError, getargs_K, Int()) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 260 | self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) |
| 261 | self.assertEqual(0, getargs_K(0)) |
| 262 | self.assertEqual(0, getargs_K(ULLONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 263 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 264 | self.assertEqual(42, getargs_K(42)) |
| 265 | self.assertEqual(42, getargs_K(42L)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 266 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 267 | self.assertEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 268 | |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 269 | |
| 270 | class Tuple_TestCase(unittest.TestCase): |
| 271 | def test_tuple(self): |
| 272 | from _testcapi import getargs_tuple |
Tim Peters | 95621b2 | 2006-07-26 23:23:15 +0000 | [diff] [blame] | 273 | |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 274 | ret = getargs_tuple(1, (2, 3)) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 275 | self.assertEqual(ret, (1,2,3)) |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 276 | |
| 277 | # make sure invalid tuple arguments are handled correctly |
| 278 | class seq: |
| 279 | def __len__(self): |
| 280 | return 2 |
| 281 | def __getitem__(self, n): |
| 282 | raise ValueError |
| 283 | self.assertRaises(TypeError, getargs_tuple, 1, seq()) |
| 284 | |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 285 | class Keywords_TestCase(unittest.TestCase): |
| 286 | def test_positional_args(self): |
| 287 | # using all positional args |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 288 | self.assertEqual( |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 289 | getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10), |
| 290 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 291 | ) |
| 292 | def test_mixed_args(self): |
| 293 | # positional and keyword args |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 294 | self.assertEqual( |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 295 | getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10), |
| 296 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 297 | ) |
| 298 | def test_keyword_args(self): |
| 299 | # all keywords |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 300 | self.assertEqual( |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 301 | getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10), |
| 302 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 303 | ) |
| 304 | def test_optional_args(self): |
| 305 | # missing optional keyword args, skipping tuples |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 306 | self.assertEqual( |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 307 | getargs_keywords(arg1=(1,2), arg2=3, arg5=10), |
| 308 | (1, 2, 3, -1, -1, -1, -1, -1, -1, 10) |
| 309 | ) |
| 310 | def test_required_args(self): |
| 311 | # required arg missing |
| 312 | try: |
| 313 | getargs_keywords(arg1=(1,2)) |
| 314 | except TypeError, err: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 315 | self.assertEqual(str(err), "Required argument 'arg2' (pos 2) not found") |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 316 | else: |
| 317 | self.fail('TypeError should have been raised') |
| 318 | def test_too_many_args(self): |
| 319 | try: |
| 320 | getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111) |
| 321 | except TypeError, err: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 322 | self.assertEqual(str(err), "function takes at most 5 arguments (6 given)") |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 323 | else: |
| 324 | self.fail('TypeError should have been raised') |
| 325 | def test_invalid_keyword(self): |
| 326 | # extraneous keyword arg |
| 327 | try: |
| 328 | getargs_keywords((1,2),3,arg5=10,arg666=666) |
| 329 | except TypeError, err: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 330 | self.assertEqual(str(err), "'arg666' is an invalid keyword argument for this function") |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 331 | else: |
| 332 | self.fail('TypeError should have been raised') |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 333 | |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame^] | 334 | |
| 335 | class Bytes_TestCase(unittest.TestCase): |
| 336 | def test_c(self): |
| 337 | from _testcapi import getargs_c |
| 338 | self.assertRaises(TypeError, getargs_c, 'abc') # len > 1 |
| 339 | self.assertEqual(getargs_c('a'), 97) |
| 340 | if test_support.have_unicode: |
| 341 | self.assertRaises(TypeError, getargs_c, u's') |
| 342 | self.assertRaises(TypeError, getargs_c, bytearray('a')) |
| 343 | self.assertRaises(TypeError, getargs_c, memoryview('a')) |
| 344 | with test_support.check_py3k_warnings(): |
| 345 | self.assertRaises(TypeError, getargs_c, buffer('a')) |
| 346 | self.assertRaises(TypeError, getargs_c, 97) |
| 347 | self.assertRaises(TypeError, getargs_c, None) |
| 348 | |
| 349 | def test_w(self): |
| 350 | from _testcapi import getargs_w |
| 351 | self.assertRaises(TypeError, getargs_w, 'abc', 3) |
| 352 | self.assertRaises(TypeError, getargs_w, u'abc', 3) |
| 353 | self.assertRaises(TypeError, getargs_w, bytearray('bytes'), 3) |
| 354 | self.assertRaises(TypeError, getargs_w, memoryview('bytes'), 3) |
| 355 | self.assertRaises(TypeError, getargs_w, |
| 356 | memoryview(bytearray('bytes')), 3) |
| 357 | with test_support.check_py3k_warnings(): |
| 358 | self.assertRaises(TypeError, getargs_w, buffer('bytes'), 3) |
| 359 | self.assertRaises(TypeError, getargs_w, |
| 360 | buffer(bytearray('bytes')), 3) |
| 361 | self.assertRaises(TypeError, getargs_w, None, 0) |
| 362 | |
| 363 | def test_w_hash(self): |
| 364 | from _testcapi import getargs_w_hash |
| 365 | self.assertRaises(TypeError, getargs_w_hash, 'abc') |
| 366 | self.assertRaises(TypeError, getargs_w_hash, u'abc') |
| 367 | self.assertRaises(TypeError, getargs_w_hash, bytearray('bytes')) |
| 368 | self.assertRaises(TypeError, getargs_w_hash, memoryview('bytes')) |
| 369 | self.assertRaises(TypeError, getargs_w_hash, |
| 370 | memoryview(bytearray('bytes'))) |
| 371 | with test_support.check_py3k_warnings(): |
| 372 | self.assertRaises(TypeError, getargs_w_hash, buffer('bytes')) |
| 373 | self.assertRaises(TypeError, getargs_w_hash, |
| 374 | buffer(bytearray('bytes'))) |
| 375 | self.assertRaises(TypeError, getargs_w_hash, None) |
| 376 | |
| 377 | def test_w_star(self): |
| 378 | # getargs_w_star() modifies first and last byte |
| 379 | from _testcapi import getargs_w_star |
| 380 | self.assertRaises(TypeError, getargs_w_star, 'abc') |
| 381 | self.assertRaises(TypeError, getargs_w_star, u'abc') |
| 382 | self.assertRaises(TypeError, getargs_w_star, memoryview('bytes')) |
| 383 | buf = bytearray('bytearray') |
| 384 | self.assertEqual(getargs_w_star(buf), '[ytearra]') |
| 385 | self.assertEqual(buf, bytearray('[ytearra]')) |
| 386 | buf = bytearray(b'memoryview') |
| 387 | self.assertEqual(getargs_w_star(memoryview(buf)), '[emoryvie]') |
| 388 | self.assertEqual(buf, bytearray('[emoryvie]')) |
| 389 | with test_support.check_py3k_warnings(): |
| 390 | self.assertRaises(TypeError, getargs_w_star, buffer('buffer')) |
| 391 | self.assertRaises(TypeError, getargs_w_star, |
| 392 | buffer(bytearray('buffer'))) |
| 393 | self.assertRaises(TypeError, getargs_w_star, None) |
| 394 | |
| 395 | |
| 396 | class String_TestCase(unittest.TestCase): |
| 397 | def test_s(self): |
| 398 | from _testcapi import getargs_s |
| 399 | self.assertEqual(getargs_s('abc\xe9'), 'abc\xe9') |
| 400 | self.assertEqual(getargs_s(u'abc'), 'abc') |
| 401 | self.assertRaises(TypeError, getargs_s, 'nul:\0') |
| 402 | self.assertRaises(TypeError, getargs_s, u'nul:\0') |
| 403 | self.assertRaises(TypeError, getargs_s, bytearray('bytearray')) |
| 404 | self.assertRaises(TypeError, getargs_s, memoryview('memoryview')) |
| 405 | with test_support.check_py3k_warnings(): |
| 406 | self.assertRaises(TypeError, getargs_s, buffer('buffer')) |
| 407 | self.assertRaises(TypeError, getargs_s, None) |
| 408 | |
| 409 | def test_s_star(self): |
| 410 | from _testcapi import getargs_s_star |
| 411 | self.assertEqual(getargs_s_star('abc\xe9'), 'abc\xe9') |
| 412 | self.assertEqual(getargs_s_star(u'abc'), 'abc') |
| 413 | self.assertEqual(getargs_s_star('nul:\0'), 'nul:\0') |
| 414 | self.assertEqual(getargs_s_star(u'nul:\0'), 'nul:\0') |
| 415 | self.assertEqual(getargs_s_star(bytearray('abc\xe9')), 'abc\xe9') |
| 416 | self.assertEqual(getargs_s_star(memoryview('abc\xe9')), 'abc\xe9') |
| 417 | with test_support.check_py3k_warnings(): |
| 418 | self.assertEqual(getargs_s_star(buffer('abc\xe9')), 'abc\xe9') |
| 419 | self.assertEqual(getargs_s_star(buffer(u'abc\xe9')), |
| 420 | str(buffer(u'abc\xe9'))) |
| 421 | self.assertRaises(TypeError, getargs_s_star, None) |
| 422 | |
| 423 | def test_s_hash(self): |
| 424 | from _testcapi import getargs_s_hash |
| 425 | self.assertEqual(getargs_s_hash('abc\xe9'), 'abc\xe9') |
| 426 | self.assertEqual(getargs_s_hash(u'abc'), 'abc') |
| 427 | self.assertEqual(getargs_s_hash('nul:\0'), 'nul:\0') |
| 428 | self.assertEqual(getargs_s_hash(u'nul:\0'), 'nul:\0') |
| 429 | self.assertRaises(TypeError, getargs_s_hash, bytearray('bytearray')) |
| 430 | self.assertRaises(TypeError, getargs_s_hash, memoryview('memoryview')) |
| 431 | with test_support.check_py3k_warnings(): |
| 432 | self.assertEqual(getargs_s_hash(buffer('abc\xe9')), 'abc\xe9') |
| 433 | self.assertEqual(getargs_s_hash(buffer(u'abc\xe9')), |
| 434 | str(buffer(u'abc\xe9'))) |
| 435 | self.assertRaises(TypeError, getargs_s_hash, None) |
| 436 | |
| 437 | def test_t_hash(self): |
| 438 | from _testcapi import getargs_t_hash |
| 439 | self.assertEqual(getargs_t_hash('abc\xe9'), 'abc\xe9') |
| 440 | self.assertEqual(getargs_t_hash(u'abc'), 'abc') |
| 441 | self.assertEqual(getargs_t_hash('nul:\0'), 'nul:\0') |
| 442 | self.assertEqual(getargs_t_hash(u'nul:\0'), 'nul:\0') |
| 443 | self.assertRaises(TypeError, getargs_t_hash, bytearray('bytearray')) |
| 444 | self.assertRaises(TypeError, getargs_t_hash, memoryview('memoryview')) |
| 445 | with test_support.check_py3k_warnings(): |
| 446 | self.assertEqual(getargs_t_hash(buffer('abc\xe9')), 'abc\xe9') |
| 447 | self.assertEqual(getargs_t_hash(buffer(u'abc')), 'abc') |
| 448 | self.assertRaises(TypeError, getargs_t_hash, None) |
| 449 | |
| 450 | def test_z(self): |
| 451 | from _testcapi import getargs_z |
| 452 | self.assertEqual(getargs_z('abc\xe9'), 'abc\xe9') |
| 453 | self.assertEqual(getargs_z(u'abc'), 'abc') |
| 454 | self.assertRaises(TypeError, getargs_z, 'nul:\0') |
| 455 | self.assertRaises(TypeError, getargs_z, u'nul:\0') |
| 456 | self.assertRaises(TypeError, getargs_z, bytearray('bytearray')) |
| 457 | self.assertRaises(TypeError, getargs_z, memoryview('memoryview')) |
| 458 | with test_support.check_py3k_warnings(): |
| 459 | self.assertRaises(TypeError, getargs_z, buffer('buffer')) |
| 460 | self.assertIsNone(getargs_z(None)) |
| 461 | |
| 462 | def test_z_star(self): |
| 463 | from _testcapi import getargs_z_star |
| 464 | self.assertEqual(getargs_z_star('abc\xe9'), 'abc\xe9') |
| 465 | self.assertEqual(getargs_z_star(u'abc'), 'abc') |
| 466 | self.assertEqual(getargs_z_star('nul:\0'), 'nul:\0') |
| 467 | self.assertEqual(getargs_z_star(u'nul:\0'), 'nul:\0') |
| 468 | self.assertEqual(getargs_z_star(bytearray('abc\xe9')), 'abc\xe9') |
| 469 | self.assertEqual(getargs_z_star(memoryview('abc\xe9')), 'abc\xe9') |
| 470 | with test_support.check_py3k_warnings(): |
| 471 | self.assertEqual(getargs_z_star(buffer('abc\xe9')), 'abc\xe9') |
| 472 | self.assertEqual(getargs_z_star(buffer(u'abc\xe9')), |
| 473 | str(buffer(u'abc\xe9'))) |
| 474 | self.assertIsNone(getargs_z_star(None)) |
| 475 | |
| 476 | def test_z_hash(self): |
| 477 | from _testcapi import getargs_z_hash |
| 478 | self.assertEqual(getargs_z_hash('abc\xe9'), 'abc\xe9') |
| 479 | self.assertEqual(getargs_z_hash(u'abc'), 'abc') |
| 480 | self.assertEqual(getargs_z_hash('nul:\0'), 'nul:\0') |
| 481 | self.assertEqual(getargs_z_hash(u'nul:\0'), 'nul:\0') |
| 482 | self.assertRaises(TypeError, getargs_z_hash, bytearray('bytearray')) |
| 483 | self.assertRaises(TypeError, getargs_z_hash, memoryview('memoryview')) |
| 484 | with test_support.check_py3k_warnings(): |
| 485 | self.assertEqual(getargs_z_hash(buffer('abc\xe9')), 'abc\xe9') |
| 486 | self.assertEqual(getargs_z_hash(buffer(u'abc\xe9')), |
| 487 | str(buffer(u'abc\xe9'))) |
| 488 | self.assertIsNone(getargs_z_hash(None)) |
| 489 | |
| 490 | |
| 491 | @test_support.requires_unicode |
| 492 | class Unicode_TestCase(unittest.TestCase): |
| 493 | def test_es(self): |
| 494 | from _testcapi import getargs_es |
| 495 | self.assertEqual(getargs_es('abc'), 'abc') |
| 496 | self.assertEqual(getargs_es(u'abc'), 'abc') |
| 497 | self.assertEqual(getargs_es('abc', 'ascii'), 'abc') |
| 498 | self.assertEqual(getargs_es(u'abc\xe9', 'latin1'), 'abc\xe9') |
| 499 | self.assertRaises(UnicodeEncodeError, getargs_es, u'abc\xe9', 'ascii') |
| 500 | self.assertRaises(LookupError, getargs_es, u'abc', 'spam') |
| 501 | self.assertRaises(TypeError, getargs_es, |
| 502 | bytearray('bytearray'), 'latin1') |
| 503 | self.assertRaises(TypeError, getargs_es, |
| 504 | memoryview('memoryview'), 'latin1') |
| 505 | with test_support.check_py3k_warnings(): |
| 506 | self.assertEqual(getargs_es(buffer('abc'), 'ascii'), 'abc') |
| 507 | self.assertEqual(getargs_es(buffer(u'abc'), 'ascii'), 'abc') |
| 508 | self.assertRaises(TypeError, getargs_es, None, 'latin1') |
| 509 | self.assertRaises(TypeError, getargs_es, 'nul:\0', 'latin1') |
| 510 | self.assertRaises(TypeError, getargs_es, u'nul:\0', 'latin1') |
| 511 | |
| 512 | def test_et(self): |
| 513 | from _testcapi import getargs_et |
| 514 | self.assertEqual(getargs_et('abc\xe9'), 'abc\xe9') |
| 515 | self.assertEqual(getargs_et(u'abc'), 'abc') |
| 516 | self.assertEqual(getargs_et('abc', 'ascii'), 'abc') |
| 517 | self.assertEqual(getargs_et('abc\xe9', 'ascii'), 'abc\xe9') |
| 518 | self.assertEqual(getargs_et(u'abc\xe9', 'latin1'), 'abc\xe9') |
| 519 | self.assertRaises(UnicodeEncodeError, getargs_et, u'abc\xe9', 'ascii') |
| 520 | self.assertRaises(LookupError, getargs_et, u'abc', 'spam') |
| 521 | self.assertRaises(TypeError, getargs_et, |
| 522 | bytearray('bytearray'), 'latin1') |
| 523 | self.assertRaises(TypeError, getargs_et, |
| 524 | memoryview('memoryview'), 'latin1') |
| 525 | with test_support.check_py3k_warnings(): |
| 526 | self.assertEqual(getargs_et(buffer('abc'), 'ascii'), 'abc') |
| 527 | self.assertEqual(getargs_et(buffer(u'abc'), 'ascii'), 'abc') |
| 528 | self.assertRaises(TypeError, getargs_et, None, 'latin1') |
| 529 | self.assertRaises(TypeError, getargs_et, 'nul:\0', 'latin1') |
| 530 | self.assertRaises(TypeError, getargs_et, u'nul:\0', 'latin1') |
| 531 | |
| 532 | def test_es_hash(self): |
| 533 | from _testcapi import getargs_es_hash |
| 534 | self.assertEqual(getargs_es_hash('abc'), 'abc') |
| 535 | self.assertEqual(getargs_es_hash(u'abc'), 'abc') |
| 536 | self.assertEqual(getargs_es_hash(u'abc\xe9', 'latin1'), 'abc\xe9') |
| 537 | self.assertRaises(UnicodeEncodeError, getargs_es_hash, u'abc\xe9', 'ascii') |
| 538 | self.assertRaises(LookupError, getargs_es_hash, u'abc', 'spam') |
| 539 | self.assertRaises(TypeError, getargs_es_hash, |
| 540 | bytearray('bytearray'), 'latin1') |
| 541 | self.assertRaises(TypeError, getargs_es_hash, |
| 542 | memoryview('memoryview'), 'latin1') |
| 543 | with test_support.check_py3k_warnings(): |
| 544 | self.assertEqual(getargs_es_hash(buffer('abc'), 'ascii'), 'abc') |
| 545 | self.assertEqual(getargs_es_hash(buffer(u'abc'), 'ascii'), 'abc') |
| 546 | self.assertRaises(TypeError, getargs_es_hash, None, 'latin1') |
| 547 | self.assertEqual(getargs_es_hash('nul:\0', 'latin1'), 'nul:\0') |
| 548 | self.assertEqual(getargs_es_hash(u'nul:\0', 'latin1'), 'nul:\0') |
| 549 | |
| 550 | buf = bytearray('x'*8) |
| 551 | self.assertEqual(getargs_es_hash(u'abc\xe9', 'latin1', buf), 'abc\xe9') |
| 552 | self.assertEqual(buf, bytearray('abc\xe9\x00xxx')) |
| 553 | buf = bytearray('x'*5) |
| 554 | self.assertEqual(getargs_es_hash(u'abc\xe9', 'latin1', buf), 'abc\xe9') |
| 555 | self.assertEqual(buf, bytearray('abc\xe9\x00')) |
| 556 | buf = bytearray('x'*4) |
| 557 | self.assertRaises(TypeError, getargs_es_hash, u'abc\xe9', 'latin1', buf) |
| 558 | self.assertEqual(buf, bytearray('x'*4)) |
| 559 | buf = bytearray() |
| 560 | self.assertRaises(TypeError, getargs_es_hash, u'abc\xe9', 'latin1', buf) |
| 561 | |
| 562 | def test_et_hash(self): |
| 563 | from _testcapi import getargs_et_hash |
| 564 | self.assertEqual(getargs_et_hash('abc\xe9'), 'abc\xe9') |
| 565 | self.assertEqual(getargs_et_hash(u'abc'), 'abc') |
| 566 | self.assertEqual(getargs_et_hash('abc\xe9', 'ascii'), 'abc\xe9') |
| 567 | self.assertEqual(getargs_et_hash(u'abc\xe9', 'latin1'), 'abc\xe9') |
| 568 | self.assertRaises(UnicodeEncodeError, getargs_et_hash, |
| 569 | u'abc\xe9', 'ascii') |
| 570 | self.assertRaises(LookupError, getargs_et_hash, u'abc', 'spam') |
| 571 | self.assertRaises(TypeError, getargs_et_hash, |
| 572 | bytearray('bytearray'), 'latin1') |
| 573 | self.assertRaises(TypeError, getargs_et_hash, |
| 574 | memoryview('memoryview'), 'latin1') |
| 575 | with test_support.check_py3k_warnings(): |
| 576 | self.assertEqual(getargs_et_hash(buffer('abc'), 'ascii'), 'abc') |
| 577 | self.assertEqual(getargs_et_hash(buffer(u'abc'), 'ascii'), 'abc') |
| 578 | self.assertRaises(TypeError, getargs_et_hash, None, 'latin1') |
| 579 | self.assertEqual(getargs_et_hash('nul:\0', 'latin1'), 'nul:\0') |
| 580 | self.assertEqual(getargs_et_hash(u'nul:\0', 'latin1'), 'nul:\0') |
| 581 | |
| 582 | buf = bytearray('x'*8) |
| 583 | self.assertEqual(getargs_et_hash(u'abc\xe9', 'latin1', buf), 'abc\xe9') |
| 584 | self.assertEqual(buf, bytearray('abc\xe9\x00xxx')) |
| 585 | buf = bytearray('x'*5) |
| 586 | self.assertEqual(getargs_et_hash(u'abc\xe9', 'latin1', buf), 'abc\xe9') |
| 587 | self.assertEqual(buf, bytearray('abc\xe9\x00')) |
| 588 | buf = bytearray('x'*4) |
| 589 | self.assertRaises(TypeError, getargs_et_hash, u'abc\xe9', 'latin1', buf) |
| 590 | self.assertEqual(buf, bytearray('x'*4)) |
| 591 | buf = bytearray() |
| 592 | self.assertRaises(TypeError, getargs_et_hash, u'abc\xe9', 'latin1', buf) |
| 593 | |
| 594 | def test_u(self): |
| 595 | from _testcapi import getargs_u |
| 596 | self.assertEqual(getargs_u(u'abc\xe9'), u'abc\xe9') |
| 597 | self.assertEqual(getargs_u(u'nul:\0'), u'nul:') |
| 598 | self.assertRaises(TypeError, getargs_u, 'bytes') |
| 599 | self.assertRaises(TypeError, getargs_u, bytearray('bytearray')) |
| 600 | self.assertRaises(TypeError, getargs_u, memoryview('memoryview')) |
| 601 | with test_support.check_py3k_warnings(): |
| 602 | self.assertRaises(TypeError, getargs_u, buffer('buffer')) |
| 603 | self.assertRaises(TypeError, getargs_u, None) |
| 604 | |
| 605 | def test_u_hash(self): |
| 606 | from _testcapi import getargs_u_hash |
| 607 | self.assertEqual(getargs_u_hash(u'abc\xe9'), u'abc\xe9') |
| 608 | self.assertEqual(getargs_u_hash(u'nul:\0'), u'nul:\0') |
| 609 | self.assertRaises(TypeError, getargs_u_hash, 'bytes') |
| 610 | self.assertRaises(TypeError, getargs_u_hash, bytearray('bytearray')) |
| 611 | self.assertRaises(TypeError, getargs_u_hash, memoryview('memoryview')) |
| 612 | with test_support.check_py3k_warnings(): |
| 613 | self.assertRaises(TypeError, getargs_u_hash, buffer('buffer')) |
| 614 | self.assertRaises(TypeError, getargs_u_hash, None) |
| 615 | |
| 616 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 617 | def test_main(): |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 618 | tests = [Signed_TestCase, Unsigned_TestCase, LongLong_TestCase, |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame^] | 619 | Tuple_TestCase, Keywords_TestCase, |
| 620 | Bytes_TestCase, String_TestCase, Unicode_TestCase] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 621 | test_support.run_unittest(*tests) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 622 | |
| 623 | if __name__ == "__main__": |
| 624 | test_main() |