Exemple #1
0
 def test_diamond(self):
   # Disassembled from:
   # | if []:
   # |   y = 1
   # | elif []:
   # |   y = 2
   # | elif []:
   # |   y = None
   # | return y
   code = self.make_code([
       0x67, 0, 0,   #  0 BUILD_LIST, arg=0,
       0x72, 15, 0,  #  3 POP_JUMP_IF_FALSE, dest=15,
       0x64, 1, 0,   #  6 LOAD_CONST, arg=1 (1),
       0x7d, 1, 0,   #  9 STORE_FAST, arg=1 "y",
       0x6e, 30, 0,  # 12 JUMP_FORWARD, dest=45,
       0x67, 0, 0,   # 15 BUILD_LIST, arg=0,
       0x72, 30, 0,  # 18 POP_JUMP_IF_FALSE, dest=30,
       0x64, 2, 0,   # 21 LOAD_CONST, arg=2 (2),
       0x7d, 1, 0,   # 24 STORE_FAST, arg=1 "y",
       0x6e, 15, 0,  # 27 JUMP_FORWARD, dest=45,
       0x67, 0, 0,   # 30 BUILD_LIST, arg=0,
       0x72, 45, 0,  # 33 POP_JUMP_IF_FALSE, dest=45,
       0x64, 0, 0,   # 36 LOAD_CONST, arg=0 (None),
       0x7d, 1, 0,   # 39 STORE_FAST, arg=1 "y",
       0x6e, 0, 0,   # 42 JUMP_FORWARD, dest=45,
       0x7c, 1, 0,   # 45 LOAD_FAST, arg=1,
       0x53,         # 48 RETURN_VALUE
   ])
   code = blocks.process_code(code, {})
   v = vm.VirtualMachine(self.errorlog, self.options, loader=self.loader)
   v.run_bytecode(v.program.NewCFGNode(), code)
Exemple #2
0
 def test_diamond(self):
   program = cfg.Program()
   # Disassembled from:
   # | if []:
   # |   y = 1
   # | elif []:
   # |   y = 2
   # | elif []:
   # |   y = None
   # | return y
   code = self.make_code([
       0x67, 0, 0,   #  0 BUILD_LIST, arg=0,
       0x72, 15, 0,  #  3 POP_JUMP_IF_FALSE, dest=15,
       0x64, 1, 0,   #  6 LOAD_CONST, arg=1 (1),
       0x7d, 1, 0,   #  9 STORE_FAST, arg=1 "y",
       0x6e, 30, 0,  # 12 JUMP_FORWARD, dest=45,
       0x67, 0, 0,   # 15 BUILD_LIST, arg=0,
       0x72, 30, 0,  # 18 POP_JUMP_IF_FALSE, dest=30,
       0x64, 2, 0,   # 21 LOAD_CONST, arg=2 (2),
       0x7d, 1, 0,   # 24 STORE_FAST, arg=1 "y",
       0x6e, 15, 0,  # 27 JUMP_FORWARD, dest=45,
       0x67, 0, 0,   # 30 BUILD_LIST, arg=0,
       0x72, 45, 0,  # 33 POP_JUMP_IF_FALSE, dest=45,
       0x64, 0, 0,   # 36 LOAD_CONST, arg=0 (None),
       0x7d, 1, 0,   # 39 STORE_FAST, arg=1 "y",
       0x6e, 0, 0,   # 42 JUMP_FORWARD, dest=45,
       0x7c, 1, 0,   # 45 LOAD_FAST, arg=1,
       0x53,         # 48 RETURN_VALUE
   ])
   code = blocks.process_code(code)
   v = vm.VirtualMachine(self.PYTHON_VERSION, errors.ErrorLog())
   v.run_bytecode(program.NewCFGNode(), code)
Exemple #3
0
 def test_apply_typecomments(self):
     # Disassembly + type comment map from
     #   a = 1; b = 2  # type: float
     # The type comment should only apply to b.
     co = self.make_code([
         0x64,
         0,
         0,  # [0]  0 LOAD_CONST, arg=0 (1)
         0x5a,
         0,
         0,  # [1]  3 STORE_NAME, arg=0 (a)
         0x64,
         1,
         0,  # [2]  6 LOAD_CONST, arg=1 (2)
         0x5a,
         1,
         0,  # [3]  9 STORE_NAME, arg=1 (b)
         0x64,
         2,
         0,  # [4] 12 LOAD_CONST, arg=2 (None)
         0x53  # [5] 15 RETURN_VALUE
     ])
     ordered_code = blocks.process_code(co, {1: ("a = 1; b = 2", "float")})
     bytecode = ordered_code.order[0].code
     self.assertEqual(bytecode[1].type_comment, None)
     self.assertEqual(bytecode[3].type_comment, "float")
 def _compile(self, src, mode="exec"):
   exe = (["python" + ".".join(map(str, self.python_version))], [])
   pyc_data = pyc.compile_src_string_to_pyc_string(
       src, filename="test_input.py", python_version=self.python_version,
       python_exe=exe, mode=mode)
   code = pyc.parse_pyc_string(pyc_data)
   code = blocks.process_code(code, self.python_version)
   return code
Exemple #5
0
 def test_simple(self):
   # Disassembled from:
   # | return None
   code = self.make_code([
       0x64, 1, 0,  # 0 LOAD_CONST, arg=1 (1)
       0x53,  # 3 RETURN_VALUE
   ], name="simple")
   code = blocks.process_code(code, {})
   v = vm.VirtualMachine(self.errorlog, self.options, loader=self.loader)
   v.run_bytecode(v.program.NewCFGNode(), code)
Exemple #6
0
 def test_simple(self):
   program = cfg.Program()
   # Disassembled from:
   # | return None
   code = self.make_code([
       0x64, 1, 0,  # 0 LOAD_CONST, arg=1 (1)
       0x53,  # 3 RETURN_VALUE
   ], name="simple")
   code = blocks.process_code(code)
   v = vm.VirtualMachine(self.PYTHON_VERSION, errors.ErrorLog())
   v.run_bytecode(program.NewCFGNode(), code)
Exemple #7
0
 def _create(self, src, disable=()):
     self.num_lines = len(src.rstrip().splitlines())
     src = textwrap.dedent(src)
     src_tree = directors.parse_src(src, self.python_version)
     self._errorlog = errors.ErrorLog()
     if self.python_version < (3, 8):
         raw_code = pyc.compile_src(src, _TEST_FILENAME,
                                    self.python_version, None)
         code = blocks.process_code(raw_code, self.python_version)
     else:
         code = None
     self._director = directors.Director(src_tree, self._errorlog,
                                         _TEST_FILENAME, disable, code)
Exemple #8
0
 def test_apply_typecomments(self):
     # Disassembly + type comment map from
     #   a = 1; b = 2  # type: float
     # The type comment should only apply to b.
     o = test_utils.Py2Opcodes
     co = self.make_code([
         o.LOAD_CONST, 0, 0, o.STORE_NAME, 0, 0, o.LOAD_CONST, 1, 0,
         o.STORE_NAME, 1, 0, o.LOAD_CONST, 2, 0, o.RETURN_VALUE
     ])
     ordered_code = blocks.process_code(co, {1: ("a = 1; b = 2", "float")},
                                        [])
     bytecode = ordered_code.order[0].code
     self.assertEqual(bytecode[1].type_comment, None)
     self.assertEqual(bytecode[3].type_comment, "float")
Exemple #9
0
 def test_apply_typecomments(self):
     # Disassembly + type comment map from
     #   a = 1; b = 2  # type: float
     # The type comment should only apply to b.
     o = test_utils.Py2Opcodes
     co = self.make_code([
         o.LOAD_CONST, 0, 0, o.STORE_NAME, 0, 0, o.LOAD_CONST, 1, 0,
         o.STORE_NAME, 1, 0, o.LOAD_CONST, 2, 0, o.RETURN_VALUE
     ])
     ordered_code = blocks.merge_annotations(
         blocks.process_code(co, self.python_version), {1: "float"}, [])
     bytecode = ordered_code.order[0].code
     self.assertIsNone(bytecode[1].annotation)
     self.assertEqual(bytecode[3].annotation, "float")
Exemple #10
0
 def test_simple(self):
     # Disassembled from:
     # | return None
     code = self.make_code(
         [
             0x64,
             1,  # 0 LOAD_CONST, arg=1 (1)
             0x53,
             0,  # 3 RETURN_VALUE (0)
         ],
         name="simple")
     code = blocks.process_code(code, self.python_version)
     ctx = context.Context(self.errorlog, self.options, loader=self.loader)
     ctx.vm = vm.VirtualMachine(ctx)
     ctx.vm.run_bytecode(ctx.program.NewCFGNode(), code)
Exemple #11
0
 def test_simple(self):
     program = cfg.Program()
     # Disassembled from:
     # | return None
     code = self.make_code(
         [
             0x64,
             1,
             0,  # 0 LOAD_CONST, arg=1 (1)
             0x53,  # 3 RETURN_VALUE
         ],
         name="simple")
     code = blocks.process_code(code)
     v = vm.VirtualMachine(self.PYTHON_VERSION, errors.ErrorLog())
     v.run_bytecode(program.NewCFGNode(), code)
Exemple #12
0
 def test_diamond(self):
     # Disassembled from:
     # | if []:
     # |   y = 1
     # | elif []:
     # |   y = 2
     # | elif []:
     # |   y = None
     # | return y
     o = test_utils.Py37Opcodes
     code = self.make_code([
         o.BUILD_LIST,
         0,
         o.POP_JUMP_IF_FALSE,
         10,
         o.LOAD_CONST,
         1,
         o.STORE_FAST,
         0,
         o.JUMP_FORWARD,
         18,
         o.BUILD_LIST,
         0,
         o.POP_JUMP_IF_FALSE,
         20,
         o.LOAD_CONST,
         2,
         o.STORE_FAST,
         0,
         o.JUMP_FORWARD,
         8,
         o.BUILD_LIST,
         0,
         o.POP_JUMP_IF_FALSE,
         28,
         o.LOAD_CONST,
         0,
         o.STORE_FAST,
         0,
         o.LOAD_FAST,
         0,
         o.RETURN_VALUE,
         0,
     ])
     code = blocks.process_code(code, self.python_version)
     ctx = context.Context(self.errorlog, self.options, loader=self.loader)
     ctx.vm = vm.VirtualMachine(ctx)
     ctx.vm.run_bytecode(ctx.program.NewCFGNode(), code)
Exemple #13
0
 def test_diamond(self):
     # Disassembled from:
     # | if []:
     # |   y = 1
     # | elif []:
     # |   y = 2
     # | elif []:
     # |   y = None
     # | return y
     o = test_utils.Py2Opcodes
     code = self.make_code([
         o.BUILD_LIST,
         0,
         0,
         o.POP_JUMP_IF_FALSE,
         15,
         0,
         o.LOAD_CONST,
         1,
         0,
         o.STORE_FAST,
         1,
         0,
         o.JUMP_FORWARD,
         30,
         0,
         o.BUILD_LIST,
         0,
         0,
         o.POP_JUMP_IF_FALSE,
         30,
         0,
         o.LOAD_CONST,
         2,
         0,
         o.STORE_FAST,
         1,
         0,
         o.JUMP_FORWARD,
         15,
         0,
         o.BUILD_LIST,
         0,
         0,
         o.POP_JUMP_IF_FALSE,
         45,
         0,
         o.LOAD_CONST,
         0,
         0,
         o.STORE_FAST,
         1,
         0,
         o.JUMP_FORWARD,
         0,
         0,
         o.LOAD_FAST,
         1,
         0,
         o.RETURN_VALUE,
     ])
     code = blocks.process_code(code)
     v = vm.VirtualMachine(self.errorlog, self.options, loader=self.loader)
     v.run_bytecode(v.program.NewCFGNode(), code)