Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(40555)

Side by Side Diff: Lib/test/test_inspect.py

Issue 19611: inspect.getcallargs doesn't properly interpret set comprehension code objects.
Patch Set: Created 3 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/inspect.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import builtins 1 import builtins
2 import collections 2 import collections
3 import datetime 3 import datetime
4 import functools 4 import functools
5 import importlib 5 import importlib
6 import inspect 6 import inspect
7 import io 7 import io
8 import linecache 8 import linecache
9 import os 9 import os
10 from os.path import normcase 10 from os.path import normcase
(...skipping 2884 matching lines...) Expand 10 before | Expand all | Expand 10 after
2895 2895
2896 with self.assertRaisesRegex(ValueError, 'not a valid parameter name'): 2896 with self.assertRaisesRegex(ValueError, 'not a valid parameter name'):
2897 inspect.Parameter('1', kind=inspect.Parameter.VAR_KEYWORD) 2897 inspect.Parameter('1', kind=inspect.Parameter.VAR_KEYWORD)
2898 2898
2899 with self.assertRaisesRegex(TypeError, 'name must be a str'): 2899 with self.assertRaisesRegex(TypeError, 'name must be a str'):
2900 inspect.Parameter(None, kind=inspect.Parameter.VAR_KEYWORD) 2900 inspect.Parameter(None, kind=inspect.Parameter.VAR_KEYWORD)
2901 2901
2902 with self.assertRaisesRegex(ValueError, 2902 with self.assertRaisesRegex(ValueError,
2903 'is not a valid parameter name'): 2903 'is not a valid parameter name'):
2904 inspect.Parameter('$', kind=inspect.Parameter.VAR_KEYWORD) 2904 inspect.Parameter('$', kind=inspect.Parameter.VAR_KEYWORD)
2905
2906 with self.assertRaisesRegex(ValueError,
2907 'is not a valid parameter name'):
2908 inspect.Parameter('.a', kind=inspect.Parameter.VAR_KEYWORD)
2905 2909
2906 with self.assertRaisesRegex(ValueError, 'cannot have default values'): 2910 with self.assertRaisesRegex(ValueError, 'cannot have default values'):
2907 inspect.Parameter('a', default=42, 2911 inspect.Parameter('a', default=42,
2908 kind=inspect.Parameter.VAR_KEYWORD) 2912 kind=inspect.Parameter.VAR_KEYWORD)
2909 2913
2910 with self.assertRaisesRegex(ValueError, 'cannot have default values'): 2914 with self.assertRaisesRegex(ValueError, 'cannot have default values'):
2911 inspect.Parameter('a', default=42, 2915 inspect.Parameter('a', default=42,
2912 kind=inspect.Parameter.VAR_POSITIONAL) 2916 kind=inspect.Parameter.VAR_POSITIONAL)
2913 2917
2914 p = inspect.Parameter('a', default=42, 2918 p = inspect.Parameter('a', default=42,
2915 kind=inspect.Parameter.POSITIONAL_OR_KEYWORD) 2919 kind=inspect.Parameter.POSITIONAL_OR_KEYWORD)
2916 with self.assertRaisesRegex(ValueError, 'cannot have default values'): 2920 with self.assertRaisesRegex(ValueError, 'cannot have default values'):
2917 p.replace(kind=inspect.Parameter.VAR_POSITIONAL) 2921 p.replace(kind=inspect.Parameter.VAR_POSITIONAL)
2918 2922
2919 self.assertTrue(repr(p).startswith('<Parameter')) 2923 self.assertTrue(repr(p).startswith('<Parameter'))
2920 self.assertTrue('"a=42"' in repr(p)) 2924 self.assertTrue('"a=42"' in repr(p))
2925
2926 implicit_param = inspect.Parameter('.0', kind=inspect.Parameter.POSITION AL_ONLY)
2927 self.assertEqual(implicit_param.name, '.0')
2921 2928
2922 def test_signature_parameter_hashable(self): 2929 def test_signature_parameter_hashable(self):
2923 P = inspect.Parameter 2930 P = inspect.Parameter
2924 foo = P('foo', kind=P.POSITIONAL_ONLY) 2931 foo = P('foo', kind=P.POSITIONAL_ONLY)
2925 self.assertEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY))) 2932 self.assertEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY)))
2926 self.assertNotEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY, 2933 self.assertNotEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY,
2927 default=42))) 2934 default=42)))
2928 self.assertNotEqual(hash(foo), 2935 self.assertNotEqual(hash(foo),
2929 hash(foo.replace(kind=P.VAR_POSITIONAL))) 2936 hash(foo.replace(kind=P.VAR_POSITIONAL)))
2930 2937
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
3226 3233
3227 sig.bind(a=0, args=1) 3234 sig.bind(a=0, args=1)
3228 3235
3229 def test(*args, **kwargs): 3236 def test(*args, **kwargs):
3230 return args, kwargs 3237 return args, kwargs
3231 self.assertEqual(self.call(test, args=1), ((), {'args': 1})) 3238 self.assertEqual(self.call(test, args=1), ((), {'args': 1}))
3232 3239
3233 sig = inspect.signature(test) 3240 sig = inspect.signature(test)
3234 ba = sig.bind(args=1) 3241 ba = sig.bind(args=1)
3235 self.assertEqual(ba.arguments, {'kwargs': {'args': 1}}) 3242 self.assertEqual(ba.arguments, {'kwargs': {'args': 1}})
3243
3244 def test_signature_bind_implicit_arg(self):
3245 # Issue #19611: getcallargs should work with set comprehensions
3246 def make_set():
3247 return {z * z for z in range(5)}
3248 setcomp_code = make_set.__code__.co_consts[1]
3249 setcomp_func = types.FunctionType(setcomp_code, {})
3250
3251 iterator = iter(range(5))
3252 self.assertEqual(self.call(setcomp_func, iterator), ((iterator,), {}))
3236 3253
3237 3254
3238 class TestBoundArguments(unittest.TestCase): 3255 class TestBoundArguments(unittest.TestCase):
3239 def test_signature_bound_arguments_unhashable(self): 3256 def test_signature_bound_arguments_unhashable(self):
3240 def foo(a): pass 3257 def foo(a): pass
3241 ba = inspect.signature(foo).bind(1) 3258 ba = inspect.signature(foo).bind(1)
3242 3259
3243 with self.assertRaisesRegex(TypeError, 'unhashable type'): 3260 with self.assertRaisesRegex(TypeError, 'unhashable type'):
3244 hash(ba) 3261 hash(ba)
3245 3262
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
3593 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState, 3610 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
3594 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject, 3611 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
3595 TestBoundArguments, TestSignaturePrivateHelpers, 3612 TestBoundArguments, TestSignaturePrivateHelpers,
3596 TestSignatureDefinitions, 3613 TestSignatureDefinitions,
3597 TestGetClosureVars, TestUnwrap, TestMain, TestReload, 3614 TestGetClosureVars, TestUnwrap, TestMain, TestReload,
3598 TestGetCoroutineState 3615 TestGetCoroutineState
3599 ) 3616 )
3600 3617
3601 if __name__ == "__main__": 3618 if __name__ == "__main__":
3602 test_main() 3619 test_main()
OLDNEW
« no previous file with comments | « Lib/inspect.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+