PageRenderTime 417ms CodeModel.GetById 35ms app.highlight 314ms RepoModel.GetById 30ms app.codeStats 1ms

/Lib/test/test_getargs2.py

http://unladen-swallow.googlecode.com/
Python | 312 lines | 286 code | 2 blank | 24 comment | 2 complexity | d09f4ba3dba7d58a9543d87d003d1270 MD5 | raw file
  1import unittest
  2from test import test_support
  3from _testcapi import getargs_keywords
  4
  5import warnings
  6warnings.filterwarnings("ignore",
  7                        category=DeprecationWarning,
  8                        message=".*integer argument expected, got float",
  9                        module=__name__)
 10warnings.filterwarnings("ignore",
 11                        category=DeprecationWarning,
 12                        message=".*integer argument expected, got float",
 13                        module="unittest")
 14
 15"""
 16> How about the following counterproposal. This also changes some of
 17> the other format codes to be a little more regular.
 18>
 19> Code C type Range check
 20>
 21> b unsigned char 0..UCHAR_MAX
 22> h signed short SHRT_MIN..SHRT_MAX
 23> B unsigned char none **
 24> H unsigned short none **
 25> k * unsigned long none
 26> I * unsigned int 0..UINT_MAX
 27
 28
 29> i int INT_MIN..INT_MAX
 30> l long LONG_MIN..LONG_MAX
 31
 32> K * unsigned long long none
 33> L long long LLONG_MIN..LLONG_MAX
 34
 35> Notes:
 36>
 37> * New format codes.
 38>
 39> ** Changed from previous "range-and-a-half" to "none"; the
 40> range-and-a-half checking wasn't particularly useful.
 41
 42Plus a C API or two, e.g. PyInt_AsLongMask() ->
 43unsigned long and PyInt_AsLongLongMask() -> unsigned
 44long long (if that exists).
 45"""
 46
 47LARGE = 0x7FFFFFFF
 48VERY_LARGE = 0xFF0000121212121212121242L
 49
 50from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \
 51     INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX
 52
 53# fake, they are not defined in Python's header files
 54LLONG_MAX = 2**63-1
 55LLONG_MIN = -2**63
 56ULLONG_MAX = 2**64-1
 57
 58class Long:
 59    def __int__(self):
 60        return 99L
 61
 62class Int:
 63    def __int__(self):
 64        return 99
 65
 66class Unsigned_TestCase(unittest.TestCase):
 67    def test_b(self):
 68        from _testcapi import getargs_b
 69        # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX)
 70        self.failUnlessEqual(3, getargs_b(3.14))
 71        self.failUnlessEqual(99, getargs_b(Long()))
 72        self.failUnlessEqual(99, getargs_b(Int()))
 73
 74        self.assertRaises(OverflowError, getargs_b, -1)
 75        self.failUnlessEqual(0, getargs_b(0))
 76        self.failUnlessEqual(UCHAR_MAX, getargs_b(UCHAR_MAX))
 77        self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1)
 78
 79        self.failUnlessEqual(42, getargs_b(42))
 80        self.failUnlessEqual(42, getargs_b(42L))
 81        self.assertRaises(OverflowError, getargs_b, VERY_LARGE)
 82
 83    def test_B(self):
 84        from _testcapi import getargs_B
 85        # B returns 'unsigned char', no range checking
 86        self.failUnlessEqual(3, getargs_B(3.14))
 87        self.failUnlessEqual(99, getargs_B(Long()))
 88        self.failUnlessEqual(99, getargs_B(Int()))
 89
 90        self.failUnlessEqual(UCHAR_MAX, getargs_B(-1))
 91        self.failUnlessEqual(UCHAR_MAX, getargs_B(-1L))
 92        self.failUnlessEqual(0, getargs_B(0))
 93        self.failUnlessEqual(UCHAR_MAX, getargs_B(UCHAR_MAX))
 94        self.failUnlessEqual(0, getargs_B(UCHAR_MAX+1))
 95
 96        self.failUnlessEqual(42, getargs_B(42))
 97        self.failUnlessEqual(42, getargs_B(42L))
 98        self.failUnlessEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE))
 99
100    def test_H(self):
101        from _testcapi import getargs_H
102        # H returns 'unsigned short', no range checking
103        self.failUnlessEqual(3, getargs_H(3.14))
104        self.failUnlessEqual(99, getargs_H(Long()))
105        self.failUnlessEqual(99, getargs_H(Int()))
106
107        self.failUnlessEqual(USHRT_MAX, getargs_H(-1))
108        self.failUnlessEqual(0, getargs_H(0))
109        self.failUnlessEqual(USHRT_MAX, getargs_H(USHRT_MAX))
110        self.failUnlessEqual(0, getargs_H(USHRT_MAX+1))
111
112        self.failUnlessEqual(42, getargs_H(42))
113        self.failUnlessEqual(42, getargs_H(42L))
114
115        self.failUnlessEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE))
116
117    def test_I(self):
118        from _testcapi import getargs_I
119        # I returns 'unsigned int', no range checking
120        self.failUnlessEqual(3, getargs_I(3.14))
121        self.failUnlessEqual(99, getargs_I(Long()))
122        self.failUnlessEqual(99, getargs_I(Int()))
123
124        self.failUnlessEqual(UINT_MAX, getargs_I(-1))
125        self.failUnlessEqual(0, getargs_I(0))
126        self.failUnlessEqual(UINT_MAX, getargs_I(UINT_MAX))
127        self.failUnlessEqual(0, getargs_I(UINT_MAX+1))
128
129        self.failUnlessEqual(42, getargs_I(42))
130        self.failUnlessEqual(42, getargs_I(42L))
131
132        self.failUnlessEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE))
133
134    def test_k(self):
135        from _testcapi import getargs_k
136        # k returns 'unsigned long', no range checking
137        # it does not accept float, or instances with __int__
138        self.assertRaises(TypeError, getargs_k, 3.14)
139        self.assertRaises(TypeError, getargs_k, Long())
140        self.assertRaises(TypeError, getargs_k, Int())
141
142        self.failUnlessEqual(ULONG_MAX, getargs_k(-1))
143        self.failUnlessEqual(0, getargs_k(0))
144        self.failUnlessEqual(ULONG_MAX, getargs_k(ULONG_MAX))
145        self.failUnlessEqual(0, getargs_k(ULONG_MAX+1))
146
147        self.failUnlessEqual(42, getargs_k(42))
148        self.failUnlessEqual(42, getargs_k(42L))
149
150        self.failUnlessEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE))
151
152class Signed_TestCase(unittest.TestCase):
153    def test_i(self):
154        from _testcapi import getargs_i
155        # i returns 'int', and does range checking (INT_MIN ... INT_MAX)
156        self.failUnlessEqual(3, getargs_i(3.14))
157        self.failUnlessEqual(99, getargs_i(Long()))
158        self.failUnlessEqual(99, getargs_i(Int()))
159
160        self.assertRaises(OverflowError, getargs_i, INT_MIN-1)
161        self.failUnlessEqual(INT_MIN, getargs_i(INT_MIN))
162        self.failUnlessEqual(INT_MAX, getargs_i(INT_MAX))
163        self.assertRaises(OverflowError, getargs_i, INT_MAX+1)
164
165        self.failUnlessEqual(42, getargs_i(42))
166        self.failUnlessEqual(42, getargs_i(42L))
167        self.assertRaises(OverflowError, getargs_i, VERY_LARGE)
168
169    def test_l(self):
170        from _testcapi import getargs_l
171        # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX)
172        self.failUnlessEqual(3, getargs_l(3.14))
173        self.failUnlessEqual(99, getargs_l(Long()))
174        self.failUnlessEqual(99, getargs_l(Int()))
175
176        self.assertRaises(OverflowError, getargs_l, LONG_MIN-1)
177        self.failUnlessEqual(LONG_MIN, getargs_l(LONG_MIN))
178        self.failUnlessEqual(LONG_MAX, getargs_l(LONG_MAX))
179        self.assertRaises(OverflowError, getargs_l, LONG_MAX+1)
180
181        self.failUnlessEqual(42, getargs_l(42))
182        self.failUnlessEqual(42, getargs_l(42L))
183        self.assertRaises(OverflowError, getargs_l, VERY_LARGE)
184
185    def test_n(self):
186        from _testcapi import getargs_n
187        # n returns 'Py_ssize_t', and does range checking
188        # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX)
189        self.failUnlessEqual(3, getargs_n(3.14))
190        self.failUnlessEqual(99, getargs_n(Long()))
191        self.failUnlessEqual(99, getargs_n(Int()))
192
193        self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1)
194        self.failUnlessEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN))
195        self.failUnlessEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX))
196        self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1)
197
198        self.failUnlessEqual(42, getargs_n(42))
199        self.failUnlessEqual(42, getargs_n(42L))
200        self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
201
202
203class LongLong_TestCase(unittest.TestCase):
204    def test_L(self):
205        from _testcapi import getargs_L
206        # L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX)
207        self.failUnlessRaises(TypeError, getargs_L, "Hello")
208        self.failUnlessEqual(3, getargs_L(3.14))
209        self.failUnlessEqual(99, getargs_L(Long()))
210        self.failUnlessEqual(99, getargs_L(Int()))
211
212        self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)
213        self.failUnlessEqual(LLONG_MIN, getargs_L(LLONG_MIN))
214        self.failUnlessEqual(LLONG_MAX, getargs_L(LLONG_MAX))
215        self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1)
216
217        self.failUnlessEqual(42, getargs_L(42))
218        self.failUnlessEqual(42, getargs_L(42L))
219        self.assertRaises(OverflowError, getargs_L, VERY_LARGE)
220
221    def test_K(self):
222        from _testcapi import getargs_K
223        # K return 'unsigned long long', no range checking
224        self.assertRaises(TypeError, getargs_K, 3.14)
225        self.assertRaises(TypeError, getargs_K, Long())
226        self.assertRaises(TypeError, getargs_K, Int())
227        self.failUnlessEqual(ULLONG_MAX, getargs_K(ULLONG_MAX))
228        self.failUnlessEqual(0, getargs_K(0))
229        self.failUnlessEqual(0, getargs_K(ULLONG_MAX+1))
230
231        self.failUnlessEqual(42, getargs_K(42))
232        self.failUnlessEqual(42, getargs_K(42L))
233
234        self.failUnlessEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE))
235
236
237class Tuple_TestCase(unittest.TestCase):
238    def test_tuple(self):
239        from _testcapi import getargs_tuple
240
241        ret = getargs_tuple(1, (2, 3))
242        self.assertEquals(ret, (1,2,3))
243
244        # make sure invalid tuple arguments are handled correctly
245        class seq:
246            def __len__(self):
247                return 2
248            def __getitem__(self, n):
249                raise ValueError
250        self.assertRaises(TypeError, getargs_tuple, 1, seq())
251
252class Keywords_TestCase(unittest.TestCase):
253    def test_positional_args(self):
254        # using all positional args
255        self.assertEquals(
256            getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10),
257            (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
258            )
259    def test_mixed_args(self):
260        # positional and keyword args
261        self.assertEquals(
262            getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10),
263            (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
264            )
265    def test_keyword_args(self):
266        # all keywords
267        self.assertEquals(
268            getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10),
269            (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
270            )
271    def test_optional_args(self):
272        # missing optional keyword args, skipping tuples
273        self.assertEquals(
274            getargs_keywords(arg1=(1,2), arg2=3, arg5=10),
275            (1, 2, 3, -1, -1, -1, -1, -1, -1, 10)
276            )
277    def test_required_args(self):
278        # required arg missing
279        try:
280            getargs_keywords(arg1=(1,2))
281        except TypeError, err:
282            self.assertEquals(str(err), "Required argument 'arg2' (pos 2) not found")
283        else:
284            self.fail('TypeError should have been raised')
285    def test_too_many_args(self):
286        try:
287            getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111)
288        except TypeError, err:
289            self.assertEquals(str(err), "function takes at most 5 arguments (6 given)")
290        else:
291            self.fail('TypeError should have been raised')
292    def test_invalid_keyword(self):
293        # extraneous keyword arg
294        try:
295            getargs_keywords((1,2),3,arg5=10,arg666=666)
296        except TypeError, err:
297            self.assertEquals(str(err), "'arg666' is an invalid keyword argument for this function")
298        else:
299            self.fail('TypeError should have been raised')
300
301def test_main():
302    tests = [Signed_TestCase, Unsigned_TestCase, Tuple_TestCase, Keywords_TestCase]
303    try:
304        from _testcapi import getargs_L, getargs_K
305    except ImportError:
306        pass # PY_LONG_LONG not available
307    else:
308        tests.append(LongLong_TestCase)
309    test_support.run_unittest(*tests)
310
311if __name__ == "__main__":
312    test_main()