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

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,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
2978 2982
2979 with self.assertRaisesRegex(ValueError, 'invalid value for'): 2983 with self.assertRaisesRegex(ValueError, 'invalid value for'):
2980 p2 = p2.replace(kind=p2.empty) 2984 p2 = p2.replace(kind=p2.empty)
2981 2985
2982 p2 = p2.replace(kind=p2.KEYWORD_ONLY) 2986 p2 = p2.replace(kind=p2.KEYWORD_ONLY)
2983 self.assertEqual(p2, p) 2987 self.assertEqual(p2, p)
2984 2988
2985 def test_signature_parameter_positional_only(self): 2989 def test_signature_parameter_positional_only(self):
2986 with self.assertRaisesRegex(TypeError, 'name must be a str'): 2990 with self.assertRaisesRegex(TypeError, 'name must be a str'):
2987 inspect.Parameter(None, kind=inspect.Parameter.POSITIONAL_ONLY) 2991 inspect.Parameter(None, kind=inspect.Parameter.POSITIONAL_ONLY)
2992
2993 @cpython_only
2994 def test_signature_parameter_implicit(self):
2995 with self.assertRaisesRegex(ValueError,
2996 'implicit arguments must be passed in as'):
2997 inspect.Parameter('.0', kind=inspect.Parameter.POSITIONAL_ONLY)
2998
2999 param = inspect.Parameter(
3000 '.0', kind=inspect.Parameter.POSITIONAL_OR_KEYWORD)
3001 self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_ONLY)
3002 self.assertEqual(param.name, 'implicit0')
2988 3003
2989 def test_signature_parameter_immutability(self): 3004 def test_signature_parameter_immutability(self):
2990 p = inspect.Parameter('spam', kind=inspect.Parameter.KEYWORD_ONLY) 3005 p = inspect.Parameter('spam', kind=inspect.Parameter.KEYWORD_ONLY)
2991 3006
2992 with self.assertRaises(AttributeError): 3007 with self.assertRaises(AttributeError):
2993 p.foo = 'bar' 3008 p.foo = 'bar'
2994 3009
2995 with self.assertRaises(AttributeError): 3010 with self.assertRaises(AttributeError):
2996 p.kind = 123 3011 p.kind = 123
2997 3012
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
3226 3241
3227 sig.bind(a=0, args=1) 3242 sig.bind(a=0, args=1)
3228 3243
3229 def test(*args, **kwargs): 3244 def test(*args, **kwargs):
3230 return args, kwargs 3245 return args, kwargs
3231 self.assertEqual(self.call(test, args=1), ((), {'args': 1})) 3246 self.assertEqual(self.call(test, args=1), ((), {'args': 1}))
3232 3247
3233 sig = inspect.signature(test) 3248 sig = inspect.signature(test)
3234 ba = sig.bind(args=1) 3249 ba = sig.bind(args=1)
3235 self.assertEqual(ba.arguments, {'kwargs': {'args': 1}}) 3250 self.assertEqual(ba.arguments, {'kwargs': {'args': 1}})
3251
3252 @cpython_only
3253 def test_signature_bind_implicit_arg(self):
3254 # Issue #19611: getcallargs should work with set comprehensions
3255 def make_set():
3256 return {z * z for z in range(5)}
3257 setcomp_code = make_set.__code__.co_consts[1]
3258 setcomp_func = types.FunctionType(setcomp_code, {})
3259
3260 iterator = iter(range(5))
3261 self.assertEqual(self.call(setcomp_func, iterator), {0, 1, 4, 9, 16})
3236 3262
3237 3263
3238 class TestBoundArguments(unittest.TestCase): 3264 class TestBoundArguments(unittest.TestCase):
3239 def test_signature_bound_arguments_unhashable(self): 3265 def test_signature_bound_arguments_unhashable(self):
3240 def foo(a): pass 3266 def foo(a): pass
3241 ba = inspect.signature(foo).bind(1) 3267 ba = inspect.signature(foo).bind(1)
3242 3268
3243 with self.assertRaisesRegex(TypeError, 'unhashable type'): 3269 with self.assertRaisesRegex(TypeError, 'unhashable type'):
3244 hash(ba) 3270 hash(ba)
3245 3271
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
3593 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState, 3619 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
3594 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject, 3620 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
3595 TestBoundArguments, TestSignaturePrivateHelpers, 3621 TestBoundArguments, TestSignaturePrivateHelpers,
3596 TestSignatureDefinitions, 3622 TestSignatureDefinitions,
3597 TestGetClosureVars, TestUnwrap, TestMain, TestReload, 3623 TestGetClosureVars, TestUnwrap, TestMain, TestReload,
3598 TestGetCoroutineState 3624 TestGetCoroutineState
3599 ) 3625 )
3600 3626
3601 if __name__ == "__main__": 3627 if __name__ == "__main__":
3602 test_main() 3628 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+