def _visit_and_reindent(self, nodes):
   new_nodes = []
   current_dest = new_nodes
   alias_map = {}
   reindent_requested = False
   for n in nodes:
     n = self.visit(n)
     # NOTE: the order in which these statements execute is important; in
     # particular, watch out for ending up with cycles in the AST.
     if alias_map:
       n = ast_util.rename_symbols(n, alias_map)
     if isinstance(n, (list, tuple)):
       current_dest.extend(n)
     else:
       current_dest.append(n)
     if anno.hasanno(n, anno.Basic.INDENT_BLOCK_REMAINDER):
       reindent_requested = True
       new_dest, new_alias_map = anno.getanno(
           n, anno.Basic.INDENT_BLOCK_REMAINDER)
       anno.delanno(n, anno.Basic.INDENT_BLOCK_REMAINDER)
       new_alias_map.update(alias_map)
       alias_map = new_alias_map
       current_dest = new_dest
   if reindent_requested and not current_dest:
     # TODO(mdan): There may still be something that could be done.
     raise ValueError('Unable to insert statement into the computation flow: '
                      'it is not followed by any computation which '
                      'the statement could gate.')
   return new_nodes
Exemplo n.º 2
0
 def _visit_and_reindent(self, nodes):
     new_nodes = []
     current_dest = new_nodes
     alias_map = {}
     reindent_requested = False
     for n in nodes:
         n = self.visit(n)
         # NOTE: the order in which these statements execute is important; in
         # particular, watch out for ending up with cycles in the AST.
         if alias_map:
             n = ast_util.rename_symbols(n, alias_map)
         if isinstance(n, (list, tuple)):
             current_dest.extend(n)
         else:
             current_dest.append(n)
         if anno.hasanno(n, anno.Basic.INDENT_BLOCK_REMAINDER):
             reindent_requested = True
             new_dest, new_alias_map = anno.getanno(
                 n, anno.Basic.INDENT_BLOCK_REMAINDER)
             anno.delanno(n, anno.Basic.INDENT_BLOCK_REMAINDER)
             new_alias_map.update(alias_map)
             alias_map = new_alias_map
             current_dest = new_dest
     if reindent_requested and not current_dest:
         # TODO(mdan): There may still be something that could be done.
         raise ValueError(
             'Unable to insert statement into the computation flow: '
             'it is not followed by any computation which '
             'the statement could gate.')
     return new_nodes
Exemplo n.º 3
0
    def test_no_origin_annotation(self):
        def test_fn(x):
            return x + 1

        node, ctx = self.prepare(test_fn, {})
        anno.delanno(node, anno.Basic.ORIGIN)
        node = error_handlers.transform(node, ctx)
        self.assertIsInstance(node.body[0], gast.Return)
Exemplo n.º 4
0
  def test_basic(self):
    node = ast.Name()

    self.assertEqual(anno.keys(node), set())
    self.assertFalse(anno.hasanno(node, 'foo'))
    with self.assertRaises(AttributeError):
      anno.getanno(node, 'foo')

    anno.setanno(node, 'foo', 3)

    self.assertEqual(anno.keys(node), {'foo'})
    self.assertTrue(anno.hasanno(node, 'foo'))
    self.assertEqual(anno.getanno(node, 'foo'), 3)
    self.assertEqual(anno.getanno(node, 'bar', default=7), 7)

    anno.delanno(node, 'foo')

    self.assertEqual(anno.keys(node), set())
    self.assertFalse(anno.hasanno(node, 'foo'))
    with self.assertRaises(AttributeError):
      anno.getanno(node, 'foo')
    self.assertIsNone(anno.getanno(node, 'foo', default=None))