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

Delta Between Two Patch Sets: Lib/test/test_inspect.py

Issue 19611: inspect.getcallargs doesn't properly interpret set comprehension code objects.
Left Patch Set: Created 3 years, 2 months ago
Right Patch Set: Created 3 years, 2 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/inspect.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 2905 matching lines...) Expand 10 before | Expand all | Expand 10 after
2916 kind=inspect.Parameter.VAR_POSITIONAL) 2916 kind=inspect.Parameter.VAR_POSITIONAL)
2917 2917
2918 p = inspect.Parameter('a', default=42, 2918 p = inspect.Parameter('a', default=42,
2919 kind=inspect.Parameter.POSITIONAL_OR_KEYWORD) 2919 kind=inspect.Parameter.POSITIONAL_OR_KEYWORD)
2920 with self.assertRaisesRegex(ValueError, 'cannot have default values'): 2920 with self.assertRaisesRegex(ValueError, 'cannot have default values'):
2921 p.replace(kind=inspect.Parameter.VAR_POSITIONAL) 2921 p.replace(kind=inspect.Parameter.VAR_POSITIONAL)
2922 2922
2923 self.assertTrue(repr(p).startswith('<Parameter')) 2923 self.assertTrue(repr(p).startswith('<Parameter'))
2924 self.assertTrue('"a=42"' in repr(p)) 2924 self.assertTrue('"a=42"' in repr(p))
2925 2925
2926 implicit_param = inspect.Parameter('.0', kind=inspect.Parameter.POSITION AL_ONLY)
2927 self.assertEqual(implicit_param.name, '.0')
2928
2929 def test_signature_parameter_hashable(self): 2926 def test_signature_parameter_hashable(self):
2930 P = inspect.Parameter 2927 P = inspect.Parameter
2931 foo = P('foo', kind=P.POSITIONAL_ONLY) 2928 foo = P('foo', kind=P.POSITIONAL_ONLY)
2932 self.assertEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY))) 2929 self.assertEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY)))
2933 self.assertNotEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY, 2930 self.assertNotEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY,
2934 default=42))) 2931 default=42)))
2935 self.assertNotEqual(hash(foo), 2932 self.assertNotEqual(hash(foo),
2936 hash(foo.replace(kind=P.VAR_POSITIONAL))) 2933 hash(foo.replace(kind=P.VAR_POSITIONAL)))
2937 2934
2938 def test_signature_parameter_equality(self): 2935 def test_signature_parameter_equality(self):
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
2985 2982
2986 with self.assertRaisesRegex(ValueError, 'invalid value for'): 2983 with self.assertRaisesRegex(ValueError, 'invalid value for'):
2987 p2 = p2.replace(kind=p2.empty) 2984 p2 = p2.replace(kind=p2.empty)
2988 2985
2989 p2 = p2.replace(kind=p2.KEYWORD_ONLY) 2986 p2 = p2.replace(kind=p2.KEYWORD_ONLY)
2990 self.assertEqual(p2, p) 2987 self.assertEqual(p2, p)
2991 2988
2992 def test_signature_parameter_positional_only(self): 2989 def test_signature_parameter_positional_only(self):
2993 with self.assertRaisesRegex(TypeError, 'name must be a str'): 2990 with self.assertRaisesRegex(TypeError, 'name must be a str'):
2994 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')
2995 3003
2996 def test_signature_parameter_immutability(self): 3004 def test_signature_parameter_immutability(self):
2997 p = inspect.Parameter('spam', kind=inspect.Parameter.KEYWORD_ONLY) 3005 p = inspect.Parameter('spam', kind=inspect.Parameter.KEYWORD_ONLY)
2998 3006
2999 with self.assertRaises(AttributeError): 3007 with self.assertRaises(AttributeError):
3000 p.foo = 'bar' 3008 p.foo = 'bar'
3001 3009
3002 with self.assertRaises(AttributeError): 3010 with self.assertRaises(AttributeError):
3003 p.kind = 123 3011 p.kind = 123
3004 3012
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
3234 sig.bind(a=0, args=1) 3242 sig.bind(a=0, args=1)
3235 3243
3236 def test(*args, **kwargs): 3244 def test(*args, **kwargs):
3237 return args, kwargs 3245 return args, kwargs
3238 self.assertEqual(self.call(test, args=1), ((), {'args': 1})) 3246 self.assertEqual(self.call(test, args=1), ((), {'args': 1}))
3239 3247
3240 sig = inspect.signature(test) 3248 sig = inspect.signature(test)
3241 ba = sig.bind(args=1) 3249 ba = sig.bind(args=1)
3242 self.assertEqual(ba.arguments, {'kwargs': {'args': 1}}) 3250 self.assertEqual(ba.arguments, {'kwargs': {'args': 1}})
3243 3251
3252 @cpython_only
3244 def test_signature_bind_implicit_arg(self): 3253 def test_signature_bind_implicit_arg(self):
3245 # Issue #19611: getcallargs should work with set comprehensions 3254 # Issue #19611: getcallargs should work with set comprehensions
3246 def make_set(): 3255 def make_set():
3247 return {z * z for z in range(5)} 3256 return {z * z for z in range(5)}
3248 setcomp_code = make_set.__code__.co_consts[1] 3257 setcomp_code = make_set.__code__.co_consts[1]
3249 setcomp_func = types.FunctionType(setcomp_code, {}) 3258 setcomp_func = types.FunctionType(setcomp_code, {})
3250 3259
3251 iterator = iter(range(5)) 3260 iterator = iter(range(5))
3252 self.assertEqual(self.call(setcomp_func, iterator), ((iterator,), {})) 3261 self.assertEqual(self.call(setcomp_func, iterator), {0, 1, 4, 9, 16})
3253 3262
3254 3263
3255 class TestBoundArguments(unittest.TestCase): 3264 class TestBoundArguments(unittest.TestCase):
3256 def test_signature_bound_arguments_unhashable(self): 3265 def test_signature_bound_arguments_unhashable(self):
3257 def foo(a): pass 3266 def foo(a): pass
3258 ba = inspect.signature(foo).bind(1) 3267 ba = inspect.signature(foo).bind(1)
3259 3268
3260 with self.assertRaisesRegex(TypeError, 'unhashable type'): 3269 with self.assertRaisesRegex(TypeError, 'unhashable type'):
3261 hash(ba) 3270 hash(ba)
3262 3271
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
3610 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState, 3619 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
3611 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject, 3620 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
3612 TestBoundArguments, TestSignaturePrivateHelpers, 3621 TestBoundArguments, TestSignaturePrivateHelpers,
3613 TestSignatureDefinitions, 3622 TestSignatureDefinitions,
3614 TestGetClosureVars, TestUnwrap, TestMain, TestReload, 3623 TestGetClosureVars, TestUnwrap, TestMain, TestReload,
3615 TestGetCoroutineState 3624 TestGetCoroutineState
3616 ) 3625 )
3617 3626
3618 if __name__ == "__main__": 3627 if __name__ == "__main__":
3619 test_main() 3628 test_main()
LEFTRIGHT

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