/Lib/test/test_extcall.py

http://unladen-swallow.googlecode.com/ · Python · 262 lines · 244 code · 16 blank · 2 comment · 4 complexity · 8bc59b3dc8d8abd242e40708502825d4 MD5 · raw file

  1. """Doctest for method/function calls.
  2. We're going the use these types for extra testing
  3. >>> from UserList import UserList
  4. >>> from UserDict import UserDict
  5. We're defining four helper functions
  6. >>> def e(a,b):
  7. ... print a, b
  8. >>> def f(*a, **k):
  9. ... print a, test_support.sortdict(k)
  10. >>> def g(x, *y, **z):
  11. ... print x, y, test_support.sortdict(z)
  12. >>> def h(j=1, a=2, h=3):
  13. ... print j, a, h
  14. Argument list examples
  15. >>> f()
  16. () {}
  17. >>> f(1)
  18. (1,) {}
  19. >>> f(1, 2)
  20. (1, 2) {}
  21. >>> f(1, 2, 3)
  22. (1, 2, 3) {}
  23. >>> f(1, 2, 3, *(4, 5))
  24. (1, 2, 3, 4, 5) {}
  25. >>> f(1, 2, 3, *[4, 5])
  26. (1, 2, 3, 4, 5) {}
  27. >>> f(1, 2, 3, *UserList([4, 5]))
  28. (1, 2, 3, 4, 5) {}
  29. Here we add keyword arguments
  30. >>> f(1, 2, 3, **{'a':4, 'b':5})
  31. (1, 2, 3) {'a': 4, 'b': 5}
  32. >>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
  33. (1, 2, 3, 4, 5) {'a': 6, 'b': 7}
  34. >>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
  35. (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
  36. >>> f(1, 2, 3, **UserDict(a=4, b=5))
  37. (1, 2, 3) {'a': 4, 'b': 5}
  38. >>> f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7))
  39. (1, 2, 3, 4, 5) {'a': 6, 'b': 7}
  40. >>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9))
  41. (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
  42. Examples with invalid arguments (TypeErrors). We're also testing the function
  43. names in the exception messages.
  44. Verify clearing of SF bug #733667
  45. >>> e(c=4)
  46. Traceback (most recent call last):
  47. ...
  48. TypeError: e() got an unexpected keyword argument 'c'
  49. >>> g()
  50. Traceback (most recent call last):
  51. ...
  52. TypeError: g() takes at least 1 argument (0 given)
  53. >>> g(*())
  54. Traceback (most recent call last):
  55. ...
  56. TypeError: g() takes at least 1 argument (0 given)
  57. >>> g(*(), **{})
  58. Traceback (most recent call last):
  59. ...
  60. TypeError: g() takes at least 1 argument (0 given)
  61. >>> g(1)
  62. 1 () {}
  63. >>> g(1, 2)
  64. 1 (2,) {}
  65. >>> g(1, 2, 3)
  66. 1 (2, 3) {}
  67. >>> g(1, 2, 3, *(4, 5))
  68. 1 (2, 3, 4, 5) {}
  69. >>> class Nothing: pass
  70. ...
  71. >>> g(*Nothing())
  72. Traceback (most recent call last):
  73. ...
  74. TypeError: g() argument after * must be a sequence, not instance
  75. >>> class Nothing:
  76. ... def __len__(self): return 5
  77. ...
  78. >>> g(*Nothing())
  79. Traceback (most recent call last):
  80. ...
  81. TypeError: g() argument after * must be a sequence, not instance
  82. >>> class Nothing():
  83. ... def __len__(self): return 5
  84. ... def __getitem__(self, i):
  85. ... if i<3: return i
  86. ... else: raise IndexError(i)
  87. ...
  88. >>> g(*Nothing())
  89. 0 (1, 2) {}
  90. >>> class Nothing:
  91. ... def __init__(self): self.c = 0
  92. ... def __iter__(self): return self
  93. ... def next(self):
  94. ... if self.c == 4:
  95. ... raise StopIteration
  96. ... c = self.c
  97. ... self.c += 1
  98. ... return c
  99. ...
  100. >>> g(*Nothing())
  101. 0 (1, 2, 3) {}
  102. Make sure that the function doesn't stomp the dictionary
  103. >>> d = {'a': 1, 'b': 2, 'c': 3}
  104. >>> d2 = d.copy()
  105. >>> g(1, d=4, **d)
  106. 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}
  107. >>> d == d2
  108. True
  109. What about willful misconduct?
  110. >>> def saboteur(**kw):
  111. ... kw['x'] = 'm'
  112. ... return kw
  113. >>> d = {}
  114. >>> kw = saboteur(a=1, **d)
  115. >>> d
  116. {}
  117. >>> g(1, 2, 3, **{'x': 4, 'y': 5})
  118. Traceback (most recent call last):
  119. ...
  120. TypeError: g() got multiple values for keyword argument 'x'
  121. >>> f(**{1:2})
  122. Traceback (most recent call last):
  123. ...
  124. TypeError: f() keywords must be strings
  125. >>> h(**{'e': 2})
  126. Traceback (most recent call last):
  127. ...
  128. TypeError: h() got an unexpected keyword argument 'e'
  129. >>> h(*h)
  130. Traceback (most recent call last):
  131. ...
  132. TypeError: h() argument after * must be a sequence, not function
  133. >>> dir(*h)
  134. Traceback (most recent call last):
  135. ...
  136. TypeError: dir() argument after * must be a sequence, not function
  137. >>> None(*h)
  138. Traceback (most recent call last):
  139. ...
  140. TypeError: NoneType object argument after * must be a sequence, \
  141. not function
  142. >>> h(**h)
  143. Traceback (most recent call last):
  144. ...
  145. TypeError: h() argument after ** must be a mapping, not function
  146. >>> dir(**h)
  147. Traceback (most recent call last):
  148. ...
  149. TypeError: dir() argument after ** must be a mapping, not function
  150. >>> None(**h)
  151. Traceback (most recent call last):
  152. ...
  153. TypeError: NoneType object argument after ** must be a mapping, \
  154. not function
  155. >>> dir(b=1, **{'b': 1})
  156. Traceback (most recent call last):
  157. ...
  158. TypeError: dir() got multiple values for keyword argument 'b'
  159. Another helper function
  160. >>> def f2(*a, **b):
  161. ... return a, b
  162. >>> d = {}
  163. >>> for i in xrange(512):
  164. ... key = 'k%d' % i
  165. ... d[key] = i
  166. >>> a, b = f2(1, *(2,3), **d)
  167. >>> len(a), len(b), b == d
  168. (3, 512, True)
  169. >>> class Foo:
  170. ... def method(self, arg1, arg2):
  171. ... return arg1+arg2
  172. >>> x = Foo()
  173. >>> Foo.method(*(x, 1, 2))
  174. 3
  175. >>> Foo.method(x, *(1, 2))
  176. 3
  177. >>> Foo.method(*(1, 2, 3))
  178. Traceback (most recent call last):
  179. ...
  180. TypeError: unbound method method() must be called with Foo instance as \
  181. first argument (got int instance instead)
  182. >>> Foo.method(1, *[2, 3])
  183. Traceback (most recent call last):
  184. ...
  185. TypeError: unbound method method() must be called with Foo instance as \
  186. first argument (got int instance instead)
  187. A PyCFunction that takes only positional parameters shoud allow an
  188. empty keyword dictionary to pass without a complaint, but raise a
  189. TypeError if te dictionary is not empty
  190. >>> try:
  191. ... silence = id(1, *{})
  192. ... True
  193. ... except:
  194. ... False
  195. True
  196. >>> id(1, **{'foo': 1})
  197. Traceback (most recent call last):
  198. ...
  199. TypeError: id() takes no keyword arguments
  200. """
  201. from test import test_support
  202. def test_main():
  203. from test import test_extcall # self import
  204. test_support.run_doctest(test_extcall, True)
  205. if __name__ == '__main__':
  206. test_main()