+++ /dev/null
-# -*- coding: utf-8 -*-\r
-\r
-"""Doctest for method/function calls.\r
-\r
-We're going the use these types for extra testing\r
-\r
- >>> from UserList import UserList\r
- >>> from UserDict import UserDict\r
-\r
-We're defining four helper functions\r
-\r
- >>> def e(a,b):\r
- ... print a, b\r
-\r
- >>> def f(*a, **k):\r
- ... print a, test_support.sortdict(k)\r
-\r
- >>> def g(x, *y, **z):\r
- ... print x, y, test_support.sortdict(z)\r
-\r
- >>> def h(j=1, a=2, h=3):\r
- ... print j, a, h\r
-\r
-Argument list examples\r
-\r
- >>> f()\r
- () {}\r
- >>> f(1)\r
- (1,) {}\r
- >>> f(1, 2)\r
- (1, 2) {}\r
- >>> f(1, 2, 3)\r
- (1, 2, 3) {}\r
- >>> f(1, 2, 3, *(4, 5))\r
- (1, 2, 3, 4, 5) {}\r
- >>> f(1, 2, 3, *[4, 5])\r
- (1, 2, 3, 4, 5) {}\r
- >>> f(1, 2, 3, *UserList([4, 5]))\r
- (1, 2, 3, 4, 5) {}\r
-\r
-Here we add keyword arguments\r
-\r
- >>> f(1, 2, 3, **{'a':4, 'b':5})\r
- (1, 2, 3) {'a': 4, 'b': 5}\r
- >>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})\r
- (1, 2, 3, 4, 5) {'a': 6, 'b': 7}\r
- >>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})\r
- (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}\r
-\r
- >>> f(1, 2, 3, **UserDict(a=4, b=5))\r
- (1, 2, 3) {'a': 4, 'b': 5}\r
- >>> f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7))\r
- (1, 2, 3, 4, 5) {'a': 6, 'b': 7}\r
- >>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9))\r
- (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}\r
-\r
-Examples with invalid arguments (TypeErrors). We're also testing the function\r
-names in the exception messages.\r
-\r
-Verify clearing of SF bug #733667\r
-\r
- >>> e(c=4)\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: e() got an unexpected keyword argument 'c'\r
-\r
- >>> g()\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: g() takes at least 1 argument (0 given)\r
-\r
- >>> g(*())\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: g() takes at least 1 argument (0 given)\r
-\r
- >>> g(*(), **{})\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: g() takes at least 1 argument (0 given)\r
-\r
- >>> g(1)\r
- 1 () {}\r
- >>> g(1, 2)\r
- 1 (2,) {}\r
- >>> g(1, 2, 3)\r
- 1 (2, 3) {}\r
- >>> g(1, 2, 3, *(4, 5))\r
- 1 (2, 3, 4, 5) {}\r
-\r
- >>> class Nothing: pass\r
- ...\r
- >>> g(*Nothing())\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: g() argument after * must be a sequence, not instance\r
-\r
- >>> class Nothing:\r
- ... def __len__(self): return 5\r
- ...\r
-\r
- >>> g(*Nothing())\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: g() argument after * must be a sequence, not instance\r
-\r
- >>> class Nothing():\r
- ... def __len__(self): return 5\r
- ... def __getitem__(self, i):\r
- ... if i<3: return i\r
- ... else: raise IndexError(i)\r
- ...\r
-\r
- >>> g(*Nothing())\r
- 0 (1, 2) {}\r
-\r
- >>> class Nothing:\r
- ... def __init__(self): self.c = 0\r
- ... def __iter__(self): return self\r
- ... def next(self):\r
- ... if self.c == 4:\r
- ... raise StopIteration\r
- ... c = self.c\r
- ... self.c += 1\r
- ... return c\r
- ...\r
-\r
- >>> g(*Nothing())\r
- 0 (1, 2, 3) {}\r
-\r
-Make sure that the function doesn't stomp the dictionary\r
-\r
- >>> d = {'a': 1, 'b': 2, 'c': 3}\r
- >>> d2 = d.copy()\r
- >>> g(1, d=4, **d)\r
- 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}\r
- >>> d == d2\r
- True\r
-\r
-What about willful misconduct?\r
-\r
- >>> def saboteur(**kw):\r
- ... kw['x'] = 'm'\r
- ... return kw\r
-\r
- >>> d = {}\r
- >>> kw = saboteur(a=1, **d)\r
- >>> d\r
- {}\r
-\r
-\r
- >>> g(1, 2, 3, **{'x': 4, 'y': 5})\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: g() got multiple values for keyword argument 'x'\r
-\r
- >>> f(**{1:2})\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: f() keywords must be strings\r
-\r
- >>> h(**{'e': 2})\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: h() got an unexpected keyword argument 'e'\r
-\r
- >>> h(*h)\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: h() argument after * must be a sequence, not function\r
-\r
- >>> dir(*h)\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: dir() argument after * must be a sequence, not function\r
-\r
- >>> None(*h)\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: NoneType object argument after * must be a sequence, \\r
-not function\r
-\r
- >>> h(**h)\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: h() argument after ** must be a mapping, not function\r
-\r
- >>> dir(**h)\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: dir() argument after ** must be a mapping, not function\r
-\r
- >>> None(**h)\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: NoneType object argument after ** must be a mapping, \\r
-not function\r
-\r
- >>> dir(b=1, **{'b': 1})\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: dir() got multiple values for keyword argument 'b'\r
-\r
-Another helper function\r
-\r
- >>> def f2(*a, **b):\r
- ... return a, b\r
-\r
-\r
- >>> d = {}\r
- >>> for i in xrange(512):\r
- ... key = 'k%d' % i\r
- ... d[key] = i\r
- >>> a, b = f2(1, *(2,3), **d)\r
- >>> len(a), len(b), b == d\r
- (3, 512, True)\r
-\r
- >>> class Foo:\r
- ... def method(self, arg1, arg2):\r
- ... return arg1+arg2\r
-\r
- >>> x = Foo()\r
- >>> Foo.method(*(x, 1, 2))\r
- 3\r
- >>> Foo.method(x, *(1, 2))\r
- 3\r
- >>> Foo.method(*(1, 2, 3))\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: unbound method method() must be called with Foo instance as \\r
-first argument (got int instance instead)\r
-\r
- >>> Foo.method(1, *[2, 3])\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: unbound method method() must be called with Foo instance as \\r
-first argument (got int instance instead)\r
-\r
-A PyCFunction that takes only positional parameters should allow an\r
-empty keyword dictionary to pass without a complaint, but raise a\r
-TypeError if te dictionary is not empty\r
-\r
- >>> try:\r
- ... silence = id(1, *{})\r
- ... True\r
- ... except:\r
- ... False\r
- True\r
-\r
- >>> id(1, **{'foo': 1})\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: id() takes no keyword arguments\r
-\r
-A corner case of keyword dictionary items being deleted during\r
-the function call setup. See <http://bugs.python.org/issue2016>.\r
-\r
- >>> class Name(str):\r
- ... def __eq__(self, other):\r
- ... try:\r
- ... del x[self]\r
- ... except KeyError:\r
- ... pass\r
- ... return str.__eq__(self, other)\r
- ... def __hash__(self):\r
- ... return str.__hash__(self)\r
-\r
- >>> x = {Name("a"):1, Name("b"):2}\r
- >>> def f(a, b):\r
- ... print a,b\r
- >>> f(**x)\r
- 1 2\r
-\r
-A obscure message:\r
-\r
- >>> def f(a, b):\r
- ... pass\r
- >>> f(b=1)\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: f() takes exactly 2 arguments (1 given)\r
-\r
-The number of arguments passed in includes keywords:\r
-\r
- >>> def f(a):\r
- ... pass\r
- >>> f(6, a=4, *(1, 2, 3))\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: f() takes exactly 1 argument (5 given)\r
-"""\r
-\r
-import unittest\r
-import sys\r
-from test import test_support\r
-\r
-\r
-class ExtCallTest(unittest.TestCase):\r
-\r
- def test_unicode_keywords(self):\r
- def f(a):\r
- return a\r
- self.assertEqual(f(**{u'a': 4}), 4)\r
- self.assertRaises(TypeError, f, **{u'stören': 4})\r
- self.assertRaises(TypeError, f, **{u'someLongString':2})\r
- try:\r
- f(a=4, **{u'a': 4})\r
- except TypeError:\r
- pass\r
- else:\r
- self.fail("duplicate arguments didn't raise")\r
-\r
-\r
-def test_main():\r
- test_support.run_doctest(sys.modules[__name__], True)\r
- test_support.run_unittest(ExtCallTest)\r
-\r
-if __name__ == '__main__':\r
- test_main()\r