def test_locate_identifiers(self): """test the location of identifiers in a python code string""" code = """ a = 10 b = 5 c = x * 5 + a + b + q (g,h,i) = (1,2,3) [u,k,j] = [4,5,6] foo.hoho.lala.bar = 7 + gah.blah + u + blah for lar in (1,2,3): gh = 5 x = 12 ("hello world, ", a, b) ("Another expr", c) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_( parsed.declared_identifiers, set( ["a", "b", "c", "g", "h", "i", "u", "k", "j", "gh", "lar", "x"] ), ) eq_( parsed.undeclared_identifiers, set(["x", "q", "foo", "gah", "blah"]), ) parsed = ast.PythonCode("x + 5 * (y-z)", **exception_kwargs) assert parsed.undeclared_identifiers == set(["x", "y", "z"]) assert parsed.declared_identifiers == set()
def test_locate_identifiers_6(self): code = """ def foo(): return bar() """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"bar"}) code = """ def lala(x, y): return x, y, z print(x) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"z", "x"}) eq_(parsed.declared_identifiers, {"lala"}) code = """ def lala(x, y): def hoho(): def bar(): z = 7 print(z) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"z"}) eq_(parsed.declared_identifiers, {"lala"})
def test_locate_identifiers(self): """test the location of identifiers in a python code string""" code = """ a = 10 b = 5 c = x * 5 + a + b + q (g,h,i) = (1,2,3) [u,k,j] = [4,5,6] foo.hoho.lala.bar = 7 + gah.blah + u + blah for lar in (1,2,3): gh = 5 x = 12 ("hello world, ", a, b) ("Another expr", c) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_( parsed.declared_identifiers, set([ 'a', 'b', 'c', 'g', 'h', 'i', 'u', 'k', 'j', 'gh', 'lar', 'x' ])) eq_(parsed.undeclared_identifiers, set(['x', 'q', 'foo', 'gah', 'blah'])) parsed = ast.PythonCode("x + 5 * (y-z)", **exception_kwargs) assert parsed.undeclared_identifiers == set(['x', 'y', 'z']) assert parsed.declared_identifiers == set()
def test_locate_identifiers_6(self): code = """ def foo(): return bar() """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, set(['bar'])) code = """ def lala(x, y): return x, y, z print(x) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, set(['z', 'x'])) eq_(parsed.declared_identifiers, set(['lala'])) code = """ def lala(x, y): def hoho(): def bar(): z = 7 print(z) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, set(['z'])) eq_(parsed.declared_identifiers, set(['lala']))
def test_locate_identifiers_9(self): code = """ ",".join([t for t in ("a", "b", "c")]) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, {"t"}) eq_(parsed.undeclared_identifiers, {"t"}) code = """ [(val, name) for val, name in x] """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, {"val", "name"}) eq_(parsed.undeclared_identifiers, {"val", "name", "x"})
def test_locate_identifiers_9(self): code = """ ",".join([t for t in ("a", "b", "c")]) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set(['t'])) eq_(parsed.undeclared_identifiers, set(['t'])) code = """ [(val, name) for val, name in x] """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set(['val', 'name'])) eq_(parsed.undeclared_identifiers, set(['val', 'name', 'x']))
def __init__(self, keyword, attributes, **kwargs): super(CallTag, self).__init__(keyword, attributes, ('args'), ('expr', ), ('expr', ), **kwargs) self.expression = attributes['expr'] self.code = ast.PythonCode(self.expression, **self.exception_kwargs) self.body_decl = ast.FunctionArgs(attributes.get('args', ''), **self.exception_kwargs)
def test_locate_identifiers_10(self): code = """ lambda q: q + 5 """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set()) eq_(parsed.undeclared_identifiers, set())
def _parse_attributes(self, expressions, nonexpressions): undeclared_identifiers = set() self.parsed_attributes = {} for key in self.attributes: if key in expressions: expr = [] for x in re.compile(r"(\${.+?})", re.S).split(self.attributes[key]): m = re.compile(r"^\${(.+?)}$", re.S).match(x) if m: code = ast.PythonCode( m.group(1).rstrip(), **self.exception_kwargs) # we aren't discarding "declared_identifiers" here, # which we do so that list comprehension-declared # variables aren't counted. As yet can't find a # condition that requires it here. undeclared_identifiers = undeclared_identifiers.union( code.undeclared_identifiers) expr.append("(%s)" % m.group(1)) else: if x: expr.append(repr(x)) self.parsed_attributes[key] = " + ".join(expr) or repr("") elif key in nonexpressions: if re.search(r"\${.+?}", self.attributes[key]): raise exceptions.CompileException( "Attibute '%s' in tag '%s' does not allow embedded " "expressions" % (key, self.keyword), **self.exception_kwargs) self.parsed_attributes[key] = repr(self.attributes[key]) else: raise exceptions.CompileException( "Invalid attribute for tag '%s': '%s'" % (self.keyword, key), **self.exception_kwargs) self.expression_undeclared_identifiers = undeclared_identifiers
def __init__(self, keyword, attributes, **kwargs): super().__init__(keyword, attributes, ("args"), ("expr", ), ("expr", ), **kwargs) self.expression = attributes["expr"] self.code = ast.PythonCode(self.expression, **self.exception_kwargs) self.body_decl = ast.FunctionArgs(attributes.get("args", ""), **self.exception_kwargs)
def test_locate_identifiers_7(self): code = """ import foo.bar """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set(['foo'])) eq_(parsed.undeclared_identifiers, set())
def _parse_attributes(self, expressions, nonexpressions): undeclared_identifiers = set() self.parsed_attributes = {} for key in self.attributes: if key in expressions: expr = [] for x in re.compile(r'(\${.+?})', re.S).split(self.attributes[key]): m = re.compile(r'^\${(.+?)}$', re.S).match(x) if m: code = ast.PythonCode( m.group(1).rstrip(), **self.exception_kwargs) undeclared_identifiers = \ undeclared_identifiers.union( code.undeclared_identifiers) expr.append('(%s)' % m.group(1)) else: if x: expr.append(repr(x)) self.parsed_attributes[key] = " + ".join(expr) or repr('') elif key in nonexpressions: if re.search(r'\${.+?}', self.attributes[key]): raise exceptions.CompileException( "Attibute '%s' in tag '%s' does not allow embedded " "expressions" % (key, self.keyword), **self.exception_kwargs) self.parsed_attributes[key] = repr(self.attributes[key]) else: raise exceptions.CompileException( "Invalid attribute for tag '%s': '%s'" % (self.keyword, key), **self.exception_kwargs) self.expression_undeclared_identifiers = undeclared_identifiers
def __init__(self, keyword, attributes, **kwargs): super(IncludeTag, self).__init__(keyword, attributes, ('file', 'import', 'args'), (), ('file', ), **kwargs) self.page_args = ast.PythonCode( "__DUMMY(%s)" % attributes.get('args', ''), **self.exception_kwargs)
def __init__(self, keyword, attributes, **kwargs): super(IncludeTag, self).__init__(keyword, attributes, ("file", "import", "args"), (), ("file", ), **kwargs) self.page_args = ast.PythonCode( "__DUMMY(%s)" % attributes.get("args", ""), **self.exception_kwargs)
def __init__(self, namespace, defname, attributes, **kwargs): super(CallNamespaceTag, self).__init__( namespace + ":" + defname, attributes, tuple(attributes.keys()) + ("args",), (), (), **kwargs ) self.expression = "%s.%s(%s)" % ( namespace, defname, ",".join( [ "%s=%s" % (k, v) for k, v in self.parsed_attributes.items() if k != "args" ] ), ) self.code = ast.PythonCode(self.expression, **self.exception_kwargs) self.body_decl = ast.FunctionArgs( attributes.get, **self.exception_kwargs )
def undeclared_identifiers(self): res = [] for c in self.function_decl.defaults: res += list(ast.PythonCode(c, **self.exception_kwargs).undeclared_identifiers) return res + list(self.filter_args.\ undeclared_identifiers.\ difference(filters.DEFAULT_ESCAPES.keys()) )
def test_locate_identifiers_11(self): code = """ def x(q): return q + 5 """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set(['x'])) eq_(parsed.undeclared_identifiers, set())
def test_locate_identifiers_17(self): code = """ try: print(x) except (Foo, Bar) as e: print(y) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, set(["x", "y", "Foo", "Bar"]))
def test_locate_identifiers_5(self): code = """ try: print(x) except: print(y) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, set(['x', 'y']))
def test_locate_identifiers_16(self): code = """ try: print(x) except Exception as e: print(y) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"x", "y", "Exception"})
def test_locate_identifiers_12(self): code = """ def foo(): s = 1 def bar(): t = s """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set(['foo'])) eq_(parsed.undeclared_identifiers, set())
def test_locate_identifiers_6(self): code = """ def foo(): return bar() """ parsed = ast.PythonCode(code, **exception_kwargs) assert parsed.undeclared_identifiers == set(['bar']) if util.py3k: code = """ def lala(x, y): return x, y, z print(x) """ else: code = """ def lala(x, y): return x, y, z print x """ parsed = ast.PythonCode(code, **exception_kwargs) assert parsed.undeclared_identifiers == set(['z', 'x']) assert parsed.declared_identifiers == set(['lala']) if util.py3k: code = """ def lala(x, y): def hoho(): def bar(): z = 7 print(z) """ else: code = """ def lala(x, y): def hoho(): def bar(): z = 7 print z """ parsed = ast.PythonCode(code, **exception_kwargs) assert parsed.undeclared_identifiers == set(['z']) assert parsed.declared_identifiers == set(['lala'])
def test_locate_identifiers_4(self): code = """ x = 5 (y, ) def mydef(mydefarg): print("mda is", mydefarg) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, set(['y'])) eq_(parsed.declared_identifiers, set(['mydef', 'x']))
def test_locate_identifiers_15(self): code = """ def t1((x,y)): return x+5, y+4 t2 = lambda (x,y):(x+5, y+4) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set(['t1', 't2'])) eq_(parsed.undeclared_identifiers, set())
def test_locate_identifiers_13(self): code = """ def foo(): class Bat(object): pass Bat """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set(['foo'])) eq_(parsed.undeclared_identifiers, set())
def test_locate_identifiers_8(self): code = """ class Hi(object): foo = 7 def hoho(self): x = 5 """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set(['Hi'])) eq_(parsed.undeclared_identifiers, set())
def undeclared_identifiers(self): res = [] for c in self.function_decl.defaults: res += list( ast.PythonCode(c, **self.exception_kwargs).undeclared_identifiers) return (set(res).union( self.filter_args.undeclared_identifiers.difference( filters.DEFAULT_ESCAPES.keys())).union( self.expression_undeclared_identifiers).difference( self.function_decl.allargnames))
def test_locate_identifiers_14(self): code = """ def foo(): class Bat(object): pass Bat print(Bat) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set(["foo"])) eq_(parsed.undeclared_identifiers, set(["Bat"]))
def test_locate_identifiers_3(self): """test that combination assignment/expressions of the same identifier log the ident as 'undeclared'""" code = """ x = x + 5 for y in range(1, y): ("hi",) [z for z in range(1, z)] (q for q in range (1, q)) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, set(['x', 'y', 'z', 'q', 'range']))
def test_locate_identifiers_2(self): code = """ import foobar from lala import hoho, yaya import bleep as foo result = [] data = get_data() for x in data: result.append(x+7) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, set(['get_data'])) eq_(parsed.declared_identifiers, set(['result', 'data', 'x', 'hoho', 'foobar', 'foo', 'yaya']))