[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/go-python/gpython/main/symtable/make_symtable_test.py [Back]  [Original]

#!/usr/bin/env python3.4

# Copyright 2018 The go-python Authors.  All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

"""
Write symtable_data_test.go
"""

import sys
import ast
import subprocess
import dis
from symtable import symtable
try:
    # run ./build.sh in the readsymtab directory to create this module
    from readsymtab import readsymtab
    use_readsymtab = True
except ImportError:
    import ctypes
    use_readsymtab = False
    print("Using compiler dependent code to read bitfields - compile readsymtab module to be sure!")

inp = [
    ('''1''', "eval"),
    ('''a*b*c''', "eval"),
    # Functions
    ('''def fn(): pass''', "exec"),
    ('''def fn(a,b):\n e=1\n return a*b*c*d*e''', "exec"),
    ('''def fn(*args,a=2,b=3,**kwargs): return (args,a,b,kwargs)''', "exec"),
    ('''def fn(a,b):\n def nested(c,d):\n  return a*b*c*d*e''', "exec"),
    ('''\
def fn(a:A,*arg:ARG,b:B=BB,c:C=CC,**kwargs:KW) -> RET:
    def fn(A,b):
        e=1
        return a*arg*b*c*kwargs*A*e*glob''', "exec"),
    ('''\
def fn(a):
    global b
    b = a''', "exec"),
    ('''\
def fn(a):
    global b
    global b
    return b''', "exec"),
    ('''\
def inner():
  print(x)
  global x
''', "exec"),
    ('''\
def fn(a):
    b = 6
    global b
    b = a''', "exec"),
    ('''\
def fn(a=b,c=1):
    return a+b''', "exec"),
    ('''\
@sausage
@potato(beans)
def outer():
   x = 1
   def inner():
       nonlocal x
       x = 2''', "exec"),
    ('''\
def fn(a):
    nonlocal b
    ''', "exec", SyntaxError),
    ('''\
def outer():
   def inner():
       nonlocal x
       x = 2''', "exec", SyntaxError),
    ('''\
def outer():
   x = 1
   def inner():
       print(x)
       nonlocal x
''', "exec"),
    ('''\
def outer():
   x = 1
   def inner():
       x = 2
       nonlocal x''', "exec"),
    ('''\
def outer():
   x = 1
   def inner(x):
       nonlocal x''', "exec", SyntaxError),
    ('''\
def outer():
   x = 1
   def inner(x):
       global x''', "exec", SyntaxError),
    ('''\
def outer():
   def inner():
       global x
       nonlocal x
       ''', "exec", SyntaxError),
    ('''\
def outer():
   x = 1
   def inner():
       y = 2
       return x + y + z
''', "exec"),
    ('''\
def outer():
   global x
   def inner():
       return x
''', "exec"),
    ('''\
nonlocal x
''', "exec", SyntaxError),
    ('''def fn(a,a): pass''', "exec", SyntaxError),
    # List Comp
    ('''[ x for x in xs ]''', "exec"),
    ('''[ x+y for x in xs for y in ys ]''', "exec"),
    ('''[ x+y+z for x in xs if x if y if z if r for y in ys if x if y if z if p for z in zs if x if y if z if q]''', "exec"),
    ('''[ x+y for x in [ x for x in xs ] ]''', "exec"),
    ('''[ x for x in xs ]\n[ y for y in ys ]''', "exec"),
    # Generator expr
    ('''( x for x in xs )''', "exec"),
    ('''( x+y for x in xs for y in ys )''', "exec"),
    ('''( x+y+z for x in xs if x if y if z if r for y in ys if x if y if z if p for z in zs if x if y if z if q)''', "exec"),
    ('''( x+y for x in ( x for x in xs ) )''', "exec"),
    # Set comp
    ('''{ x for x in xs }''', "exec"),
    ('''{ x+y for x in xs for y in ys }''', "exec"),
    ('''{ x+y+z for x in xs if x if y if z if r for y in ys if x if y if z if p for z in zs if x if y if z if q}''', "exec"),
    ('''{ x+y for x in { x for x in xs } }''', "exec"),
    # Dict comp
    ('''{ x:1 for x in xs }''', "exec"),
    ('''{ x+y:1 for x in xs for y in ys }''', "exec"),
    ('''{ x+y+z:1 for x in xs if x if y if z if r for y in ys if x if y if z if p for z in zs if x if y if z if q}''', "exec"),
    ('''{ x+y:k for k, x in { x:1 for x in xs } }''', "exec"),
    # Class
    ('''\
@potato
@sausage()
class A(a,b,c="1",d="2",*args,**kwargs):
    VAR = x
    def method(self):
        super().method()
        return VAR
''', "exec"),
    # Lambda
    ('''lambda: x''', "exec"),
    ('''lambda y: x+y''', "exec"),
    ('''lambda a,*arg,b=BB,c=CC,**kwargs: POTATO+a+arg+b+c+kwargs''', "exec"),
    # With
    ('''\
with x() as y:
  y.floop()
print(y)
''', "exec"),
    # try, except
    ('''\
try:
  something()
except RandomError as e:
  print(e)
print(e)
''', "exec"),
    # Import
    ('''import potato''', "exec"),
    ('''import potato.sausage''', "exec"),
    ('''from potato import sausage''', "exec"),
    ('''from potato import sausage as salami''', "exec"),
    ('''from potato import *''', "exec"),
    ('''\
def fn():
  from potato import *
''', "exec", SyntaxError),
    # Yield
    ('''\
def f():
    yield
    ''', "exec"),
    ('''\
def f():
    yield from range(10)
    ''', "exec"),
]

def dump_bool(b):
    return ("true" if b else "false")

def dump_strings(ss):
    return "[]string{"+",".join([ '"%s"' % s for s in ss ])+"}"

# Scope numbers to names (from symtable.h)
SCOPES = {
    1: "ScopeLocal",
    2: "ScopeGlobalExplicit",
    3: "ScopeGlobalImplicit",
    4: "ScopeFree",
    5: "ScopeCell",
}

#def-use flags to names (from symtable.h)
DEF_FLAGS = (
    ("DefGlobal", 1),      # global stmt
    ("DefLocal", 2),       # assignment in code block
    ("DefParam", 2

Web Proxy Viewer  |  New URL  |  Original Page