示例#1
0
    def test_errors(self):
        support.skipIfBCI(self)

        # Test syntax restrictions - these are all compile-time errors:
        #
        for expr in ["1+2", "x[3]", "(1, 2)"]:
            # Sanity check: is expr is a valid expression by itself?
            compile(expr, "testexpr", "exec")

            codestr = "@%s\ndef f(): pass" % expr
            self.assertRaises(SyntaxError, compile, codestr, "test", "exec")

        # You can't put multiple decorators on a single line:
        #
        self.assertRaises(SyntaxError, compile, "@f1 @f2\ndef f(): pass",
                          "test", "exec")

        # Test runtime errors

        def unimp(func):
            raise NotImplementedError

        context = dict(nullval=None, unimp=unimp)

        for expr, exc in [("undef", NameError), ("nullval", TypeError),
                          ("nullval.attr", AttributeError),
                          ("unimp", NotImplementedError)]:
            codestr = "@%s\ndef f(): pass\nassert f() is None" % expr
            code = compile(codestr, "test", "exec")
            self.assertRaises(exc, eval, code, context)
示例#2
0
    def test_string_in_loop_on_same_line(self):
        support.skipIfBCI(self)

        python2_print_str = 'for i in s: print i'
        with self.assertRaises(SyntaxError) as context:
            exec(python2_print_str)

        self.assertIn('print(i)', str(context.exception))
示例#3
0
    def test_string_with_semicolon(self):
        support.skipIfBCI(self)

        python2_print_str = 'print p;'
        with self.assertRaises(SyntaxError) as context:
            exec(python2_print_str)

        self.assertIn('print(p)', str(context.exception))
示例#4
0
    def test_string_with_excessive_whitespace(self):
        support.skipIfBCI(self)

        python2_print_str = 'print  "Hello World", '
        with self.assertRaises(SyntaxError) as context:
            exec(python2_print_str)

        self.assertIn('print("Hello World", end=" ")', str(context.exception))
示例#5
0
    def test_normal_string(self):
        support.skipIfBCI(self)

        python2_print_str = 'print "Hello World"'
        with self.assertRaises(SyntaxError) as context:
            exec(python2_print_str)

        self.assertIn('print("Hello World")', str(context.exception))
示例#6
0
    def test_format_locals(self):
        support.skipIfBCI(self)

        def some_inner(k, v):
            a = 1
            b = 2
            return traceback.StackSummary.extract(traceback.walk_stack(None),
                                                  capture_locals=True,
                                                  limit=1)

        s = some_inner(3, 4)
        self.assertEqual([
            '  File "%s", line %d, in some_inner\n'
            '    return traceback.StackSummary.extract(\n'
            '    a = 1\n'
            '    b = 2\n'
            '    k = 3\n'
            '    v = 4\n' % (__file__, some_inner.__code__.co_firstlineno + 3)
        ], s.format())