[] active = first for lineno, line in enumerate(fileobj): if line.strip() == '----': if active is second: raise InvalidTemplate( 'Line %d: separator already seen' % lineno) active = second else: active.append(line) viewcode_str = ''.join(first) viewcode, viewglobals = self.compile(viewcode_str, fileobj.name) def viewfunc(viewlocals): exec viewcode in viewglobals, viewlocals return viewlocals return viewfunc
filename, 'exec') viewglobals = {} for stmt in compiler.parse(viewcode_str).node: # if ... elif isinstance(stmt, From): fromlist = [x[0] for x in stmt.names] mod = __import__(stmt.modname, {}, {}, fromlist) for name, asname in stmt.names: if name == '*': for asname in getattr(mod, '__all__', dir(mod)): viewglobals[asname] = getattr(mod, asname) else: if asname is None: asname = name viewglobals[asname] = getattr(mod, name) return viewcode, viewglobals