示例#1
0
 def test_noImportStar(self):
     """
     Code containing 'import *' is rejected.
     """
     tree = ast.parse("from pickle import *")
     f = _ImportExportFinder()
     self.assertRaises(SyntaxError, f.visit, tree)
示例#2
0
 def test_noImportStar(self):
     """
     Code containing 'import *' is rejected.
     """
     tree = ast.parse("from pickle import *")
     f = _ImportExportFinder()
     self.assertRaises(SyntaxError, f.visit, tree)
示例#3
0
    def test_literalStringsAll(self):
        """
        Modules with a definition of __all__ that isn't a list of
        literal strings are rejected by L{_ImportExportFinder}.
        """

        bits = ["__all__ = ['a' + 'b']", "__all__ = ['a', 1, 'b']", "x, __all__ = ['x', ['a', 1]]"]
        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            self.assertRaises(SyntaxError, f.visit, tree)
示例#4
0
    def test_justOneAll(self):
        """
        Modules with more than one definition of __all__ are rejected
        by L{_ImportExportFinder}.
        """

        bits = ["__all__ = ['a']\n__all__ = ['a', 'b']", "__all__ = ['a']\n__all__, x = ['a', 'b'], 1"]
        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            self.assertRaises(SyntaxError, f.visit, tree)
示例#5
0
    def test_identifierStringsAll(self):
        """
        Modules with a definition of __all__ that contains strings
        that aren't valid Python identifiers are rejected by
        L{_ImportExportFinder}.
        """
        bits = ["__all__ = ['a nice variable', 'etc']", "__all__ = ['call-with-current-continuation', 'goto']"]

        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            self.assertRaises(SyntaxError, f.visit, tree)
示例#6
0
    def test_all(self):
        """
        If a module's '__all__' attribute contains a sequence of
        literal strings, they are collected as export names.
        """

        bits = ["foo = 1\n__all__ = ['foo', 'baz']", "__all__ = ('foo', 'baz')\nbaz = 1"]

        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            f.visit(tree)
            self.assertEqual(f.exports, set(["foo", "baz"]))
示例#7
0
    def test_multiAssigns(self):
        """
        Assignments to __all__ in a tuple unpacking statement are recognized properly.
        """
        bits = ['__all__, x = (["foo", "baz"], 1)',
                'foo = 1\nx, __all__ = (None, ["foo", "baz"])',
                'x, __all__, y = [[], ["foo", "baz"], {}]']

        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            f.visit(tree)
            self.assertEqual(f.exports, set(["foo", "baz"]))
示例#8
0
    def test_bogusMultiAssign(self):
        """
        Tuple-unpacking assigments containing __all__ are rejected if
        the RHS is not a literal sequence of the proper length.
        """
        bits = [
            "(x, __all__) = 1", "[x, __all__] = []", "__all__, x = (1,)",
            "(x, (y, __all__)) = 1, 2", "(x, (y, __all__)) = (1, (2,))"
        ]

        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            self.assertRaises(SyntaxError, f.visit, tree)
示例#9
0
    def test_literalStringsAll(self):
        """
        Modules with a definition of __all__ that isn't a list of
        literal strings are rejected by L{_ImportExportFinder}.
        """

        bits = [
            "__all__ = ['a' + 'b']", "__all__ = ['a', 1, 'b']",
            "x, __all__ = ['x', ['a', 1]]"
        ]
        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            self.assertRaises(SyntaxError, f.visit, tree)
示例#10
0
    def test_justOneAll(self):
        """
        Modules with more than one definition of __all__ are rejected
        by L{_ImportExportFinder}.
        """

        bits = [
            "__all__ = ['a']\n__all__ = ['a', 'b']",
            "__all__ = ['a']\n__all__, x = ['a', 'b'], 1",
        ]
        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            self.assertRaises(SyntaxError, f.visit, tree)
示例#11
0
    def test_bogusMultiAssign(self):
        """
        Tuple-unpacking assigments containing __all__ are rejected if
        the RHS is not a literal sequence of the proper length.
        """
        bits = ["(x, __all__) = 1",
                "[x, __all__] = []",
                "__all__, x = (1,)",
                "(x, (y, __all__)) = 1, 2",
                "(x, (y, __all__)) = (1, (2,))"]

        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            self.assertRaises(SyntaxError, f.visit, tree)
示例#12
0
    def test_multiAssigns(self):
        """
        Assignments to __all__ in a tuple unpacking statement are recognized properly.
        """
        bits = [
            '__all__, x = (["foo", "baz"], 1)',
            'foo = 1\nx, __all__ = (None, ["foo", "baz"])',
            'x, __all__, y = [[], ["foo", "baz"], {}]'
        ]

        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            f.visit(tree)
            self.assertEqual(f.exports, set(["foo", "baz"]))
示例#13
0
    def test_identifierStringsAll(self):
        """
        Modules with a definition of __all__ that contains strings
        that aren't valid Python identifiers are rejected by
        L{_ImportExportFinder}.
        """
        bits = [
            "__all__ = ['a nice variable', 'etc']",
            "__all__ = ['call-with-current-continuation', 'goto']"
        ]

        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            self.assertRaises(SyntaxError, f.visit, tree)
示例#14
0
    def test_all(self):
        """
        If a module's '__all__' attribute contains a sequence of
        literal strings, they are collected as export names.
        """

        bits = [
            "foo = 1\n__all__ = ['foo', 'baz']",
            "__all__ = ('foo', 'baz')\nbaz = 1"
        ]

        for code in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            f.visit(tree)
            self.assertEqual(f.exports, set(["foo", "baz"]))
示例#15
0
    def test_funnyAssigns(self):
        """
        Assignments that aren't to regular names are
        skipped. Assignments to regular names (whether in unpacking
        contexts or not) are recognized.
        """
        bits = [('x[0] = 1', []), ('x.y = 2', []), ('x, y[0] = 3, 4', ["x"]),
                ('x.y, z = 5, 6', ["z"]), ("a, b = 7, 8", ["a", "b"]),
                ("[c, d] = 9, 0", ["c", "d"]),
                ("[e, (f, g)] = 0xA, 0xB", ["e", "f", "g"])]

        for (code, expectedNames) in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            f.visit(tree)
            self.assertEqual(
                f.definedNames, set(expectedNames),
                "expected %s as defined names in %r" % (expectedNames, code))
示例#16
0
    def test_funnyAssigns(self):
        """
        Assignments that aren't to regular names are
        skipped. Assignments to regular names (whether in unpacking
        contexts or not) are recognized.
        """
        bits = [('x[0] = 1', []),
                ('x.y = 2', []),
                ('x, y[0] = 3, 4', ["x"]),
                ('x.y, z = 5, 6', ["z"]),
                ("a, b = 7, 8", ["a", "b"]),
                ("[c, d] = 9, 0", ["c", "d"]),
                ("[e, (f, g)] = 0xA, 0xB", ["e", "f", "g"])]

        for (code, expectedNames) in bits:
            tree = ast.parse(code)
            f = _ImportExportFinder()
            f.visit(tree)
            self.assertEqual(f.definedNames, set(expectedNames), "expected %s as defined names in %r" % (expectedNames, code))