PyXR

c:\python24\lib \ test \ test_extcall.py



0001 from test.test_support import verify, verbose, TestFailed, sortdict
0002 from UserList import UserList
0003 
0004 def e(a, b):
0005     print a, b
0006 
0007 def f(*a, **k):
0008     print a, sortdict(k)
0009 
0010 def g(x, *y, **z):
0011     print x, y, sortdict(z)
0012 
0013 def h(j=1, a=2, h=3):
0014     print j, a, h
0015 
0016 f()
0017 f(1)
0018 f(1, 2)
0019 f(1, 2, 3)
0020 
0021 f(1, 2, 3, *(4, 5))
0022 f(1, 2, 3, *[4, 5])
0023 f(1, 2, 3, *UserList([4, 5]))
0024 f(1, 2, 3, **{'a':4, 'b':5})
0025 f(1, 2, 3, *(4, 5), **{'a':6, 'b':7})
0026 f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b':9})
0027 
0028 # Verify clearing of SF bug #733667
0029 try:
0030     e(c=3)
0031 except TypeError:
0032     pass
0033 else:
0034     print "should raise TypeError: e() got an unexpected keyword argument 'c'"
0035 
0036 try:
0037     g()
0038 except TypeError, err:
0039     print "TypeError:", err
0040 else:
0041     print "should raise TypeError: not enough arguments; expected 1, got 0"
0042 
0043 try:
0044     g(*())
0045 except TypeError, err:
0046     print "TypeError:", err
0047 else:
0048     print "should raise TypeError: not enough arguments; expected 1, got 0"
0049 
0050 try:
0051     g(*(), **{})
0052 except TypeError, err:
0053     print "TypeError:", err
0054 else:
0055     print "should raise TypeError: not enough arguments; expected 1, got 0"
0056 
0057 g(1)
0058 g(1, 2)
0059 g(1, 2, 3)
0060 g(1, 2, 3, *(4, 5))
0061 class Nothing: pass
0062 try:
0063     g(*Nothing())
0064 except TypeError, attr:
0065     pass
0066 else:
0067     print "should raise TypeError"
0068 
0069 class Nothing:
0070     def __len__(self):
0071         return 5
0072 try:
0073     g(*Nothing())
0074 except TypeError, attr:
0075     pass
0076 else:
0077     print "should raise TypeError"
0078 
0079 class Nothing:
0080     def __len__(self):
0081         return 5
0082     def __getitem__(self, i):
0083         if i < 3:
0084             return i
0085         else:
0086             raise IndexError, i
0087 g(*Nothing())
0088 
0089 class Nothing:
0090     def __init__(self):
0091         self.c = 0
0092     def __iter__(self):
0093         return self
0094 try:
0095     g(*Nothing())
0096 except TypeError, attr:
0097     pass
0098 else:
0099     print "should raise TypeError"
0100 
0101 class Nothing:
0102     def __init__(self):
0103         self.c = 0
0104     def __iter__(self):
0105         return self
0106     def next(self):
0107         if self.c == 4:
0108             raise StopIteration
0109         c = self.c
0110         self.c += 1
0111         return c
0112 g(*Nothing())
0113 
0114 # make sure the function call doesn't stomp on the dictionary?
0115 d = {'a': 1, 'b': 2, 'c': 3}
0116 d2 = d.copy()
0117 verify(d == d2)
0118 g(1, d=4, **d)
0119 print sortdict(d)
0120 print sortdict(d2)
0121 verify(d == d2, "function call modified dictionary")
0122 
0123 # what about willful misconduct?
0124 def saboteur(**kw):
0125     kw['x'] = locals() # yields a cyclic kw
0126     return kw
0127 d = {}
0128 kw = saboteur(a=1, **d)
0129 verify(d == {})
0130 # break the cycle
0131 del kw['x']
0132 
0133 try:
0134     g(1, 2, 3, **{'x':4, 'y':5})
0135 except TypeError, err:
0136     print err
0137 else:
0138     print "should raise TypeError: keyword parameter redefined"
0139 
0140 try:
0141     g(1, 2, 3, a=4, b=5, *(6, 7), **{'a':8, 'b':9})
0142 except TypeError, err:
0143     print err
0144 else:
0145     print "should raise TypeError: keyword parameter redefined"
0146 
0147 try:
0148     f(**{1:2})
0149 except TypeError, err:
0150     print err
0151 else:
0152     print "should raise TypeError: keywords must be strings"
0153 
0154 try:
0155     h(**{'e': 2})
0156 except TypeError, err:
0157     print err
0158 else:
0159     print "should raise TypeError: unexpected keyword argument: e"
0160 
0161 try:
0162     h(*h)
0163 except TypeError, err:
0164     print err
0165 else:
0166     print "should raise TypeError: * argument must be a tuple"
0167 
0168 try:
0169     dir(*h)
0170 except TypeError, err:
0171     print err
0172 else:
0173     print "should raise TypeError: * argument must be a tuple"
0174 
0175 try:
0176     None(*h)
0177 except TypeError, err:
0178     print err
0179 else:
0180     print "should raise TypeError: * argument must be a tuple"
0181 
0182 try:
0183     h(**h)
0184 except TypeError, err:
0185     print err
0186 else:
0187     print "should raise TypeError: ** argument must be a dictionary"
0188 
0189 try:
0190     dir(**h)
0191 except TypeError, err:
0192     print err
0193 else:
0194     print "should raise TypeError: ** argument must be a dictionary"
0195 
0196 try:
0197     None(**h)
0198 except TypeError, err:
0199     print err
0200 else:
0201     print "should raise TypeError: ** argument must be a dictionary"
0202 
0203 try:
0204     dir(b=1,**{'b':1})
0205 except TypeError, err:
0206     print err
0207 else:
0208     print "should raise TypeError: dir() got multiple values for keyword argument 'b'"
0209 
0210 def f2(*a, **b):
0211     return a, b
0212 
0213 d = {}
0214 for i in range(512):
0215     key = 'k%d' % i
0216     d[key] = i
0217 a, b = f2(1, *(2, 3), **d)
0218 print len(a), len(b), b == d
0219 
0220 class Foo:
0221     def method(self, arg1, arg2):
0222         return arg1 + arg2
0223 
0224 x = Foo()
0225 print Foo.method(*(x, 1, 2))
0226 print Foo.method(x, *(1, 2))
0227 try:
0228     print Foo.method(*(1, 2, 3))
0229 except TypeError, err:
0230     pass
0231 else:
0232     print 'expected a TypeError for unbound method call'
0233 try:
0234     print Foo.method(1, *(2, 3))
0235 except TypeError, err:
0236     pass
0237 else:
0238     print 'expected a TypeError for unbound method call'
0239 
0240 # A PyCFunction that takes only positional parameters should allow an
0241 # empty keyword dictionary to pass without a complaint, but raise a
0242 # TypeError if the dictionary is non-empty.
0243 id(1, **{})
0244 try:
0245     id(1, **{"foo": 1})
0246 except TypeError:
0247     pass
0248 else:
0249     raise TestFailed, 'expected TypeError; no exception raised'
0250 
0251 a, b, d, e, v, k = 'A', 'B', 'D', 'E', 'V', 'K'
0252 funcs = []
0253 maxargs = {}
0254 for args in ['', 'a', 'ab']:
0255     for defargs in ['', 'd', 'de']:
0256         for vararg in ['', 'v']:
0257             for kwarg in ['', 'k']:
0258                 name = 'z' + args + defargs + vararg + kwarg
0259                 arglist = list(args) + map(
0260                     lambda x: '%s="%s"' % (x, x), defargs)
0261                 if vararg: arglist.append('*' + vararg)
0262                 if kwarg: arglist.append('**' + kwarg)
0263                 decl = (('def %s(%s): print "ok %s", a, b, d, e, v, ' +
0264                          'type(k) is type ("") and k or sortdict(k)')
0265                          % (name, ', '.join(arglist), name))
0266                 exec(decl)
0267                 func = eval(name)
0268                 funcs.append(func)
0269                 maxargs[func] = len(args + defargs)
0270 
0271 for name in ['za', 'zade', 'zabk', 'zabdv', 'zabdevk']:
0272     func = eval(name)
0273     for args in [(), (1, 2), (1, 2, 3, 4, 5)]:
0274         for kwargs in ['', 'a', 'd', 'ad', 'abde']:
0275             kwdict = {}
0276             for k in kwargs: kwdict[k] = k + k
0277             print func.func_name, args, sortdict(kwdict), '->',
0278             try: func(*args, **kwdict)
0279             except TypeError, err: print err
0280 

Generated by PyXR 0.9.4
SourceForge.net Logo