# eric7.Plugins.CheckerPlugins.CodeStyleChecker.Security.SecurityUtils

Module implementing utility functions used by the security checks.

### Global Attributes

None

### Classes

InvalidModulePath | Class defining an exception for invalid module paths.

### Functions

_get |
concatString | Function to build a string from an ast.BinOp chain.
deepgetattr | Function to recurs through an attribute chain to get the ultimate value.
escapedBytesRepresentation | Function to escape bytes for comparison with other strings.
getAttrQualName | Function to get a the full name for the attribute node.
getCallName | Function to extract the call name from an ast.Call node.
getCalledName | Function to get the function name from an ast.Call node.
getModuleQualnameFromPath | Function to get the module's qualified name by analysis of the path.
getQualAttr | Function to extract the qualified name from an ast.Attribute node.
linerange | Function to get line number range from a node.
linerange_fix | Function to get a line number range working around a known Python bug with multi-line strings.
namespacePathJoin | Function to extend a given namespace path.
namespacePathSplit | Function to split a namespace path into a head and tail.

## InvalidModulePath

Class defining an exception for invalid module paths.

### Derived from

Exception

### Class Attributes

None

### Class Methods

None

### Methods

None

### Static Methods

None

Up

## _get

_get(node, bits, stop=None)
Up

## concatString

concatString(node, stop=None)

Function to build a string from an ast.BinOp chain.

This will build a string from a series of ast.Str/ast.Constant nodes wrapped in ast.BinOp nodes. Something like "a" + "b" + "c" or "a %s" % val etc. The provided node can be any participant in the BinOp chain.

node (ast.BinOp or ast.Str/ast.Constant)
  node to be processed
stop (ast.BinOp or ast.Str/ast.Constant)
  base node to stop at

Return:
  tuple containing the root node of the expression and the string value

Return Type:
  tuple of (ast.AST, str)

Up

## deepgetattr

deepgetattr(obj, attr)

Function to recurs through an attribute chain to get the ultimate value.

obj (ast.Name or ast.Attribute)
  reference to the object to be recursed
attr (ast.Attribute)
  attribute chain to be parsed

Return:
  ultimate value

Return Type:
  ast.AST

Up

## escapedBytesRepresentation

escapedBytesRepresentation(b)

Function to escape bytes for comparison with other strings.

In practice it turns control characters into acceptable codepoints then encodes them into bytes again to turn unprintable bytes into printable escape sequences.

This is safe to do for the whole range 0..255 and result matches unicode_escape on a unicode string.

b (bytes)
  bytes object to be escaped

Return:
  escaped bytes object

Return Type:
  bytes

Up

## getAttrQualName

getAttrQualName(node, aliases)

Function to get a the full name for the attribute node.

This will resolve a pseudo-qualified name for the attribute rooted at node as long as all the deeper nodes are Names or Attributes. This will give you how the code referenced the name but will not tell you what the name actually refers to. If we encounter a node without a static name we punt with an empty string. If this encounters something more complex, such as foo.mylist[0](a,b) we just return empty string.

node (ast.Attribute)
  attribute node to be treated
aliases (dict)
  dictionary of import aliases

Return:
  qualified name of the attribute

Return Type:
  str

Up

## getCallName

getCallName(node, aliases)

Function to extract the call name from an ast.Call node.

node (ast.Call)
  node to extract information from
aliases (dict)
  dictionary of import aliases

Return:
  name of the ast.Call node

Return Type:
  str

Up

## getCalledName

getCalledName(node)

Function to get the function name from an ast.Call node.

An ast.Call node representing a method call will present differently to one wrapping a function call: thing.call() vs call(). This helper will grab the unqualified call name correctly in either case.

node (ast.Call)
  reference to the call node

Return:
  function name of the node

Return Type:
  str

Up

## getModuleQualnameFromPath

getModuleQualnameFromPath(path)

Function to get the module's qualified name by analysis of the path.

Resolve the absolute pathname and eliminate symlinks. This could result in an incorrect name if symlinks are used to restructure the python lib directory.

Starting from the right-most directory component look for __init__.py in the directory component. If it exists then the directory name is part of the module name. Move left to the subsequent directory components until a directory is found without __init__.py.

path (str)
  path of the module to be analyzed

Return:
  qualified name of the module

Return Type:
  str

Raises InvalidModulePath:
  raised to indicate an invalid module path

Up

## getQualAttr

getQualAttr(node, aliases)

Function to extract the qualified name from an ast.Attribute node.

node (ast.Attribute)
  node to extract information from
aliases (dict)
  dictionary of import aliases

Return:
  qualified attribute name

Return Type:
  str

Up

## linerange

linerange(node)

Function to get line number range from a node.

node (ast.AST)
  node to extract a line range from

Return:
  list containing the line number range

Return Type:
  list of int

Up

## linerange_fix

linerange_fix(node)

Function to get a line number range working around a known Python bug with multi-line strings.

node (ast.AST)
  node to extract a line range from

Return:
  list containing the line number range

Return Type:
  list of int

Up

## namespacePathJoin

namespacePathJoin(namespace, name)

Function to extend a given namespace path.

namespace (str)
  namespace to be extended
name (str)
  node name to be appended

Return:
  extended namespace

Return Type:
  str

Up

## namespacePathSplit

namespacePathSplit(path)

Function to split a namespace path into a head and tail.

Tail will be the last namespace path component and head will be everything leading up to that in the path. This is similar to os.path.split.

path (str)
  namespace path to be split

Return:
  tuple containing the namespace path head and tail

Return Type:
  tuple of (str, str)

Up