Back to index

python3.2  3.2.2
Public Member Functions
test.test_inspect.TestGetcallargsFunctions Class Reference
Inheritance diagram for test.test_inspect.TestGetcallargsFunctions:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def assertEqualCallArgs
def assertEqualException
def makeCallable
def test_plain
def test_varargs
def test_varkw
def test_varkw_only
def test_keyword_only
def test_multiple_features
def test_errors

Detailed Description

Definition at line 575 of file test_inspect.py.


Member Function Documentation

def test.test_inspect.TestGetcallargsFunctions.assertEqualCallArgs (   self,
  func,
  call_params_string,
  locs = None 
)

Reimplemented in test.test_inspect.TestGetcallargsUnboundMethods.

Definition at line 577 of file test_inspect.py.

00577 
00578     def assertEqualCallArgs(self, func, call_params_string, locs=None):
00579         locs = dict(locs or {}, func=func)
00580         r1 = eval('func(%s)' % call_params_string, None, locs)
00581         r2 = eval('inspect.getcallargs(func, %s)' % call_params_string, None,
00582                   locs)
00583         self.assertEqual(r1, r2)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_inspect.TestGetcallargsFunctions.assertEqualException (   self,
  func,
  call_param_string,
  locs = None 
)

Reimplemented in test.test_inspect.TestGetcallargsUnboundMethods.

Definition at line 584 of file test_inspect.py.

00584 
00585     def assertEqualException(self, func, call_param_string, locs=None):
00586         locs = dict(locs or {}, func=func)
00587         try:
00588             eval('func(%s)' % call_param_string, None, locs)
00589         except Exception as e:
00590             ex1 = e
00591         else:
00592             self.fail('Exception not raised')
00593         try:
00594             eval('inspect.getcallargs(func, %s)' % call_param_string, None,
00595                  locs)
00596         except Exception as e:
00597             ex2 = e
00598         else:
00599             self.fail('Exception not raised')
00600         self.assertIs(type(ex1), type(ex2))
00601         self.assertEqual(str(ex1), str(ex2))
00602         del ex1, ex2

Here is the call graph for this function:

Here is the caller graph for this function:

Create a function that returns its locals()

Reimplemented in test.test_inspect.TestGetcallargsUnboundMethods, and test.test_inspect.TestGetcallargsMethods.

Definition at line 603 of file test_inspect.py.

00603 
00604     def makeCallable(self, signature):
00605         """Create a function that returns its locals()"""
00606         code = "lambda %s: locals()"
00607         return eval(code % signature)

Here is the caller graph for this function:

Definition at line 702 of file test_inspect.py.

00702 
00703     def test_errors(self):
00704         f0 = self.makeCallable('')
00705         f1 = self.makeCallable('a, b')
00706         f2 = self.makeCallable('a, b=1')
00707         # f0 takes no arguments
00708         self.assertEqualException(f0, '1')
00709         self.assertEqualException(f0, 'x=1')
00710         self.assertEqualException(f0, '1,x=1')
00711         # f1 takes exactly 2 arguments
00712         self.assertEqualException(f1, '')
00713         self.assertEqualException(f1, '1')
00714         self.assertEqualException(f1, 'a=2')
00715         self.assertEqualException(f1, 'b=3')
00716         # f2 takes at least 1 argument
00717         self.assertEqualException(f2, '')
00718         self.assertEqualException(f2, 'b=3')
00719         for f in f1, f2:
00720             # f1/f2 takes exactly/at most 2 arguments
00721             self.assertEqualException(f, '2, 3, 4')
00722             self.assertEqualException(f, '1, 2, 3, a=1')
00723             self.assertEqualException(f, '2, 3, 4, c=5')
00724             self.assertEqualException(f, '2, 3, 4, a=1, c=5')
00725             # f got an unexpected keyword argument
00726             self.assertEqualException(f, 'c=2')
00727             self.assertEqualException(f, '2, c=3')
00728             self.assertEqualException(f, '2, 3, c=4')
00729             self.assertEqualException(f, '2, c=4, b=3')
00730             self.assertEqualException(f, '**{u"\u03c0\u03b9": 4}')
00731             # f got multiple values for keyword argument
00732             self.assertEqualException(f, '1, a=2')
00733             self.assertEqualException(f, '1, **{"a":2}')
00734             self.assertEqualException(f, '1, 2, b=3')
00735             # XXX: Python inconsistency
00736             # - for functions and bound methods: unexpected keyword 'c'
00737             # - for unbound methods: multiple values for keyword 'a'
00738             #self.assertEqualException(f, '1, c=3, a=2')
00739         # issue11256:
00740         f3 = self.makeCallable('**c')
00741         self.assertEqualException(f3, '1, 2')
00742         self.assertEqualException(f3, '1, 2, a=1, b=2')
00743         f4 = self.makeCallable('*, a, b=0')
00744         self.assertEqualException(f3, '1, 2')
00745         self.assertEqualException(f3, '1, 2, a=1, b=2')

Here is the call graph for this function:

Definition at line 662 of file test_inspect.py.

00662 
00663     def test_keyword_only(self):
00664         f = self.makeCallable('a=3, *, c, d=2')
00665         self.assertEqualCallArgs(f, 'c=3')
00666         self.assertEqualCallArgs(f, 'c=3, a=3')
00667         self.assertEqualCallArgs(f, 'a=2, c=4')
00668         self.assertEqualCallArgs(f, '4, c=4')
00669         self.assertEqualException(f, '')
00670         self.assertEqualException(f, '3')
00671         self.assertEqualException(f, 'a=3')
00672         self.assertEqualException(f, 'd=4')
00673 
00674         f = self.makeCallable('*, c, d=2')
00675         self.assertEqualCallArgs(f, 'c=3')
00676         self.assertEqualCallArgs(f, 'c=3, d=4')
00677         self.assertEqualCallArgs(f, 'd=4, c=3')

Here is the call graph for this function:

Definition at line 678 of file test_inspect.py.

00678 
00679     def test_multiple_features(self):
00680         f = self.makeCallable('a, b=2, *f, **g')
00681         self.assertEqualCallArgs(f, '2, 3, 7')
00682         self.assertEqualCallArgs(f, '2, 3, x=8')
00683         self.assertEqualCallArgs(f, '2, 3, x=8, *[(4,[5,6]), 7]')
00684         self.assertEqualCallArgs(f, '2, x=8, *[3, (4,[5,6]), 7], y=9')
00685         self.assertEqualCallArgs(f, 'x=8, *[2, 3, (4,[5,6])], y=9')
00686         self.assertEqualCallArgs(f, 'x=8, *collections.UserList('
00687                                  '[2, 3, (4,[5,6])]), **{"y":9, "z":10}')
00688         self.assertEqualCallArgs(f, '2, x=8, *collections.UserList([3, '
00689                                  '(4,[5,6])]), **collections.UserDict('
00690                                  'y=9, z=10)')
00691 
00692         f = self.makeCallable('a, b=2, *f, x, y=99, **g')
00693         self.assertEqualCallArgs(f, '2, 3, x=8')
00694         self.assertEqualCallArgs(f, '2, 3, x=8, *[(4,[5,6]), 7]')
00695         self.assertEqualCallArgs(f, '2, x=8, *[3, (4,[5,6]), 7], y=9, z=10')
00696         self.assertEqualCallArgs(f, 'x=8, *[2, 3, (4,[5,6])], y=9, z=10')
00697         self.assertEqualCallArgs(f, 'x=8, *collections.UserList('
00698                                  '[2, 3, (4,[5,6])]), q=0, **{"y":9, "z":10}')
00699         self.assertEqualCallArgs(f, '2, x=8, *collections.UserList([3, '
00700                                  '(4,[5,6])]), q=0, **collections.UserDict('
00701                                  'y=9, z=10)')

Here is the call graph for this function:

Definition at line 608 of file test_inspect.py.

00608 
00609     def test_plain(self):
00610         f = self.makeCallable('a, b=1')
00611         self.assertEqualCallArgs(f, '2')
00612         self.assertEqualCallArgs(f, '2, 3')
00613         self.assertEqualCallArgs(f, 'a=2')
00614         self.assertEqualCallArgs(f, 'b=3, a=2')
00615         self.assertEqualCallArgs(f, '2, b=3')
00616         # expand *iterable / **mapping
00617         self.assertEqualCallArgs(f, '*(2,)')
00618         self.assertEqualCallArgs(f, '*[2]')
00619         self.assertEqualCallArgs(f, '*(2, 3)')
00620         self.assertEqualCallArgs(f, '*[2, 3]')
00621         self.assertEqualCallArgs(f, '**{"a":2}')
00622         self.assertEqualCallArgs(f, 'b=3, **{"a":2}')
00623         self.assertEqualCallArgs(f, '2, **{"b":3}')
00624         self.assertEqualCallArgs(f, '**{"b":3, "a":2}')
00625         # expand UserList / UserDict
00626         self.assertEqualCallArgs(f, '*collections.UserList([2])')
00627         self.assertEqualCallArgs(f, '*collections.UserList([2, 3])')
00628         self.assertEqualCallArgs(f, '**collections.UserDict(a=2)')
00629         self.assertEqualCallArgs(f, '2, **collections.UserDict(b=3)')
00630         self.assertEqualCallArgs(f, 'b=2, **collections.UserDict(a=3)')

Here is the call graph for this function:

Definition at line 631 of file test_inspect.py.

00631 
00632     def test_varargs(self):
00633         f = self.makeCallable('a, b=1, *c')
00634         self.assertEqualCallArgs(f, '2')
00635         self.assertEqualCallArgs(f, '2, 3')
00636         self.assertEqualCallArgs(f, '2, 3, 4')
00637         self.assertEqualCallArgs(f, '*(2,3,4)')
00638         self.assertEqualCallArgs(f, '2, *[3,4]')
00639         self.assertEqualCallArgs(f, '2, 3, *collections.UserList([4])')

Here is the call graph for this function:

Definition at line 640 of file test_inspect.py.

00640 
00641     def test_varkw(self):
00642         f = self.makeCallable('a, b=1, **c')
00643         self.assertEqualCallArgs(f, 'a=2')
00644         self.assertEqualCallArgs(f, '2, b=3, c=4')
00645         self.assertEqualCallArgs(f, 'b=3, a=2, c=4')
00646         self.assertEqualCallArgs(f, 'c=4, **{"a":2, "b":3}')
00647         self.assertEqualCallArgs(f, '2, c=4, **{"b":3}')
00648         self.assertEqualCallArgs(f, 'b=2, **{"a":3, "c":4}')
00649         self.assertEqualCallArgs(f, '**collections.UserDict(a=2, b=3, c=4)')
00650         self.assertEqualCallArgs(f, '2, c=4, **collections.UserDict(b=3)')
00651         self.assertEqualCallArgs(f, 'b=2, **collections.UserDict(a=3, c=4)')

Here is the call graph for this function:

Definition at line 652 of file test_inspect.py.

00652 
00653     def test_varkw_only(self):
00654         # issue11256:
00655         f = self.makeCallable('**c')
00656         self.assertEqualCallArgs(f, '')
00657         self.assertEqualCallArgs(f, 'a=1')
00658         self.assertEqualCallArgs(f, 'a=1, b=2')
00659         self.assertEqualCallArgs(f, 'c=3, **{"a": 1, "b": 2}')
00660         self.assertEqualCallArgs(f, '**collections.UserDict(a=1, b=2)')
00661         self.assertEqualCallArgs(f, 'c=3, **collections.UserDict(a=1, b=2)')

Here is the call graph for this function:


The documentation for this class was generated from the following file: