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

changeset 7613
382f89c11e27
parent 7612
ca1ce1e0fcff
child 7614
646742c260bd
equal deleted inserted replaced
7612:ca1ce1e0fcff 7613:382f89c11e27
14 # 14 #
15 # SPDX-License-Identifier: Apache-2.0 15 # SPDX-License-Identifier: Apache-2.0
16 # 16 #
17 17
18 import ast 18 import ast
19 import copy
19 import sys 20 import sys
20 21
21 from . import SecurityUtils 22 from . import SecurityUtils
22 23
23 24
35 @param contextObject context dictionary to be used to populate the 36 @param contextObject context dictionary to be used to populate the
36 class 37 class
37 @type dict 38 @type dict
38 """ 39 """
39 if contextObject is not None: 40 if contextObject is not None:
40 self.__context = contextObject 41 self.__context = copy.copy(contextObject)
41 else: 42 else:
42 self.__context = {} 43 self.__context = {}
43 44
44 def __repr__(self): 45 def __repr__(self):
45 """ 46 """
46 Private method to generate representation of object for printing or 47 Special method to generate representation of object for printing or
47 interactive use. 48 interactive use.
48 49
49 @return string representation of the object 50 @return string representation of the object
50 @rtype str 51 @rtype str
51 """ 52 """
77 Public method to get the number of args a function call has. 78 Public method to get the number of args a function call has.
78 79
79 @return number of args a function call has 80 @return number of args a function call has
80 @rtype int 81 @rtype int
81 """ 82 """
82 if 'call' in self.__context and hasattr(self.__context['call'], 'args'): 83 if (
84 'call' in self.__context and
85 hasattr(self.__context['call'], 'args')
86 ):
83 return len(self.__context['call'].args) 87 return len(self.__context['call'].args)
84 else: 88 else:
85 return None 89 return None
86 90
87 @property 91 @property
229 233
230 elif isinstance(literal, ast.Str): 234 elif isinstance(literal, ast.Str):
231 literalValue = literal.s 235 literalValue = literal.s
232 236
233 elif isinstance(literal, ast.List): 237 elif isinstance(literal, ast.List):
234 returnList = list() 238 returnList = []
235 for li in literal.elts: 239 for li in literal.elts:
236 returnList.append(self.__getLiteralValue(li)) 240 returnList.append(self.__getLiteralValue(li))
237 literalValue = returnList 241 literalValue = returnList
238 242
239 elif isinstance(literal, ast.Tuple): 243 elif isinstance(literal, ast.Tuple):
240 returnTuple = tuple() 244 returnTuple = ()
241 for ti in literal.elts: 245 for ti in literal.elts:
242 returnTuple = returnTuple + (self.__getLiteralValue(ti),) 246 returnTuple = returnTuple + (self.__getLiteralValue(ti),)
243 literalValue = returnTuple 247 literalValue = returnTuple
244 248
245 elif isinstance(literal, ast.Set): 249 elif isinstance(literal, ast.Set):
289 @rtype Any 293 @rtype Any
290 """ 294 """
291 kwdValues = self.callKeywords 295 kwdValues = self.callKeywords
292 if kwdValues is not None and argumentName in kwdValues: 296 if kwdValues is not None and argumentName in kwdValues:
293 return kwdValues[argumentName] 297 return kwdValues[argumentName]
298
299 return None
294 300
295 def checkCallArgValue(self, argumentName, argumentValues=None): 301 def checkCallArgValue(self, argumentName, argumentValues=None):
296 """ 302 """
297 Public method to check for a value of a named argument in a function 303 Public method to check for a value of a named argument in a function
298 call. 304 call.
307 """ 313 """
308 argValue = self.getCallArgValue(argumentName) 314 argValue = self.getCallArgValue(argumentName)
309 if argValue is not None: 315 if argValue is not None:
310 if not isinstance(argumentValues, list): 316 if not isinstance(argumentValues, list):
311 # if passed a single value, or a tuple, convert to a list 317 # if passed a single value, or a tuple, convert to a list
312 argumentValues = list((argumentValues,)) 318 argumentValues = [argumentValues]
313 for val in argumentValues: 319 for val in argumentValues:
314 if argValue == val: 320 if argValue == val:
315 return True 321 return True
316 return False 322 return False
317 else: 323 else:
330 """ 336 """
331 if hasattr(self.node, 'keywords'): 337 if hasattr(self.node, 'keywords'):
332 for key in self.node.keywords: 338 for key in self.node.keywords:
333 if key.arg == argumentName: 339 if key.arg == argumentName:
334 return key.value.lineno 340 return key.value.lineno
341
342 return -1
343
344 def getOffsetForCallArg(self, argumentName):
345 """
346 Public method to get the offset for a specific named argument.
347
348 @param argumentName name of the argument to get the line number for
349 @type str
350 @return offset of the found argument or -1
351 @rtype int
352 """
353 if hasattr(self.node, 'keywords'):
354 for key in self.node.keywords:
355 if key.arg == argumentName:
356 return key.value.col_offset
357
358 return -1
335 359
336 def getCallArgAtPosition(self, positionNum): 360 def getCallArgAtPosition(self, positionNum):
337 """ 361 """
338 Public method to get a positional argument at the specified position 362 Public method to get a positional argument at the specified position
339 (if it exists). 363 (if it exists).

eric ide

mercurial