eric6/Plugins/CheckerPlugins/CodeStyleChecker/Security/SecurityUtils.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8243
cc717c2ae956
equal deleted inserted replaced
8190:fb0ef164f536 8273:698ae46f40a4
7 Module implementing utility functions used by the security checks. 7 Module implementing utility functions used by the security checks.
8 """ 8 """
9 9
10 import ast 10 import ast
11 import os 11 import os
12 import contextlib
12 13
13 import AstUtilities 14 import AstUtilities
14 15
15 16
16 class InvalidModulePath(Exception): 17 class InvalidModulePath(Exception):
152 @return qualified attribute name 153 @return qualified attribute name
153 @rtype str 154 @rtype str
154 """ 155 """
155 prefix = "" 156 prefix = ""
156 if isinstance(node, ast.Attribute): 157 if isinstance(node, ast.Attribute):
157 try: 158 with contextlib.suppress(Exception):
158 val = deepgetattr(node, 'value.id') 159 val = deepgetattr(node, 'value.id')
159 if val in aliases: 160 prefix = (
160 prefix = aliases[val] 161 aliases[val] if val in aliases
161 else: 162 else deepgetattr(node, 'value.id')
162 prefix = deepgetattr(node, 'value.id') 163 )
163 except Exception: # secok 164 # Id we can't get the fully qualified name for an attr, just return
164 # We can't get the fully qualified name for an attr, just return
165 # its base name. 165 # its base name.
166 pass
167 166
168 return "{0}.{1}".format(prefix, node.attr) 167 return "{0}.{1}".format(prefix, node.attr)
169 else: 168 else:
170 return "" 169 return ""
171 170
193 @param node node to extract a line range from 192 @param node node to extract a line range from
194 @type ast.AST 193 @type ast.AST
195 @return list containing the line number range 194 @return list containing the line number range
196 @rtype list of int 195 @rtype list of int
197 """ 196 """
198 strip = {"body": None, "orelse": None, 197 strip = {
199 "handlers": None, "finalbody": None} 198 "body": None,
200 for key in strip.keys(): 199 "orelse": None,
200 "handlers": None,
201 "finalbody": None
202 }
203 for key in strip:
201 if hasattr(node, key): 204 if hasattr(node, key):
202 strip[key] = getattr(node, key) 205 strip[key] = getattr(node, key)
203 node.key = [] 206 node.key = []
204 207
205 lines_min = 9999999999 208 lines_min = 9999999999
207 for n in ast.walk(node): 210 for n in ast.walk(node):
208 if hasattr(n, 'lineno'): 211 if hasattr(n, 'lineno'):
209 lines_min = min(lines_min, n.lineno) 212 lines_min = min(lines_min, n.lineno)
210 lines_max = max(lines_max, n.lineno) 213 lines_max = max(lines_max, n.lineno)
211 214
212 for key in strip.keys(): 215 for key in strip:
213 if strip[key] is not None: 216 if strip[key] is not None:
214 node.key = strip[key] 217 node.key = strip[key]
215 218
216 if lines_max > -1: 219 if lines_max > -1:
217 return list(range(lines_min, lines_max + 1)) 220 return list(range(lines_min, lines_max + 1))

eric ide

mercurial