Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/mako/pyparser.py: 76%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

139 statements  

1# mako/pyparser.py 

2# Copyright 2006-2025 the Mako authors and contributors <see AUTHORS file> 

3# 

4# This module is part of Mako and is released under 

5# the MIT License: http://www.opensource.org/licenses/mit-license.php 

6 

7"""Handles parsing of Python code. 

8 

9Parsing to AST is done via _ast on Python > 2.5, otherwise the compiler 

10module is used. 

11""" 

12 

13import operator 

14 

15import _ast 

16 

17from mako import _ast_util 

18from mako import compat 

19from mako import exceptions 

20from mako import util 

21 

22# words that cannot be assigned to (notably 

23# smaller than the total keys in __builtins__) 

24reserved = {"True", "False", "None", "print"} 

25 

26# the "id" attribute on a function node 

27arg_id = operator.attrgetter("arg") 

28 

29util.restore__ast(_ast) 

30 

31 

32def parse(code, mode="exec", **exception_kwargs): 

33 """Parse an expression into AST""" 

34 

35 try: 

36 return _ast_util.parse(code, "<unknown>", mode) 

37 except Exception as e: 

38 raise exceptions.SyntaxException( 

39 "(%s) %s (%r)" 

40 % ( 

41 compat.exception_as().__class__.__name__, 

42 compat.exception_as(), 

43 code[0:50], 

44 ), 

45 **exception_kwargs, 

46 ) from e 

47 

48 

49class FindIdentifiers(_ast_util.NodeVisitor): 

50 def __init__(self, listener, **exception_kwargs): 

51 self.in_function = False 

52 self.in_assign_targets = False 

53 self.local_ident_stack = set() 

54 self.listener = listener 

55 self.exception_kwargs = exception_kwargs 

56 

57 def _add_declared(self, name): 

58 if not self.in_function: 

59 self.listener.declared_identifiers.add(name) 

60 else: 

61 self.local_ident_stack.add(name) 

62 

63 def visit_ClassDef(self, node): 

64 self._add_declared(node.name) 

65 

66 def visit_Assign(self, node): 

67 # flip around the visiting of Assign so the expression gets 

68 # evaluated first, in the case of a clause like "x=x+5" (x 

69 # is undeclared) 

70 

71 self.visit(node.value) 

72 in_a = self.in_assign_targets 

73 self.in_assign_targets = True 

74 for n in node.targets: 

75 self.visit(n) 

76 self.in_assign_targets = in_a 

77 

78 def visit_ExceptHandler(self, node): 

79 if node.name is not None: 

80 self._add_declared(node.name) 

81 if node.type is not None: 

82 self.visit(node.type) 

83 for statement in node.body: 

84 self.visit(statement) 

85 

86 def visit_Lambda(self, node, *args): 

87 self._visit_function(node, True) 

88 

89 def visit_FunctionDef(self, node): 

90 self._add_declared(node.name) 

91 self._visit_function(node, False) 

92 

93 def visit_ListComp(self, node): 

94 if self.in_function: 

95 for comp in node.generators: 

96 self.visit(comp.target) 

97 self.visit(comp.iter) 

98 else: 

99 self.generic_visit(node) 

100 

101 visit_SetComp = visit_GeneratorExp = visit_ListComp 

102 

103 def visit_DictComp(self, node): 

104 if self.in_function: 

105 for comp in node.generators: 

106 self.visit(comp.target) 

107 self.visit(comp.iter) 

108 else: 

109 self.generic_visit(node) 

110 

111 def _expand_tuples(self, args): 

112 for arg in args: 

113 if isinstance(arg, _ast.Tuple): 

114 yield from arg.elts 

115 else: 

116 yield arg 

117 

118 def _visit_function(self, node, islambda): 

119 # push function state onto stack. dont log any more 

120 # identifiers as "declared" until outside of the function, 

121 # but keep logging identifiers as "undeclared". track 

122 # argument names in each function header so they arent 

123 # counted as "undeclared" 

124 

125 inf = self.in_function 

126 self.in_function = True 

127 

128 local_ident_stack = self.local_ident_stack 

129 self.local_ident_stack = local_ident_stack.union( 

130 [arg_id(arg) for arg in self._expand_tuples(node.args.args)] 

131 ) 

132 if islambda: 

133 self.visit(node.body) 

134 else: 

135 for n in node.body: 

136 self.visit(n) 

137 self.in_function = inf 

138 self.local_ident_stack = local_ident_stack 

139 

140 def visit_For(self, node): 

141 # flip around visit 

142 

143 self.visit(node.iter) 

144 self.visit(node.target) 

145 for statement in node.body: 

146 self.visit(statement) 

147 for statement in node.orelse: 

148 self.visit(statement) 

149 

150 def visit_Name(self, node): 

151 if isinstance(node.ctx, _ast.Store): 

152 # this is eqiuvalent to visit_AssName in 

153 # compiler 

154 self._add_declared(node.id) 

155 elif ( 

156 node.id not in reserved 

157 and node.id not in self.listener.declared_identifiers 

158 and node.id not in self.local_ident_stack 

159 ): 

160 self.listener.undeclared_identifiers.add(node.id) 

161 

162 def visit_Import(self, node): 

163 for name in node.names: 

164 if name.asname is not None: 

165 self._add_declared(name.asname) 

166 else: 

167 self._add_declared(name.name.split(".")[0]) 

168 

169 def visit_ImportFrom(self, node): 

170 for name in node.names: 

171 if name.asname is not None: 

172 self._add_declared(name.asname) 

173 elif name.name == "*": 

174 raise exceptions.CompileException( 

175 "'import *' is not supported, since all identifier " 

176 "names must be explicitly declared. Please use the " 

177 "form 'from <modulename> import <name1>, <name2>, " 

178 "...' instead.", 

179 **self.exception_kwargs, 

180 ) 

181 else: 

182 self._add_declared(name.name) 

183 

184 

185class FindTuple(_ast_util.NodeVisitor): 

186 def __init__(self, listener, code_factory, **exception_kwargs): 

187 self.listener = listener 

188 self.exception_kwargs = exception_kwargs 

189 self.code_factory = code_factory 

190 

191 def visit_Tuple(self, node): 

192 for n in node.elts: 

193 p = self.code_factory(n, **self.exception_kwargs) 

194 self.listener.codeargs.append(p) 

195 self.listener.args.append(ExpressionGenerator(n).value()) 

196 ldi = self.listener.declared_identifiers 

197 self.listener.declared_identifiers = ldi.union( 

198 p.declared_identifiers 

199 ) 

200 lui = self.listener.undeclared_identifiers 

201 self.listener.undeclared_identifiers = lui.union( 

202 p.undeclared_identifiers 

203 ) 

204 

205 

206class ParseFunc(_ast_util.NodeVisitor): 

207 def __init__(self, listener, **exception_kwargs): 

208 self.listener = listener 

209 self.exception_kwargs = exception_kwargs 

210 

211 def visit_FunctionDef(self, node): 

212 self.listener.funcname = node.name 

213 

214 argnames = [arg_id(arg) for arg in node.args.args] 

215 if node.args.vararg: 

216 argnames.append(node.args.vararg.arg) 

217 

218 kwargnames = [arg_id(arg) for arg in node.args.kwonlyargs] 

219 if node.args.kwarg: 

220 kwargnames.append(node.args.kwarg.arg) 

221 self.listener.argnames = argnames 

222 self.listener.defaults = node.args.defaults # ast 

223 self.listener.kwargnames = kwargnames 

224 self.listener.kwdefaults = node.args.kw_defaults 

225 self.listener.varargs = node.args.vararg 

226 self.listener.kwargs = node.args.kwarg 

227 

228 

229class ExpressionGenerator: 

230 def __init__(self, astnode): 

231 self.generator = _ast_util.SourceGenerator(" " * 4) 

232 self.generator.visit(astnode) 

233 

234 def value(self): 

235 return "".join(self.generator.result)