Team Ai
Apppublic

aravagarwal/CodeCloakPII

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
python3.8_grammar.py1573 linesDownload Raw Back to examples
1# Python test set -- part 1, grammar.2# This just tests whether the parser accepts them all.3 4from test.support import check_syntax_error5import inspect6import unittest7import sys8# testing import *9from sys import *10 11# different import patterns to check that __annotations__ does not interfere12# with import machinery13import test.ann_module as ann_module14import typing15from collections import ChainMap16from test import ann_module217import test18 19# These are shared with test_tokenize and other test modules.20#21# Note: since several test cases filter out floats by looking for "e" and ".",22# don't add hexadecimal literals that contain "e" or "E".23VALID_UNDERSCORE_LITERALS = [24    '0_0_0',25    '4_2',26    '1_0000_0000',27    '0b1001_0100',28    '0xffff_ffff',29    '0o5_7_7',30    '1_00_00.5',31    '1_00_00.5e5',32    '1_00_00e5_1',33    '1e1_0',34    '.1_4',35    '.1_4e1',36    '0b_0',37    '0x_f',38    '0o_5',39    '1_00_00j',40    '1_00_00.5j',41    '1_00_00e5_1j',42    '.1_4j',43    '(1_2.5+3_3j)',44    '(.5_6j)',45]46INVALID_UNDERSCORE_LITERALS = [47    # Trailing underscores:48    '0_',49    '42_',50    '1.4j_',51    '0x_',52    '0b1_',53    '0xf_',54    '0o5_',55    '0 if 1_Else 1',56    # Underscores in the base selector:57    '0_b0',58    '0_xf',59    '0_o5',60    # Old-style octal, still disallowed:61    '0_7',62    '09_99',63    # Multiple consecutive underscores:64    '4_______2',65    '0.1__4',66    '0.1__4j',67    '0b1001__0100',68    '0xffff__ffff',69    '0x___',70    '0o5__77',71    '1e1__0',72    '1e1__0j',73    # Underscore right before a dot:74    '1_.4',75    '1_.4j',76    # Underscore right after a dot:77    '1._4',78    '1._4j',79    '._5',80    '._5j',81    # Underscore right after a sign:82    '1.0e+_1',83    '1.0e+_1j',84    # Underscore right before j:85    '1.4_j',86    '1.4e5_j',87    # Underscore right before e:88    '1_e1',89    '1.4_e1',90    '1.4_e1j',91    # Underscore right after e:92    '1e_1',93    '1.4e_1',94    '1.4e_1j',95    # Complex cases with parens:96    '(1+1.5_j_)',97    '(1+1.5_j)',98]99 100 101class TokenTests(unittest.TestCase):102 103    def test_backslash(self):104        # Backslash means line continuation:105        x = 1 \106        + 1107        self.assertEqual(x, 2, 'backslash for line continuation')108 109        # Backslash does not means continuation in comments :\110        x = 0111        self.assertEqual(x, 0, 'backslash ending comment')112 113    def test_plain_integers(self):114        self.assertEqual(type(000), type(0))115        self.assertEqual(0xff, 255)116        self.assertEqual(0o377, 255)117        self.assertEqual(2147483647, 0o17777777777)118        self.assertEqual(0b1001, 9)119        # "0x" is not a valid literal120        self.assertRaises(SyntaxError, eval, "0x")121        from sys import maxsize122        if maxsize == 2147483647:123            self.assertEqual(-2147483647-1, -0o20000000000)124            # XXX -2147483648125            self.assertTrue(0o37777777777 > 0)126            self.assertTrue(0xffffffff > 0)127            self.assertTrue(0b1111111111111111111111111111111 > 0)128            for s in ('2147483648', '0o40000000000', '0x100000000',129                      '0b10000000000000000000000000000000'):130                try:131                    x = eval(s)132                except OverflowError:133                    self.fail("OverflowError on huge integer literal %r" % s)134        elif maxsize == 9223372036854775807:135            self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000)136            self.assertTrue(0o1777777777777777777777 > 0)137            self.assertTrue(0xffffffffffffffff > 0)138            self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0)139            for s in '9223372036854775808', '0o2000000000000000000000', \140                     '0x10000000000000000', \141                     '0b100000000000000000000000000000000000000000000000000000000000000':142                try:143                    x = eval(s)144                except OverflowError:145                    self.fail("OverflowError on huge integer literal %r" % s)146        else:147            self.fail('Weird maxsize value %r' % maxsize)148 149    def test_long_integers(self):150        x = 0151        x = 0xffffffffffffffff152        x = 0Xffffffffffffffff153        x = 0o77777777777777777154        x = 0O77777777777777777155        x = 123456789012345678901234567890156        x = 0b100000000000000000000000000000000000000000000000000000000000000000000157        x = 0B111111111111111111111111111111111111111111111111111111111111111111111158 159    def test_floats(self):160        x = 3.14161        x = 314.162        x = 0.314163        # XXX x = 000.314164        x = .314165        x = 3e14166        x = 3E14167        x = 3e-14168        x = 3e+14169        x = 3.e14170        x = .3e14171        x = 3.1e4172 173    def test_float_exponent_tokenization(self):174        # See issue 21642.175        self.assertEqual(1 if 1else 0, 1)176        self.assertEqual(1 if 0else 0, 0)177        self.assertRaises(SyntaxError, eval, "0 if 1Else 0")178 179    def test_underscore_literals(self):180        for lit in VALID_UNDERSCORE_LITERALS:181            self.assertEqual(eval(lit), eval(lit.replace('_', '')))182        for lit in INVALID_UNDERSCORE_LITERALS:183            self.assertRaises(SyntaxError, eval, lit)184        # Sanity check: no literal begins with an underscore185        self.assertRaises(NameError, eval, "_0")186 187    def test_string_literals(self):188        x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)189        x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)190        x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)191        x = "doesn't \"shrink\" does it"192        y = 'doesn\'t "shrink" does it'193        self.assertTrue(len(x) == 24 and x == y)194        x = "does \"shrink\" doesn't it"195        y = 'does "shrink" doesn\'t it'196        self.assertTrue(len(x) == 24 and x == y)197        x = """198The "quick"199brown fox200jumps over201the 'lazy' dog.202"""203        y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'204        self.assertEqual(x, y)205        y = '''206The "quick"207brown fox208jumps over209the 'lazy' dog.210'''211        self.assertEqual(x, y)212        y = "\n\213The \"quick\"\n\214brown fox\n\215jumps over\n\216the 'lazy' dog.\n\217"218        self.assertEqual(x, y)219        y = '\n\220The \"quick\"\n\221brown fox\n\222jumps over\n\223the \'lazy\' dog.\n\224'225        self.assertEqual(x, y)226 227    def test_ellipsis(self):228        x = ...229        self.assertTrue(x is Ellipsis)230        self.assertRaises(SyntaxError, eval, ".. .")231 232    def test_eof_error(self):233        samples = ("def foo(", "\ndef foo(", "def foo(\n")234        for s in samples:235            with self.assertRaises(SyntaxError) as cm:236                compile(s, "<test>", "exec")237            self.assertIn("unexpected EOF", str(cm.exception))238 239# var_annot_global: int # a global annotated is necessary for test_var_annot240 241# custom namespace for testing __annotations__242 243class CNS:244    def __init__(self):245        self._dct = {}246    def __setitem__(self, item, value):247        self._dct[item.lower()] = value248    def __getitem__(self, item):249        return self._dct[item]250 251 252class GrammarTests(unittest.TestCase):253 254    check_syntax_error = check_syntax_error255 256    # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE257    # XXX can't test in a script -- this rule is only used when interactive258 259    # file_input: (NEWLINE | stmt)* ENDMARKER260    # Being tested as this very moment this very module261 262    # expr_input: testlist NEWLINE263    # XXX Hard to test -- used only in calls to input()264 265    def test_eval_input(self):266        # testlist ENDMARKER267        x = eval('1, 0 or 1')268 269    def test_var_annot_basics(self):270        # all these should be allowed271        var1: int = 5272        # var2: [int, str]273        my_lst = [42]274        def one():275            return 1276        # int.new_attr: int277        # [list][0]: type278        my_lst[one()-1]: int = 5279        self.assertEqual(my_lst, [5])280 281    def test_var_annot_syntax_errors(self):282        # parser pass283        check_syntax_error(self, "def f: int")284        check_syntax_error(self, "x: int: str")285        check_syntax_error(self, "def f():\n"286                                 "    nonlocal x: int\n")287        # AST pass288        check_syntax_error(self, "[x, 0]: int\n")289        check_syntax_error(self, "f(): int\n")290        check_syntax_error(self, "(x,): int")291        check_syntax_error(self, "def f():\n"292                                 "    (x, y): int = (1, 2)\n")293        # symtable pass294        check_syntax_error(self, "def f():\n"295                                 "    x: int\n"296                                 "    global x\n")297        check_syntax_error(self, "def f():\n"298                                 "    global x\n"299                                 "    x: int\n")300 301    def test_var_annot_basic_semantics(self):302        # execution order303        with self.assertRaises(ZeroDivisionError):304            no_name[does_not_exist]: no_name_again = 1/0305        with self.assertRaises(NameError):306            no_name[does_not_exist]: 1/0 = 0307        global var_annot_global308 309        # function semantics310        def f():311            st: str = "Hello"312            a.b: int = (1, 2)313            return st314        self.assertEqual(f.__annotations__, {})315        def f_OK():316            # x: 1/0317        f_OK()318        def fbad():319            # x: int320            print(x)321        with self.assertRaises(UnboundLocalError):322            fbad()323        def f2bad():324            # (no_such_global): int325            print(no_such_global)326        try:327            f2bad()328        except Exception as e:329            self.assertIs(type(e), NameError)330 331        # class semantics332        class C:333            # __foo: int334            s: str = "attr"335            z = 2336            def __init__(self, x):337                self.x: int = x338        self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str})339        with self.assertRaises(NameError):340            class CBad:341                no_such_name_defined.attr: int = 0342        with self.assertRaises(NameError):343            class Cbad2(C):344                # x: int345                x.y: list = []346 347    def test_var_annot_metaclass_semantics(self):348        class CMeta(type):349            @classmethod350            def __prepare__(metacls, name, bases, **kwds):351                return {'__annotations__': CNS()}352        class CC(metaclass=CMeta):353            # XX: 'ANNOT'354        self.assertEqual(CC.__annotations__['xx'], 'ANNOT')355 356    def test_var_annot_module_semantics(self):357        with self.assertRaises(AttributeError):358            print(test.__annotations__)359        self.assertEqual(ann_module.__annotations__,360                     {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int]})361        self.assertEqual(ann_module.M.__annotations__,362                              {'123': 123, 'o': type})363        self.assertEqual(ann_module2.__annotations__, {})364 365    def test_var_annot_in_module(self):366        # check that functions fail the same way when executed367        # outside of module where they were defined368        from test.ann_module3 import f_bad_ann, g_bad_ann, D_bad_ann369        with self.assertRaises(NameError):370            f_bad_ann()371        with self.assertRaises(NameError):372            g_bad_ann()373        with self.assertRaises(NameError):374            D_bad_ann(5)375 376    def test_var_annot_simple_exec(self):377        gns = {}; lns= {}378        exec("'docstring'\n"379             "__annotations__[1] = 2\n"380             "x: int = 5\n", gns, lns)381        self.assertEqual(lns["__annotations__"], {1: 2, 'x': int})382        with self.assertRaises(KeyError):383            gns['__annotations__']384 385    def test_var_annot_custom_maps(self):386        # tests with custom locals() and __annotations__387        ns = {'__annotations__': CNS()}388        exec('X: int; Z: str = "Z"; (w): complex = 1j', ns)389        self.assertEqual(ns['__annotations__']['x'], int)390        self.assertEqual(ns['__annotations__']['z'], str)391        with self.assertRaises(KeyError):392            ns['__annotations__']['w']393        nonloc_ns = {}394        class CNS2:395            def __init__(self):396                self._dct = {}397            def __setitem__(self, item, value):398                nonlocal nonloc_ns399                self._dct[item] = value400                nonloc_ns[item] = value401            def __getitem__(self, item):402                return self._dct[item]403        exec('x: int = 1', {}, CNS2())404        self.assertEqual(nonloc_ns['__annotations__']['x'], int)405 406    def test_var_annot_refleak(self):407        # complex case: custom locals plus custom __annotations__408        # this was causing refleak409        cns = CNS()410        nonloc_ns = {'__annotations__': cns}411        class CNS2:412            def __init__(self):413                self._dct = {'__annotations__': cns}414            def __setitem__(self, item, value):415                nonlocal nonloc_ns416                self._dct[item] = value417                nonloc_ns[item] = value418            def __getitem__(self, item):419                return self._dct[item]420        exec('X: str', {}, CNS2())421        self.assertEqual(nonloc_ns['__annotations__']['x'], str)422 423    def test_funcdef(self):424        ### [decorators] 'def' NAME parameters ['->' test] ':' suite425        ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE426        ### decorators: decorator+427        ### parameters: '(' [typedargslist] ')'428        ### typedargslist: ((tfpdef ['=' test] ',')*429        ###                ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)430        ###                | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])431        ### tfpdef: NAME [':' test]432        ### varargslist: ((vfpdef ['=' test] ',')*433        ###              ('*' [vfpdef] (',' vfpdef ['=' test])*  [',' '**' vfpdef] | '**' vfpdef)434        ###              | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])435        ### vfpdef: NAME436        def f1(): pass437        f1()438        f1(*())439        f1(*(), **{})440        def f2(one_argument): pass441        def f3(two, arguments): pass442        self.assertEqual(f2.__code__.co_varnames, ('one_argument',))443        self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments'))444        def a1(one_arg,): pass445        def a2(two, args,): pass446        def v0(*rest): pass447        def v1(a, *rest): pass448        def v2(a, b, *rest): pass449 450        f1()451        f2(1)452        f2(1,)453        f3(1, 2)454        f3(1, 2,)455        v0()456        v0(1)457        v0(1,)458        v0(1,2)459        v0(1,2,3,4,5,6,7,8,9,0)460        v1(1)461        v1(1,)462        v1(1,2)463        v1(1,2,3)464        v1(1,2,3,4,5,6,7,8,9,0)465        v2(1,2)466        v2(1,2,3)467        v2(1,2,3,4)468        v2(1,2,3,4,5,6,7,8,9,0)469 470        def d01(a=1): pass471        d01()472        d01(1)473        d01(*(1,))474        d01(*[] or [2])475        d01(*() or (), *{} and (), **() or {})476        d01(**{'a':2})477        d01(**{'a':2} or {})478        def d11(a, b=1): pass479        d11(1)480        d11(1, 2)481        d11(1, **{'b':2})482        def d21(a, b, c=1): pass483        d21(1, 2)484        d21(1, 2, 3)485        d21(*(1, 2, 3))486        d21(1, *(2, 3))487        d21(1, 2, *(3,))488        d21(1, 2, **{'c':3})489        def d02(a=1, b=2): pass490        d02()491        d02(1)492        d02(1, 2)493        d02(*(1, 2))494        d02(1, *(2,))495        d02(1, **{'b':2})496        d02(**{'a': 1, 'b': 2})497        def d12(a, b=1, c=2): pass498        d12(1)499        d12(1, 2)500        d12(1, 2, 3)501        def d22(a, b, c=1, d=2): pass502        d22(1, 2)503        d22(1, 2, 3)504        d22(1, 2, 3, 4)505        def d01v(a=1, *rest): pass506        d01v()507        d01v(1)508        d01v(1, 2)509        d01v(*(1, 2, 3, 4))510        d01v(*(1,))511        d01v(**{'a':2})512        def d11v(a, b=1, *rest): pass513        d11v(1)514        d11v(1, 2)515        d11v(1, 2, 3)516        def d21v(a, b, c=1, *rest): pass517        d21v(1, 2)518        d21v(1, 2, 3)519        d21v(1, 2, 3, 4)520        d21v(*(1, 2, 3, 4))521        d21v(1, 2, **{'c': 3})522        def d02v(a=1, b=2, *rest): pass523        d02v()524        d02v(1)525        d02v(1, 2)526        d02v(1, 2, 3)527        d02v(1, *(2, 3, 4))528        d02v(**{'a': 1, 'b': 2})529        def d12v(a, b=1, c=2, *rest): pass530        d12v(1)531        d12v(1, 2)532        d12v(1, 2, 3)533        d12v(1, 2, 3, 4)534        d12v(*(1, 2, 3, 4))535        d12v(1, 2, *(3, 4, 5))536        d12v(1, *(2,), **{'c': 3})537        def d22v(a, b, c=1, d=2, *rest): pass538        d22v(1, 2)539        d22v(1, 2, 3)540        d22v(1, 2, 3, 4)541        d22v(1, 2, 3, 4, 5)542        d22v(*(1, 2, 3, 4))543        d22v(1, 2, *(3, 4, 5))544        d22v(1, *(2, 3), **{'d': 4})545 546        # keyword argument type tests547        try:548            str('x', **{b'foo':1 })549        except TypeError:550            pass551        else:552            self.fail('Bytes should not work as keyword argument names')553        # keyword only argument tests554        def pos0key1(*, key): return key555        pos0key1(key=100)556        def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2557        pos2key2(1, 2, k1=100)558        pos2key2(1, 2, k1=100, k2=200)559        pos2key2(1, 2, k2=100, k1=200)560        def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg561        pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)562        pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)563 564        self.assertRaises(SyntaxError, eval, "def f(*): pass")565        self.assertRaises(SyntaxError, eval, "def f(*,): pass")566        self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass")567 568        # keyword arguments after *arglist569        def f(*args, **kwargs):570            return args, kwargs571        self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),572                                                    {'x':2, 'y':5}))573        self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {}))574        self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")575        self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}),576                         ((), {'eggs':'scrambled', 'spam':'fried'}))577        self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}),578                         ((), {'eggs':'scrambled', 'spam':'fried'}))579 580        # Check ast errors in *args and *kwargs581        check_syntax_error(self, "f(*g(1=2))")582        check_syntax_error(self, "f(**g(1=2))")583 584        # argument annotation tests585        def f(x) -> list: pass586        self.assertEqual(f.__annotations__, {'return': list})587        def f(x: int): pass588        self.assertEqual(f.__annotations__, {'x': int})589        def f(*x: str): pass590        self.assertEqual(f.__annotations__, {'x': str})591        def f(**x: float): pass592        self.assertEqual(f.__annotations__, {'x': float})593        def f(x, y: 1+2): pass594        self.assertEqual(f.__annotations__, {'y': 3})595        def f(a, b: 1, c: 2, d): pass596        self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})597        def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass598        self.assertEqual(f.__annotations__,599                         {'b': 1, 'c': 2, 'e': 3, 'g': 6})600        def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10,601              **k: 11) -> 12: pass602        self.assertEqual(f.__annotations__,603                         {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,604                          'k': 11, 'return': 12})605        # Check for issue #20625 -- annotations mangling606        class Spam:607            def f(self, *, __kw: 1):608                pass609        class Ham(Spam): pass610        self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1})611        self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1})612        # Check for SF Bug #1697248 - mixing decorators and a return annotation613        def null(x): return x614        @null615        def f(x) -> list: pass616        self.assertEqual(f.__annotations__, {'return': list})617 618        # test closures with a variety of opargs619        closure = 1620        def f(): return closure621        def f(x=1): return closure622        def f(*, k=1): return closure623        def f() -> int: return closure624 625        # Check trailing commas are permitted in funcdef argument list626        def f(a,): pass627        def f(*args,): pass628        def f(**kwds,): pass629        def f(a, *args,): pass630        def f(a, **kwds,): pass631        def f(*args, b,): pass632        def f(*, b,): pass633        def f(*args, **kwds,): pass634        def f(a, *args, b,): pass635        def f(a, *, b,): pass636        def f(a, *args, **kwds,): pass637        def f(*args, b, **kwds,): pass638        def f(*, b, **kwds,): pass639        def f(a, *args, b, **kwds,): pass640        def f(a, *, b, **kwds,): pass641 642    def test_lambdef(self):643        ### lambdef: 'lambda' [varargslist] ':' test644        l1 = lambda : 0645        self.assertEqual(l1(), 0)646        l2 = lambda : a[d] # XXX just testing the expression647        l3 = lambda : [2 < x for x in [-1, 3, 0]]648        self.assertEqual(l3(), [0, 1, 0])649        l4 = lambda x = lambda y = lambda z=1 : z : y() : x()650        self.assertEqual(l4(), 1)651        l5 = lambda x, y, z=2: x + y + z652        self.assertEqual(l5(1, 2), 5)653        self.assertEqual(l5(1, 2, 3), 6)654        check_syntax_error(self, "lambda x: x = 2")655        check_syntax_error(self, "lambda (None,): None")656        l6 = lambda x, y, *, k=20: x+y+k657        self.assertEqual(l6(1,2), 1+2+20)658        self.assertEqual(l6(1,2,k=10), 1+2+10)659 660        # check that trailing commas are permitted661        l10 = lambda a,: 0662        l11 = lambda *args,: 0663        l12 = lambda **kwds,: 0664        l13 = lambda a, *args,: 0665        l14 = lambda a, **kwds,: 0666        l15 = lambda *args, b,: 0667        l16 = lambda *, b,: 0668        l17 = lambda *args, **kwds,: 0669        l18 = lambda a, *args, b,: 0670        l19 = lambda a, *, b,: 0671        l20 = lambda a, *args, **kwds,: 0672        l21 = lambda *args, b, **kwds,: 0673        l22 = lambda *, b, **kwds,: 0674        l23 = lambda a, *args, b, **kwds,: 0675        l24 = lambda a, *, b, **kwds,: 0676 677 678    ### stmt: simple_stmt | compound_stmt679    # Tested below680 681    def test_simple_stmt(self):682        ### simple_stmt: small_stmt (';' small_stmt)* [';']683        x = 1; pass; del x684        def foo():685            # verify statements that end with semi-colons686            x = 1; pass; del x;687        foo()688 689    ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt690    # Tested below691 692    def test_expr_stmt(self):693        # (exprlist '=')* exprlist694        1695        1, 2, 3696        x = 1697        x = 1, 2, 3698        x = y = z = 1, 2, 3699        x, y, z = 1, 2, 3700        abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)701 702        check_syntax_error(self, "x + 1 = 1")703        check_syntax_error(self, "a + 1 = b + 2")704 705    # Check the heuristic for print & exec covers significant cases706    # As well as placing some limits on false positives707    def test_former_statements_refer_to_builtins(self):708        keywords = "print", "exec"709        # Cases where we want the custom error710        cases = [711            "{} foo",712            "{} {{1:foo}}",713            "if 1: {} foo",714            "if 1: {} {{1:foo}}",715            "if 1:\n    {} foo",716            "if 1:\n    {} {{1:foo}}",717        ]718        for keyword in keywords:719            custom_msg = "call to '{}'".format(keyword)720            for case in cases:721                source = case.format(keyword)722                with self.subTest(source=source):723                    with self.assertRaisesRegex(SyntaxError, custom_msg):724                        exec(source)725                source = source.replace("foo", "(foo.)")726                with self.subTest(source=source):727                    with self.assertRaisesRegex(SyntaxError, "invalid syntax"):728                        exec(source)729 730    def test_del_stmt(self):731        # 'del' exprlist732        abc = [1,2,3]733        x, y, z = abc734        xyz = x, y, z735 736        del abc737        del x, y, (z, xyz)738 739    def test_pass_stmt(self):740        # 'pass'741        pass742 743    # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt744    # Tested below745 746    def test_break_stmt(self):747        # 'break'748        while 1: break749 750    def test_continue_stmt(self):751        # 'continue'752        i = 1753        while i: i = 0; continue754 755        msg = ""756        while not msg:757            msg = "ok"758            try:759                continue760                msg = "continue failed to continue inside try"761            except:762                msg = "continue inside try called except block"763        if msg != "ok":764            self.fail(msg)765 766        msg = ""767        while not msg:768            msg = "finally block not called"769            try:770                continue771            finally:772                msg = "ok"773        if msg != "ok":774            self.fail(msg)775 776    def test_break_continue_loop(self):777        # This test warrants an explanation. It is a test specifically for SF bugs778        # #463359 and #462937. The bug is that a 'break' statement executed or779        # exception raised inside a try/except inside a loop, *after* a continue780        # statement has been executed in that loop, will cause the wrong number of781        # arguments to be popped off the stack and the instruction pointer reset to782        # a very small number (usually 0.) Because of this, the following test783        # *must* written as a function, and the tracking vars *must* be function784        # arguments with default values. Otherwise, the test will loop and loop.785 786        def test_inner(extra_burning_oil = 1, count=0):787            big_hippo = 2788            while big_hippo:789                count += 1790                try:791                    if extra_burning_oil and big_hippo == 1:792                        extra_burning_oil -= 1793                        break794                    big_hippo -= 1795                    continue796                except:797                    raise798            if count > 2 or big_hippo != 1:799                self.fail("continue then break in try/except in loop broken!")800        test_inner()801 802    def test_return(self):803        # 'return' [testlist]804        def g1(): return805        def g2(): return 1806        g1()807        x = g2()808        check_syntax_error(self, "class foo:return 1")809 810    def test_break_in_finally(self):811        count = 0812        while count < 2:813            count += 1814            try:815                pass816            finally:817                break818        self.assertEqual(count, 1)819 820        count = 0821        while count < 2:822            count += 1823            try:824                continue825            finally:826                break827        self.assertEqual(count, 1)828 829        count = 0830        while count < 2:831            count += 1832            try:833                1/0834            finally:835                break836        self.assertEqual(count, 1)837 838        for count in [0, 1]:839            self.assertEqual(count, 0)840            try:841                pass842            finally:843                break844        self.assertEqual(count, 0)845 846        for count in [0, 1]:847            self.assertEqual(count, 0)848            try:849                continue850            finally:851                break852        self.assertEqual(count, 0)853 854        for count in [0, 1]:855            self.assertEqual(count, 0)856            try:857                1/0858            finally:859                break860        self.assertEqual(count, 0)861 862    def test_continue_in_finally(self):863        count = 0864        while count < 2:865            count += 1866            try:867                pass868            finally:869                continue870            break871        self.assertEqual(count, 2)872 873        count = 0874        while count < 2:875            count += 1876            try:877                break878            finally:879                continue880        self.assertEqual(count, 2)881 882        count = 0883        while count < 2:884            count += 1885            try:886                1/0887            finally:888                continue889            break890        self.assertEqual(count, 2)891 892        for count in [0, 1]:893            try:894                pass895            finally:896                continue897            break898        self.assertEqual(count, 1)899 900        for count in [0, 1]:901            try:902                break903            finally:904                continue905        self.assertEqual(count, 1)906 907        for count in [0, 1]:908            try:909                1/0910            finally:911                continue912            break913        self.assertEqual(count, 1)914 915    def test_return_in_finally(self):916        def g1():917            try:918                pass919            finally:920                return 1921        self.assertEqual(g1(), 1)922 923        def g2():924            try:925                return 2926            finally:927                return 3928        self.assertEqual(g2(), 3)929 930        def g3():931            try:932                1/0933            finally:934                return 4935        self.assertEqual(g3(), 4)936 937    def test_yield(self):938        # Allowed as standalone statement939        def g(): yield 1940        def g(): yield from ()941        # Allowed as RHS of assignment942        def g(): x = yield 1943        def g(): x = yield from ()944        # Ordinary yield accepts implicit tuples945        def g(): yield 1, 1946        def g(): x = yield 1, 1947        # 'yield from' does not948        check_syntax_error(self, "def g(): yield from (), 1")949        check_syntax_error(self, "def g(): x = yield from (), 1")950        # Requires parentheses as subexpression951        def g(): 1, (yield 1)952        def g(): 1, (yield from ())953        check_syntax_error(self, "def g(): 1, yield 1")954        check_syntax_error(self, "def g(): 1, yield from ()")955        # Requires parentheses as call argument956        def g(): f((yield 1))957        def g(): f((yield 1), 1)958        def g(): f((yield from ()))959        def g(): f((yield from ()), 1)960        check_syntax_error(self, "def g(): f(yield 1)")961        check_syntax_error(self, "def g(): f(yield 1, 1)")962        check_syntax_error(self, "def g(): f(yield from ())")963        check_syntax_error(self, "def g(): f(yield from (), 1)")964        # Not allowed at top level965        check_syntax_error(self, "yield")966        check_syntax_error(self, "yield from")967        # Not allowed at class scope968        check_syntax_error(self, "class foo:yield 1")969        check_syntax_error(self, "class foo:yield from ()")970        # Check annotation refleak on SyntaxError971        check_syntax_error(self, "def g(a:(yield)): pass")972 973    def test_yield_in_comprehensions(self):974        # Check yield in comprehensions975        def g(): [x for x in [(yield 1)]]976        def g(): [x for x in [(yield from ())]]977 978        check = self.check_syntax_error979        check("def g(): [(yield x) for x in ()]",980              "'yield' inside list comprehension")981        check("def g(): [x for x in () if not (yield x)]",982              "'yield' inside list comprehension")983        check("def g(): [y for x in () for y in [(yield x)]]",984              "'yield' inside list comprehension")985        check("def g(): {(yield x) for x in ()}",986              "'yield' inside set comprehension")987        check("def g(): {(yield x): x for x in ()}",988              "'yield' inside dict comprehension")989        check("def g(): {x: (yield x) for x in ()}",990              "'yield' inside dict comprehension")991        check("def g(): ((yield x) for x in ())",992              "'yield' inside generator expression")993        check("def g(): [(yield from x) for x in ()]",994              "'yield' inside list comprehension")995        check("class C: [(yield x) for x in ()]",996              "'yield' inside list comprehension")997        check("[(yield x) for x in ()]",998              "'yield' inside list comprehension")999 1000    def test_raise(self):1001        # 'raise' test [',' test]1002        try: raise RuntimeError('just testing')1003        except RuntimeError: pass1004        try: raise KeyboardInterrupt1005        except KeyboardInterrupt: pass1006 1007    def test_import(self):1008        # 'import' dotted_as_names1009        import sys1010        import time, sys1011        # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)1012        from time import time1013        from time import (time)1014        # not testable inside a function, but already done at top of the module1015        # from sys import *1016        from sys import path, argv1017        from sys import (path, argv)1018        from sys import (path, argv,)1019 1020    def test_global(self):1021        # 'global' NAME (',' NAME)*1022        global a1023        global a, b1024        global one, two, three, four, five, six, seven, eight, nine, ten1025 1026    def test_nonlocal(self):1027        # 'nonlocal' NAME (',' NAME)*1028        x = 01029        y = 01030        def f():1031            nonlocal x1032            nonlocal x, y1033 1034    def test_assert(self):1035        # assertTruestmt: 'assert' test [',' test]1036        assert 11037        assert 1, 11038        assert lambda x:x1039        assert 1, lambda x:x+11040 1041        try:1042            assert True1043        except AssertionError as e:1044            self.fail("'assert True' should not have raised an AssertionError")1045 1046        try:1047            assert True, 'this should always pass'1048        except AssertionError as e:1049            self.fail("'assert True, msg' should not have "1050                      "raised an AssertionError")1051 1052    # these tests fail if python is run with -O, so check __debug__1053    @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")1054    def testAssert2(self):1055        try:1056            assert 0, "msg"1057        except AssertionError as e:1058            self.assertEqual(e.args[0], "msg")1059        else:1060            self.fail("AssertionError not raised by assert 0")1061 1062        try:1063            assert False1064        except AssertionError as e:1065            self.assertEqual(len(e.args), 0)1066        else:1067            self.fail("AssertionError not raised by 'assert False'")1068 1069 1070    ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef1071    # Tested below1072 1073    def test_if(self):1074        # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]1075        if 1: pass1076        if 1: pass1077        else: pass1078        if 0: pass1079        elif 0: pass1080        if 0: pass1081        elif 0: pass1082        elif 0: pass1083        elif 0: pass1084        else: pass1085 1086    def test_while(self):1087        # 'while' test ':' suite ['else' ':' suite]1088        while 0: pass1089        while 0: pass1090        else: pass1091 1092        # Issue1920: "while 0" is optimized away,1093        # ensure that the "else" clause is still present.1094        x = 01095        while 0:1096            x = 11097        else:1098            x = 21099        self.assertEqual(x, 2)1100 1101    def test_for(self):1102        # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]1103        for i in 1, 2, 3: pass1104        for i, j, k in (): pass1105        else: pass1106        class Squares:1107            def __init__(self, max):1108                self.max = max1109                self.sofar = []1110            def __len__(self): return len(self.sofar)1111            def __getitem__(self, i):1112                if not 0 <= i < self.max: raise IndexError1113                n = len(self.sofar)1114                while n <= i:1115                    self.sofar.append(n*n)1116                    n = n+11117                return self.sofar[i]1118        n = 01119        for x in Squares(10): n = n+x1120        if n != 285:1121            self.fail('for over growing sequence')1122 1123        result = []1124        for x, in [(1,), (2,), (3,)]:1125            result.append(x)1126        self.assertEqual(result, [1, 2, 3])1127 1128    def test_try(self):1129        ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]1130        ###         | 'try' ':' suite 'finally' ':' suite1131        ### except_clause: 'except' [expr ['as' expr]]1132        try:1133            1/01134        except ZeroDivisionError:1135            pass1136        else:1137            pass1138        try: 1/01139        except EOFError: pass1140        except TypeError as msg: pass1141        except: pass1142        else: pass1143        try: 1/01144        except (EOFError, TypeError, ZeroDivisionError): pass1145        try: 1/01146        except (EOFError, TypeError, ZeroDivisionError) as msg: pass1147        try: pass1148        finally: pass1149 1150    def test_suite(self):1151        # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT1152        if 1: pass1153        if 1:1154            pass1155        if 1:1156            #1157            #1158            #1159            pass1160            pass1161            #1162            pass1163            #1164 1165    def test_test(self):1166        ### and_test ('or' and_test)*1167        ### and_test: not_test ('and' not_test)*1168        ### not_test: 'not' not_test | comparison1169        if not 1: pass1170        if 1 and 1: pass1171        if 1 or 1: pass1172        if not not not 1: pass1173        if not 1 and 1 and 1: pass1174        if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass1175 1176    def test_comparison(self):1177        ### comparison: expr (comp_op expr)*1178        ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'1179        if 1: pass1180        x = (1 == 1)1181        if 1 == 1: pass1182        if 1 != 1: pass1183        if 1 < 1: pass1184        if 1 > 1: pass1185        if 1 <= 1: pass1186        if 1 >= 1: pass1187        if 1 is 1: pass1188        if 1 is not 1: pass1189        if 1 in (): pass1190        if 1 not in (): pass1191        if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass1192 1193    def test_binary_mask_ops(self):1194        x = 1 & 11195        x = 1 ^ 11196        x = 1 | 11197 1198    def test_shift_ops(self):1199        x = 1 << 11200        x = 1 >> 1

Showing the first 1,200 of 1573 lines. Download the file for the rest.