1- from typing import cast
1+ from typing import Type , TypeVar
22
33from pytype .pyc import opcodes
44from pytype .rewrite import abstract
77
88import unittest
99
10+ _T = TypeVar ('_T' )
11+
1012
1113def _make_vm (src : str ) -> vm_lib .VirtualMachine :
1214 return vm_lib .VirtualMachine (test_utils .parse (src ), {})
1315
1416
17+ def _get (typ : Type [_T ], var ) -> _T :
18+ v = var .get_atomic_value ()
19+ assert isinstance (v , typ )
20+ return v
21+
22+
1523class VmTest (unittest .TestCase ):
1624
1725 def test_run_module_frame (self ):
1826 block = [opcodes .LOAD_CONST (0 , 0 , 0 , None ), opcodes .RETURN_VALUE (0 , 0 )]
1927 code = test_utils .FakeOrderedCode ([block ], [None ])
2028 vm = vm_lib .VirtualMachine (code .Seal (), {})
21- module_frame = vm ._run ()
22- self .assertIsNotNone (module_frame )
29+ self .assertIsNone (vm ._module_frame )
30+ vm ._run_module ()
31+ self .assertIsNotNone (vm ._module_frame )
2332
2433 def test_globals (self ):
2534 vm = _make_vm ("""
@@ -33,18 +42,58 @@ def g():
3342 g()
3443 f()
3544 """ )
36- module_frame = vm ._run ()
45+ vm ._run_module ()
3746
3847 def get_const (var ):
39- return cast (abstract .PythonConstant , var . get_atomic_value () ).constant
48+ return _get (abstract .PythonConstant , var ).constant
4049
41- x = get_const (module_frame .load_global ('x' ))
42- y = get_const (module_frame .load_global ('y' ))
43- z = get_const (module_frame .load_global ('z' ))
50+ x = get_const (vm . _module_frame .load_global ('x' ))
51+ y = get_const (vm . _module_frame .load_global ('y' ))
52+ z = get_const (vm . _module_frame .load_global ('z' ))
4453 self .assertEqual (x , 42 )
4554 self .assertIsNone (y )
4655 self .assertEqual (z , 42 )
4756
57+ def test_analyze_functions (self ):
58+ # Just make sure this doesn't crash.
59+ vm = _make_vm ("""
60+ def f():
61+ def g():
62+ pass
63+ """ )
64+ vm .analyze_all_defs ()
65+
66+ def test_infer_stub (self ):
67+ # Just make sure this doesn't crash.
68+ vm = _make_vm ("""
69+ def f():
70+ def g():
71+ pass
72+ """ )
73+ vm .infer_stub ()
74+
75+ def test_run_function (self ):
76+ vm = _make_vm ("""
77+ x = None
78+
79+ def f():
80+ global x
81+ x = 42
82+
83+ def g():
84+ y = x
85+ """ )
86+ vm ._run_module ()
87+ f = _get (abstract .Function , vm ._module_frame .final_locals ['f' ])
88+ g = _get (abstract .Function , vm ._module_frame .final_locals ['g' ])
89+ f_frame = vm ._run_function (f )
90+ g_frame = vm ._run_function (g )
91+
92+ self .assertEqual (f_frame .load_global ('x' ).get_atomic_value (),
93+ abstract .PythonConstant (42 ))
94+ self .assertEqual (g_frame .load_local ('y' ).get_atomic_value (),
95+ abstract .PythonConstant (None ))
96+
4897
4998if __name__ == '__main__' :
5099 unittest .main ()
0 commit comments