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