Пример #1
0
    def test_copy_of(self):
        scope = activity.Scope(None)
        scope.mark_read(QN('foo'))

        self.assertTrue(QN('foo') in activity.Scope.copy_of(scope).used)

        child_scope = activity.Scope(scope)
        child_scope.mark_read(QN('bar'))

        self.assertTrue(QN('bar') in activity.Scope.copy_of(child_scope).used)
Пример #2
0
    def test_subscripts(self):
        a = QN('a')
        b = QN('b')
        a_sub_b = QN(a, subscript=b)
        self.assertEqual(a_sub_b.qn, (a, b))
        self.assertEqual(str(a_sub_b), 'a[b]')
        self.assertEqual(a_sub_b.ssf(), 'a_sub_b')
        self.assertEqual(a_sub_b.ast().value.id, 'a')
        self.assertEqual(a_sub_b.ast().slice.value.id, 'b')
        self.assertTrue(a_sub_b.is_composite())
        self.assertTrue(a_sub_b.has_subscript())
        self.assertEqual(a_sub_b.parent.qn, ('a', ))

        c = QN('c')
        b_sub_c = QN(b, subscript=c)
        a_sub_b_sub_c = QN(a, subscript=b_sub_c)
        self.assertEqual(a_sub_b_sub_c.qn, (a, b_sub_c))
        self.assertTrue(a_sub_b.is_composite())
        self.assertTrue(a_sub_b_sub_c.is_composite())
        self.assertTrue(a_sub_b.has_subscript())
        self.assertTrue(a_sub_b_sub_c.has_subscript())
        self.assertEqual(b_sub_c.qn, (b, c))
        self.assertEqual(str(a_sub_b_sub_c), 'a[b[c]]')
        self.assertEqual(a_sub_b_sub_c.ssf(), 'a_sub_b_sub_c')
        self.assertEqual(a_sub_b_sub_c.ast().value.id, 'a')
        self.assertEqual(a_sub_b_sub_c.ast().slice.value.value.id, 'b')
        self.assertEqual(a_sub_b_sub_c.ast().slice.value.slice.value.id, 'c')
        self.assertEqual(b_sub_c.ast().slice.value.id, 'c')
        self.assertEqual(a_sub_b_sub_c.parent.qn, ('a', ))
        with self.assertRaises(ValueError):
            QN('a', 'b')
Пример #3
0
    def test_basic(self):
        a = QN('a')
        self.assertEqual(a.qn, ('a', ))
        self.assertEqual(str(a), 'a')
        self.assertEqual(a.ssf(), 'a')
        self.assertEqual(a.ast().id, 'a')
        self.assertFalse(a.is_composite())
        with self.assertRaises(ValueError):
            _ = a.parent

        a_b = QN(a, attr='b')
        self.assertEqual(a_b.qn, (a, 'b'))
        self.assertEqual(str(a_b), 'a.b')
        self.assertEqual(a_b.ssf(), 'a_b')
        self.assertEqual(a_b.ast().value.id, 'a')
        self.assertEqual(a_b.ast().attr, 'b')
        self.assertTrue(a_b.is_composite())
        self.assertEqual(a_b.parent.qn, ('a', ))
Пример #4
0
 def visit_FunctionDef(self, node):
     if self.scope:
         qn = QN(node.name)
         self.scope.mark_write(qn)
     current_scope = self.scope
     fndef_scope = Scope(current_scope, isolated=True)
     self.scope = fndef_scope
     self.generic_visit(node)
     anno.setanno(node, NodeAnno.BODY_SCOPE, fndef_scope)
     self.scope = current_scope
     return node
Пример #5
0
    def test_referenced(self):
        scope = activity.Scope(None)
        scope.mark_read(QN('a'))

        child = activity.Scope(scope)
        child.mark_read(QN('b'))

        child2 = activity.Scope(child, isolated=False)
        child2.mark_read(QN('c'))

        self.assertTrue(QN('c') in child2.referenced)
        self.assertTrue(QN('b') in child2.referenced)
        self.assertFalse(QN('a') in child2.referenced)

        self.assertTrue(QN('c') in child.referenced)
        self.assertTrue(QN('b') in child.referenced)
        self.assertFalse(QN('a') in child.referenced)
Пример #6
0
    def test_literals(self):
        a = QN('a')
        a_sub_str_b = QN(a, subscript=QN(qual_names.StringLiteral('b')))
        a_sub_b = QN(a, subscript=QN('b'))

        self.assertNotEqual(a_sub_str_b, a_sub_b)
        self.assertNotEqual(hash(a_sub_str_b), hash(a_sub_b))

        a_sub_three = QN(a, subscript=QN(qual_names.NumberLiteral(3)))
        self.assertEqual(a_sub_three.ast().slice.value.n, 3)
Пример #7
0
    def test_equality(self):
        a = QN('a')
        a2 = QN('a')
        a_b = QN(a, attr='b')
        self.assertEqual(a2.qn, ('a', ))
        with self.assertRaises(ValueError):
            _ = a.parent

        a_b2 = QN(a, attr='b')
        self.assertEqual(a_b2.qn, (a, 'b'))
        self.assertEqual(a_b2.parent.qn, ('a', ))

        self.assertTrue(a2 == a)
        self.assertFalse(a2 is a)

        self.assertTrue(a_b.parent == a)
        self.assertTrue(a_b2.parent == a)

        self.assertTrue(a_b2 == a_b)
        self.assertFalse(a_b2 is a_b)
        self.assertFalse(a_b2 == a)
        a_sub_b = QN(a, subscript='b')
        a_sub_b2 = QN(a, subscript='b')
        self.assertTrue(a_sub_b == a_sub_b2)
        self.assertFalse(a_sub_b == a_b)
Пример #8
0
    def test_nesting(self):
        scope = activity.Scope(None)
        scope.mark_write(QN('foo'))
        scope.mark_read(QN('bar'))

        child = activity.Scope(scope)
        self.assertTrue(child.has(QN('foo')))
        self.assertTrue(scope.has(QN('foo')))

        child.mark_write(QN('bar'))
        self.assertTrue(child.has(QN('bar')))
        self.assertFalse(scope.has(QN('bar')))
Пример #9
0
    def test_basic(self):
        scope = activity.Scope(None)
        self.assertFalse(scope.has(QN('foo')))

        scope.mark_read(QN('foo'))
        self.assertFalse(scope.has(QN('foo')))

        scope.mark_write(QN('foo'))
        self.assertTrue(scope.has(QN('foo')))

        scope.mark_read(QN('bar'))
        self.assertFalse(scope.has(QN('bar')))
Пример #10
0
    def test_copy(self):
        scope = activity.Scope(None)
        scope.mark_write(QN('foo'))

        other = activity.Scope(None)
        other.copy_from(scope)

        self.assertTrue(QN('foo') in other.created)

        scope.mark_write(QN('bar'))
        scope.copy_from(other)

        self.assertFalse(QN('bar') in scope.created)

        scope.mark_write(QN('bar'))
        scope.merge_from(other)

        self.assertTrue(QN('bar') in scope.created)
        self.assertFalse(QN('bar') in other.created)
Пример #11
0
 def test_hashable(self):
     d = {QN('a'): 'a', QN('b'): 'b'}
     self.assertEqual(d[QN('a')], 'a')
     self.assertEqual(d[QN('b')], 'b')
     self.assertTrue(QN('c') not in d)
Пример #12
0
    def test_nested_attrs_subscripts(self):
        a = QN('a')
        b = QN('b')
        c = QN('c')
        b_sub_c = QN(b, subscript=c)
        a_sub_b_sub_c = QN(a, subscript=b_sub_c)

        b_dot_c = QN(b, attr='c')
        a_sub__b_dot_c = QN(a, subscript=b_dot_c)

        a_sub_b = QN(a, subscript=b)
        a_sub_b__dot_c = QN(a_sub_b, attr='c')

        a_dot_b = QN(a, attr='b')
        a_dot_b_sub_c = QN(a_dot_b, subscript=c)

        self.assertEqual(str(a_sub_b_sub_c), 'a[b[c]]')
        self.assertEqual(str(a_sub__b_dot_c), 'a[b.c]')
        self.assertEqual(str(a_sub_b__dot_c), 'a[b].c')
        self.assertEqual(str(a_dot_b_sub_c), 'a.b[c]')

        self.assertNotEqual(a_sub_b_sub_c, a_sub__b_dot_c)
        self.assertNotEqual(a_sub_b_sub_c, a_sub_b__dot_c)
        self.assertNotEqual(a_sub_b_sub_c, a_dot_b_sub_c)

        self.assertNotEqual(a_sub__b_dot_c, a_sub_b__dot_c)
        self.assertNotEqual(a_sub__b_dot_c, a_dot_b_sub_c)

        self.assertNotEqual(a_sub_b__dot_c, a_dot_b_sub_c)