def test_attemptToConvertGetattrToFqn_works(self): ast = Getattr(Getattr(Name("foo"),"bah"),"hello") assert attemptToConvertGetattrToFqn(ast) == "foo.bah.hello"
def createGetattr(self,fqn): node = Name(fqn[0]) for name in fqn.split(".")[1:]: node = Getattr(node,name) return node
def test_attemptToConvertGetattrToFqn_returnsNoneIfFails(self): ast = Getattr(CallFunc(Name("foo"),[],[],[]),"hello") assert attemptToConvertGetattrToFqn(ast) is None
def __get_ast(self): from compiler.ast import Module, Stmt, Assign, AssName, Const, Function, For, Getattr,\ TryFinally, TryExcept, If, Import, AssAttr, Name, CallFunc,\ Class, Compare, Raise, And, Mod, Tuple, Pass, Not, Exec, List,\ Discard, Keyword, Return, Dict, Break, AssTuple, Subscript,\ Printnl, From, Lambda preppyNodes = self.__parse() if self._defSeen == 1: fixargs = self._fnc_argnames defaults = list(self._fnc_defaults) if self._fnc_kwargs: spargs = [fixargs[-1]] fixargs = fixargs[:-1] else: spargs = ['__kwds__'] if self._fnc_varargs: spargs.insert(0, fixargs[-1]) fixargs = fixargs[:-1] kwargs = fixargs[-len(defaults):] fixargs = fixargs[:-len(defaults)] flags = self._fnc_flags #construct the getOutput function nodes = [ Assign([AssName('__lquoteFunc__', 'OP_ASSIGN')], CallFunc(Getattr(Name(spargs[-1]), 'setdefault'), [Const('__lquoteFunc__'), Name('str')], None, None)), Discard( CallFunc(Getattr(Name(spargs[-1]), 'pop'), [Const('__lquoteFunc__')], None, None)), Assign([AssName('__quoteFunc__', 'OP_ASSIGN')], CallFunc(Getattr(Name(spargs[-1]), 'setdefault'), [Const('__quoteFunc__'), Name('str')], None, None)), Discard( CallFunc(Getattr(Name(spargs[-1]), 'pop'), [Const('__quoteFunc__')], None, None)) ] if not self._fnc_kwargs: nodes += [ If([(Name(spargs[-1]), Stmt([ Raise( CallFunc(Name('TypeError'), [ Const('get: unexpected keyword arguments') ], None, None), None, None) ]))], None) ] nodes += [ Assign([AssName('__append__', 'OP_ASSIGN')], Getattr(List(()), 'append')), Assign([AssName('__write__', 'OP_ASSIGN')], Lambda(['x'], [], 0, CallFunc(Name('__append__'), [ CallFunc(Name('__lquoteFunc__'), [Name('x')], None, None) ], None, None))), Assign([AssName('__swrite__', 'OP_ASSIGN')], Lambda(['x'], [], 0, CallFunc(Name('__append__'), [ CallFunc(Name('__quoteFunc__'), [Name('x')], None, None) ], None, None))) ] for n in nodes: _denumber(n, self._fnc_lineno) preppyNodes = nodes + preppyNodes + [ Return( CallFunc(Getattr(Const(''), 'join'), [Getattr(Name('__append__'), '__self__')], None, None)) ] argnames = list(fixargs) + list(kwargs) + list(spargs) FA = ('get', argnames, defaults, flags | 8, None, Stmt(preppyNodes)) global _newPreambleAst if not _newPreambleAst: _newPreambleAst = self._cparse(_newPreamble).node.nodes map(_denumber, _newPreambleAst) extraAst = _newPreambleAst else: global _preambleAst, _localizer if not _preambleAst: _preambleAst = self._cparse(_preamble).node.nodes map(_denumber, _preambleAst) _localizer = [ Assign([AssName('__d__', 'OP_ASSIGN')], Name('dictionary')), Discard( CallFunc( Getattr(CallFunc(Name('globals'), [], None, None), 'update'), [Name('__d__')], None, None)) ] preppyNodes = _localizer + preppyNodes FA = ('__code__', [ 'dictionary', 'outputfile', '__write__', '__swrite__', '__save_sys_stdout__' ], (), 0, None, Stmt(preppyNodes)) extraAst = _preambleAst if sys.hexversion >= 0x2040000: FA = (None, ) + FA return Module( self.filename, Stmt([ Assign([AssName('__checksum__', 'OP_ASSIGN')], Const(getattr(self, 'sourcechecksum'))), Function(*FA), ] + extraAst))