test_extcall.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. # -*- coding: utf-8 -*-
  2. """Doctest for method/function calls.
  3. We're going the use these types for extra testing
  4. >>> from UserList import UserList
  5. >>> from UserDict import UserDict
  6. We're defining four helper functions
  7. >>> def e(a,b):
  8. ... print a, b
  9. >>> def f(*a, **k):
  10. ... print a, test_support.sortdict(k)
  11. >>> def g(x, *y, **z):
  12. ... print x, y, test_support.sortdict(z)
  13. >>> def h(j=1, a=2, h=3):
  14. ... print j, a, h
  15. Argument list examples
  16. >>> f()
  17. () {}
  18. >>> f(1)
  19. (1,) {}
  20. >>> f(1, 2)
  21. (1, 2) {}
  22. >>> f(1, 2, 3)
  23. (1, 2, 3) {}
  24. >>> f(1, 2, 3, *(4, 5))
  25. (1, 2, 3, 4, 5) {}
  26. >>> f(1, 2, 3, *[4, 5])
  27. (1, 2, 3, 4, 5) {}
  28. >>> f(1, 2, 3, *UserList([4, 5]))
  29. (1, 2, 3, 4, 5) {}
  30. Here we add keyword arguments
  31. >>> f(1, 2, 3, **{'a':4, 'b':5})
  32. (1, 2, 3) {'a': 4, 'b': 5}
  33. >>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
  34. (1, 2, 3, 4, 5) {'a': 6, 'b': 7}
  35. >>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
  36. (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
  37. >>> f(1, 2, 3, **UserDict(a=4, b=5))
  38. (1, 2, 3) {'a': 4, 'b': 5}
  39. >>> f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7))
  40. (1, 2, 3, 4, 5) {'a': 6, 'b': 7}
  41. >>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9))
  42. (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
  43. Examples with invalid arguments (TypeErrors). We're also testing the function
  44. names in the exception messages.
  45. Verify clearing of SF bug #733667
  46. >>> e(c=4)
  47. Traceback (most recent call last):
  48. ...
  49. TypeError: e() got an unexpected keyword argument 'c'
  50. >>> g()
  51. Traceback (most recent call last):
  52. ...
  53. TypeError: g() takes at least 1 argument (0 given)
  54. >>> g(*())
  55. Traceback (most recent call last):
  56. ...
  57. TypeError: g() takes at least 1 argument (0 given)
  58. >>> g(*(), **{})
  59. Traceback (most recent call last):
  60. ...
  61. TypeError: g() takes at least 1 argument (0 given)
  62. >>> g(1)
  63. 1 () {}
  64. >>> g(1, 2)
  65. 1 (2,) {}
  66. >>> g(1, 2, 3)
  67. 1 (2, 3) {}
  68. >>> g(1, 2, 3, *(4, 5))
  69. 1 (2, 3, 4, 5) {}
  70. >>> class Nothing: pass
  71. ...
  72. >>> g(*Nothing())
  73. Traceback (most recent call last):
  74. ...
  75. TypeError: g() argument after * must be an iterable, not instance
  76. >>> class Nothing:
  77. ... def __len__(self): return 5
  78. ...
  79. >>> g(*Nothing())
  80. Traceback (most recent call last):
  81. ...
  82. TypeError: g() argument after * must be an iterable, not instance
  83. >>> class Nothing():
  84. ... def __len__(self): return 5
  85. ... def __getitem__(self, i):
  86. ... if i<3: return i
  87. ... else: raise IndexError(i)
  88. ...
  89. >>> g(*Nothing())
  90. 0 (1, 2) {}
  91. >>> class Nothing:
  92. ... def __init__(self): self.c = 0
  93. ... def __iter__(self): return self
  94. ... def next(self):
  95. ... if self.c == 4:
  96. ... raise StopIteration
  97. ... c = self.c
  98. ... self.c += 1
  99. ... return c
  100. ...
  101. >>> g(*Nothing())
  102. 0 (1, 2, 3) {}
  103. Check for issue #4806: Does a TypeError in a generator get propagated with the
  104. right error message?
  105. >>> def broken(): raise TypeError("myerror")
  106. ...
  107. >>> g(*(broken() for i in range(1)))
  108. Traceback (most recent call last):
  109. ...
  110. TypeError: myerror
  111. Make sure that the function doesn't stomp the dictionary
  112. >>> d = {'a': 1, 'b': 2, 'c': 3}
  113. >>> d2 = d.copy()
  114. >>> g(1, d=4, **d)
  115. 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}
  116. >>> d == d2
  117. True
  118. What about willful misconduct?
  119. >>> def saboteur(**kw):
  120. ... kw['x'] = 'm'
  121. ... return kw
  122. >>> d = {}
  123. >>> kw = saboteur(a=1, **d)
  124. >>> d
  125. {}
  126. >>> g(1, 2, 3, **{'x': 4, 'y': 5})
  127. Traceback (most recent call last):
  128. ...
  129. TypeError: g() got multiple values for keyword argument 'x'
  130. >>> f(**{1:2})
  131. Traceback (most recent call last):
  132. ...
  133. TypeError: f() keywords must be strings
  134. >>> h(**{'e': 2})
  135. Traceback (most recent call last):
  136. ...
  137. TypeError: h() got an unexpected keyword argument 'e'
  138. >>> h(*h)
  139. Traceback (most recent call last):
  140. ...
  141. TypeError: h() argument after * must be an iterable, not function
  142. >>> dir(*h)
  143. Traceback (most recent call last):
  144. ...
  145. TypeError: dir() argument after * must be an iterable, not function
  146. >>> None(*h)
  147. Traceback (most recent call last):
  148. ...
  149. TypeError: NoneType object argument after * must be an iterable, \
  150. not function
  151. >>> h(**h)
  152. Traceback (most recent call last):
  153. ...
  154. TypeError: h() argument after ** must be a mapping, not function
  155. >>> dir(**h)
  156. Traceback (most recent call last):
  157. ...
  158. TypeError: dir() argument after ** must be a mapping, not function
  159. >>> None(**h)
  160. Traceback (most recent call last):
  161. ...
  162. TypeError: NoneType object argument after ** must be a mapping, \
  163. not function
  164. >>> dir(b=1, **{'b': 1})
  165. Traceback (most recent call last):
  166. ...
  167. TypeError: dir() got multiple values for keyword argument 'b'
  168. Another helper function
  169. >>> def f2(*a, **b):
  170. ... return a, b
  171. >>> d = {}
  172. >>> for i in xrange(512):
  173. ... key = 'k%d' % i
  174. ... d[key] = i
  175. >>> a, b = f2(1, *(2,3), **d)
  176. >>> len(a), len(b), b == d
  177. (3, 512, True)
  178. >>> class Foo:
  179. ... def method(self, arg1, arg2):
  180. ... return arg1+arg2
  181. >>> x = Foo()
  182. >>> Foo.method(*(x, 1, 2))
  183. 3
  184. >>> Foo.method(x, *(1, 2))
  185. 3
  186. >>> Foo.method(*(1, 2, 3))
  187. Traceback (most recent call last):
  188. ...
  189. TypeError: unbound method method() must be called with Foo instance as \
  190. first argument (got int instance instead)
  191. >>> Foo.method(1, *[2, 3])
  192. Traceback (most recent call last):
  193. ...
  194. TypeError: unbound method method() must be called with Foo instance as \
  195. first argument (got int instance instead)
  196. A PyCFunction that takes only positional parameters should allow an
  197. empty keyword dictionary to pass without a complaint, but raise a
  198. TypeError if te dictionary is not empty
  199. >>> try:
  200. ... silence = id(1, *{})
  201. ... True
  202. ... except:
  203. ... False
  204. True
  205. >>> id(1, **{'foo': 1})
  206. Traceback (most recent call last):
  207. ...
  208. TypeError: id() takes no keyword arguments
  209. A corner case of keyword dictionary items being deleted during
  210. the function call setup. See <http://bugs.python.org/issue2016>.
  211. >>> class Name(str):
  212. ... def __eq__(self, other):
  213. ... try:
  214. ... del x[self]
  215. ... except KeyError:
  216. ... pass
  217. ... return str.__eq__(self, other)
  218. ... def __hash__(self):
  219. ... return str.__hash__(self)
  220. >>> x = {Name("a"):1, Name("b"):2}
  221. >>> def f(a, b):
  222. ... print a,b
  223. >>> f(**x)
  224. 1 2
  225. An obscure message:
  226. >>> def f(a, b):
  227. ... pass
  228. >>> f(b=1)
  229. Traceback (most recent call last):
  230. ...
  231. TypeError: f() takes exactly 2 arguments (1 given)
  232. The number of arguments passed in includes keywords:
  233. >>> def f(a):
  234. ... pass
  235. >>> f(6, a=4, *(1, 2, 3))
  236. Traceback (most recent call last):
  237. ...
  238. TypeError: f() takes exactly 1 argument (5 given)
  239. """
  240. import unittest
  241. import sys
  242. from test import test_support
  243. class ExtCallTest(unittest.TestCase):
  244. def test_unicode_keywords(self):
  245. def f(a):
  246. return a
  247. self.assertEqual(f(**{u'a': 4}), 4)
  248. self.assertRaises(TypeError, f, **{u'stören': 4})
  249. self.assertRaises(TypeError, f, **{u'someLongString':2})
  250. try:
  251. f(a=4, **{u'a': 4})
  252. except TypeError:
  253. pass
  254. else:
  255. self.fail("duplicate arguments didn't raise")
  256. def test_main():
  257. test_support.run_doctest(sys.modules[__name__], True)
  258. test_support.run_unittest(ExtCallTest)
  259. if __name__ == '__main__':
  260. test_main()