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

changeset 7615
ca2949b1a29a
parent 7613
382f89c11e27
child 7622
384e2aa5c073
equal deleted inserted replaced
7614:646742c260bd 7615:ca2949b1a29a
256 @type bytes 256 @type bytes
257 @return escaped bytes object 257 @return escaped bytes object
258 @rtype bytes 258 @rtype bytes
259 """ 259 """
260 return b.decode('unicode_escape').encode('unicode_escape') 260 return b.decode('unicode_escape').encode('unicode_escape')
261
262
263 def concatString(node, stop=None):
264 """
265 Function to build a string from an ast.BinOp chain.
266
267 This will build a string from a series of ast.Str nodes wrapped in
268 ast.BinOp nodes. Something like "a" + "b" + "c" or "a %s" % val etc.
269 The provided node can be any participant in the BinOp chain.
270
271 @param node node to be processed
272 @type ast.BinOp or ast.Str
273 @param stop base node to stop at
274 @type ast.BinOp or ast.Str
275 @return tuple containing the root node of the expression and the string
276 value
277 @rtype tuple of (ast.AST, str)
278 """
279 def _get(node, bits, stop=None):
280 if node != stop:
281 bits.append(
282 _get(node.left, bits, stop)
283 if isinstance(node.left, ast.BinOp)
284 else node.left
285 )
286 bits.append(
287 _get(node.right, bits, stop)
288 if isinstance(node.right, ast.BinOp)
289 else node.right
290 )
291
292 bits = [node]
293 while isinstance(node._securityParent, ast.BinOp):
294 node = node._securityParent
295 if isinstance(node, ast.BinOp):
296 _get(node, bits, stop)
297
298 return (
299 node,
300 " ".join([x.s for x in bits if isinstance(x, ast.Str)])
301 )
302
303
304 def getCalledName(node):
305 """
306 Function to get the function name from an ast.Call node.
307
308 An ast.Call node representing a method call will present differently to one
309 wrapping a function call: thing.call() vs call(). This helper will grab the
310 unqualified call name correctly in either case.
311
312 @param node reference to the call node
313 @type ast.Call
314 @return function name of the node
315 @rtype str
316 """
317 func = node.func
318 try:
319 return func.attr if isinstance(func, ast.Attribute) else func.id
320 except AttributeError:
321 return ""
322
323 #
324 # eflag: noqa = M601

eric ide

mercurial