Пример #1
0
def _check_node(node):
    """
  <Purpose>
    Examines a node, its attributes, and all of its children (recursively) for
    safety. A node is safe if it is in _NODE_CLASS_OK and an attribute is safe
    if it is not a unicode string and either in _NODE_ATTR_OK or is safe as is 
    defined by _is_string_safe()
  
  <Arguments>
    node: A node in an AST
    
  <Exceptions>
    CheckNodeException if an unsafe node is used
    CheckStrException if an attribute has an unsafe string 
  
  <Return>
    None
  """
    # Subtract length of encoding header from traceback line numbers,
    # see SeattleTestbed/repy_v2#95.
    HEADERSIZE = len(encoding_header.ENCODING_DECLARATION.splitlines())

    # Proceed with the node check.

    if node.__class__.__name__ not in _NODE_CLASS_OK:
        raise exception_hierarchy.CheckNodeException(
            "Unsafe call '" + str(node.__class__.__name__) + "' in line " +
            str(node.lineno - HEADERSIZE))

    for attribute, value in node.__dict__.iteritems():
        # Don't allow the construction of unicode literals
        if type(value) == unicode:
            raise exception_hierarchy.CheckStrException(
                "Unsafe string '" + str(value) + "' in line " +
                str(node.lineno - HEADERSIZE) + ", node attribute '" +
                str(attribute) + "'")

        if attribute in _NODE_ATTR_OK:
            continue

        # JAC: don't check doc strings for __ and the like...,
        # see SeattleTestbed/repy_v1#107.
        if attribute == 'doc' and (node.__class__.__name__
                                   in ['Module', 'Function', 'Class']):
            continue

        # Check the safety of any strings
        if not _is_string_safe(value):
            raise exception_hierarchy.CheckStrException(
                "Unsafe string '" + str(value) + "' in line " +
                str(node.lineno - HEADERSIZE) + ", node attribute '" +
                str(attribute) + "'")

    for child in node.getChildNodes():
        _check_node(child)
Пример #2
0
def _check_node(node):
    if node.__class__.__name__ not in _NODE_CLASS_OK:
        raise exception_hierarchy.CheckNodeException(node.lineno,
                                                     node.__class__.__name__)
    for k, v in node.__dict__.items():
        # Don't allow the construction of unicode literals
        if type(v) == unicode:
            raise exception_hierarchy.CheckStrException(node.lineno, k, v)

        if k in _NODE_ATTR_OK: continue

        # Check the safety of any strings
        if not _is_string_safe(v):
            raise exception_hierarchy.CheckStrException(node.lineno, k, v)

    for child in node.getChildNodes():
        _check_node(child)
Пример #3
0
def _check_node(node):
    """
  <Purpose>
    Examines a node, its attributes, and all of its children (recursively) for safety.
    A node is safe if it is in _NODE_CLASS_OK and an attribute is safe if it is
    not a unicode string and either in _NODE_ATTR_OK or is safe as is 
    defined by _is_string_safe()
  
  <Arguments>
    node: A node in an AST
    
  <Exceptions>
    CheckNodeException if an unsafe node is used
    CheckStrException if an attribute has an unsafe string 
  
  <Return>
    None
  """
    if node.__class__.__name__ not in _NODE_CLASS_OK:
        raise exception_hierarchy.CheckNodeException(node.lineno,
                                                     node.__class__.__name__)

    for attribute, value in node.__dict__.iteritems():
        # Don't allow the construction of unicode literals
        if type(value) == unicode:
            raise exception_hierarchy.CheckStrException(
                node.lineno, attribute, value)

        if attribute in _NODE_ATTR_OK:
            continue

        # JAC: don't check doc strings for __ and the like... (#889)
        if attribute == 'doc' and (node.__class__.__name__
                                   in ['Module', 'Function', 'Class']):
            continue

        # Check the safety of any strings
        if not _is_string_safe(value):
            raise exception_hierarchy.CheckStrException(
                node.lineno, attribute, value)

    for child in node.getChildNodes():
        _check_node(child)
Пример #4
0
def _check_node(node):
  """
  <Purpose>
  Examines a node, its attributes, and all of its children (recursively) for
  safety. A node is safe if it is in _NODE_CLASS_OK and an attribute is safe
  if it is not a unicode string and either in _NODE_ATTR_OK or is safe as is 
  defined by _is_string_safe()
  
  <Arguments>
  node: A node in an AST
  
  <Exceptions>
  CheckNodeException if an unsafe node is used
  CheckStrException if an attribute has an unsafe string 
  
  <Return>
  None
  """
  if node.__class__.__name__ not in _NODE_CLASS_OK:
    raise exception_hierarchy.CheckNodeException("Unsafe call '" +
        str(node.__class__.__name__) + "' in line " + str(node.lineno))

  for attribute, value in node.__dict__.items():
  # Don't allow the construction of unicode literals
  # if type(value) == unicode:
  #   raise exception_hierarchy.CheckStrException("Unsafe string '" +
  #       str(value) + "' in line " + str(node.lineno) +
  #       ", node attribute '" + str(attribute) + "'")

    if attribute in _NODE_ATTR_OK: 
      continue

    # JAC: don't check doc strings for __ and the like...,
    # see SeattleTestbed/repy_v1#107.
    if attribute == 'doc' and (node.__class__.__name__ in
      ['Module', 'Function', 'Class']):
      continue

    # Check the safety of any strings
    if not _is_string_safe(value):
      raise exception_hierarchy.CheckStrException("Unsafe string '" +
        str(value) + "' in line " + str(node.lineno) +
        ", node attribute '" + str(attribute) + "'")

  if 'getChildNodes' in dir(node):
    for child in node.getChildNodes():
      _check_node(child)
  else:
    for child in compiler.iter_child_nodes(node):
      _check_node(child)