Example #1
0
    def test_tempnam_warning(self):
        with stderr_trapper() as trapper:
            temp = nt.tempnam()

        self.assertTrue(
            trapper.messages[0].endswith(
                "RuntimeWarning: tempnam is a potential security risk to your program"
            ), trapper.messages)
Example #2
0
    def test_syntax_warnings(self):
        # syntax error warnings are outputted using warnings.showwarning.  our own warning trapper therefore
        # doesn't see them.  So we trap stderr here instead.  We could use CPython's warning trapper if we
        # checked for the presence of the stdlib.
        with stderr_trapper() as trapper:
            compile("def f():\n    a = 1\n    global a\n", "", "exec")
        self.assertEqual(trapper.messages, [":3: SyntaxWarning: name 'a' is assigned to before global declaration"])

        with stderr_trapper() as trapper:
            compile("def f():\n    def a(): pass\n    global a\n", "", "exec")
        self.assertEqual(trapper.messages, [":3: SyntaxWarning: name 'a' is assigned to before global declaration"])

        with stderr_trapper() as trapper:
            compile("def f():\n    for a in []: pass\n    global a\n", "", "exec")
        self.assertEqual(trapper.messages, [":3: SyntaxWarning: name 'a' is assigned to before global declaration"])

        with stderr_trapper() as trapper:
            compile("def f():\n    global a\n    a = 1\n    global a\n", "", "exec")
        self.assertEqual(trapper.messages, [":4: SyntaxWarning: name 'a' is assigned to before global declaration"])

        with stderr_trapper() as trapper:
            compile("def f():\n    print(a)\n    global a\n", "", "exec")
        self.assertEqual(trapper.messages, [":3: SyntaxWarning: name 'a' is used prior to global declaration"])

        with stderr_trapper() as trapper:
            compile("def f():\n    a = 1\n    global a\n    global a\n    a = 1", "", "exec")
        self.assertEqual(trapper.messages,
                [":3: SyntaxWarning: name 'a' is assigned to before global declaration",
                ":4: SyntaxWarning: name 'a' is assigned to before global declaration"])

        with stderr_trapper() as trapper:
            compile("x = 10\nglobal x\n", "", "exec")
        self.assertEqual(trapper.messages, [":2: SyntaxWarning: name 'x' is assigned to before global declaration"])
Example #3
0
    def test_sanity(self):
        global EXPECTED
        try:
            with stderr_trapper() as output:
                # generate test output
                _warnings.warn("Warning Message!")
                expect(UserWarning, "Warning Message!")
                for warn_type in WARN_TYPES:
                    _warnings.warn(warn_type("Type-overriding message!"),
                                   UnicodeWarning)
                    expect(warn_type, "Type-overriding message!")
                    _warnings.warn("Another Warning Message!", warn_type)
                    expect(warn_type, "Another Warning Message!")
                    _warnings.warn_explicit("Explicit Warning!", warn_type,
                                            "nonexistent_file.py", 12)
                    expect(warn_type, "Explicit Warning!")
                    _warnings.warn_explicit("Explicit Warning!", warn_type,
                                            "test_python26.py", 34)
                    expect(warn_type, "Explicit Warning!")
                    _warnings.warn_explicit("Explicit Warning!", warn_type,
                                            "nonexistent_file.py", 56,
                                            "module.py")
                    expect(warn_type, "Explicit Warning!")
                    _warnings.warn_explicit("Explicit Warning!", warn_type,
                                            "test_python26.py", 78,
                                            "module.py")
                    expect(warn_type, "Explicit Warning!")

            temp_messages = output.messages

            #No point in going further if the number of lines is not what we expect
            nlines = len([x for x in temp_messages if not x.startswith("  ")])
            self.assertEqual(nlines, len(EXPECTED))

            # match lines
            for line in temp_messages:
                if line.startswith("  "):
                    continue
                temp = EXPECTED.pop(0).rstrip()
                self.assertTrue(line.endswith(temp),
                                str(line) + " does not end with " + temp)

        finally:
            # remove generated files
            cleanup()
    def test_sanity(self):
        global EXPECTED
        try:
            with stderr_trapper() as output:
                # generate test output
                _warnings.warn("Warning Message!")
                expect(UserWarning, "Warning Message!")
                for warn_type in WARN_TYPES:
                    _warnings.warn(warn_type("Type-overriding message!"), UnicodeWarning)
                    expect(warn_type, "Type-overriding message!")
                    _warnings.warn("Another Warning Message!", warn_type)
                    expect(warn_type, "Another Warning Message!")
                    _warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 12)
                    expect(warn_type, "Explicit Warning!")
                    _warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 34)
                    expect(warn_type, "Explicit Warning!")
                    _warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 56, "module.py")
                    expect(warn_type, "Explicit Warning!")
                    _warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 78, "module.py")
                    expect(warn_type, "Explicit Warning!")

            temp_messages = output.messages

            #No point in going further if the number of lines is not what we expect
            nlines = len([x for x in temp_messages if not x.startswith("  ")])
            self.assertEqual(nlines, len(EXPECTED))

            # match lines
            for line in temp_messages:
                if line.startswith("  "):
                    continue
                temp = EXPECTED.pop(0).rstrip()
                self.assertTrue(line.endswith(temp), str(line) + " does not end with " + temp)

        finally:
            # remove generated files
            cleanup()
Example #5
0
    def test_syntax_warnings(self):
        if is_cli or sys.version_info >= (3,6):
            with self.assertRaisesRegex(SyntaxError, "name 'a' is assigned to before global declaration") as cm:
                compile("def f():\n    a = 1\n    global a\n", "", "exec")
            self.assertEqual(cm.exception.lineno, 3)

            with self.assertRaisesRegex(SyntaxError, "name 'a' is assigned to before global declaration") as cm:
                compile("def f():\n    def a(): pass\n    global a\n", "", "exec")
            self.assertEqual(cm.exception.lineno, 3)

            with self.assertRaisesRegex(SyntaxError, "name 'a' is assigned to before global declaration") as cm:
                compile("def f():\n    for a in []: pass\n    global a\n", "", "exec")
            self.assertEqual(cm.exception.lineno, 3)

            with self.assertRaisesRegex(SyntaxError, "name 'a' is used prior to global declaration" if is_cli else "name 'a' is assigned to before global declaration") as cm:
                compile("def f():\n    global a\n    a = 1\n    global a\n", "", "exec")
            self.assertEqual(cm.exception.lineno, 4)

            with self.assertRaisesRegex(SyntaxError, "name 'a' is used prior to global declaration") as cm:
                compile("def f():\n    print(a)\n    global a\n", "", "exec")
            self.assertEqual(cm.exception.lineno, 3)

            with self.assertRaisesRegex(SyntaxError, "name 'a' is assigned to before global declaration") as cm:
                compile("def f():\n    a = 1\n    global a\n    global a\n    a = 1", "", "exec")
            self.assertEqual(cm.exception.lineno, 3)

            with self.assertRaisesRegex(SyntaxError, "name 'x' is assigned to before global declaration") as cm:
                compile("x = 10\nglobal x\n", "", "exec")
            self.assertEqual(cm.exception.lineno, 2)
        else:
            # syntax error warnings are outputted using warnings.showwarning.  our own warning trapper therefore
            # doesn't see them.  So we trap stderr here instead.  We could use CPython's warning trapper if we
            # checked for the presence of the stdlib.
            with stderr_trapper() as trapper:
                compile("def f():\n    a = 1\n    global a\n", "", "exec")
            self.assertEqual(trapper.messages, [":3: SyntaxWarning: name 'a' is assigned to before global declaration"])

            with stderr_trapper() as trapper:
                compile("def f():\n    def a(): pass\n    global a\n", "", "exec")
            self.assertEqual(trapper.messages, [":3: SyntaxWarning: name 'a' is assigned to before global declaration"])

            with stderr_trapper() as trapper:
                compile("def f():\n    for a in []: pass\n    global a\n", "", "exec")
            self.assertEqual(trapper.messages, [":3: SyntaxWarning: name 'a' is assigned to before global declaration"])

            with stderr_trapper() as trapper:
                compile("def f():\n    global a\n    a = 1\n    global a\n", "", "exec")
            self.assertEqual(trapper.messages, [":4: SyntaxWarning: name 'a' is assigned to before global declaration"])

            with stderr_trapper() as trapper:
                compile("def f():\n    print(a)\n    global a\n", "", "exec")
            self.assertEqual(trapper.messages, [":3: SyntaxWarning: name 'a' is used prior to global declaration"])

            with stderr_trapper() as trapper:
                compile("def f():\n    a = 1\n    global a\n    global a\n    a = 1", "", "exec")
            self.assertEqual(trapper.messages,
                    [":3: SyntaxWarning: name 'a' is assigned to before global declaration",
                    ":4: SyntaxWarning: name 'a' is assigned to before global declaration"])

            with stderr_trapper() as trapper:
                compile("x = 10\nglobal x\n", "", "exec")
            self.assertEqual(trapper.messages, [":2: SyntaxWarning: name 'x' is assigned to before global declaration"])
Example #6
0
 def test_tempnam_warning(self):
     with stderr_trapper() as trapper:
         temp = nt.tempnam()
     
     self.assertTrue(trapper.messages[0].endswith("RuntimeWarning: tempnam is a potential security risk to your program"), trapper.messages)