Ejemplo n.º 1
0
    def convert_leaf(self, grammar, type, value, prefix, start_pos):
        #print('leaf', value, pytree.type_repr(type))
        if type == tokenize.NAME:
            if value in grammar.keywords:
                if value in ('def', 'class', 'lambda'):
                    self._scope_names_stack.append({})

                return pt.Keyword(self.position_modifier, value, start_pos,
                                  prefix)
            else:
                name = pt.Name(self.position_modifier, value, start_pos,
                               prefix)
                # Keep a listing of all used names
                arr = self._used_names.setdefault(name.value, [])
                arr.append(name)
                arr = self._scope_names_stack[-1].setdefault(name.value, [])
                arr.append(name)
                return name
        elif type == STRING:
            return pt.String(self.position_modifier, value, start_pos, prefix)
        elif type == NUMBER:
            return pt.Number(self.position_modifier, value, start_pos, prefix)
        elif type in (NEWLINE, ENDMARKER):
            return pt.Whitespace(self.position_modifier, value, start_pos,
                                 prefix)
        else:
            return pt.Operator(self.position_modifier, value, start_pos,
                               prefix)
Ejemplo n.º 2
0
def get_faked(module, obj, name=None):
    obj = obj.__class__ if is_class_instance(obj) else obj
    result = _faked(module, obj, name)
    # TODO may this ever happen? result None? if so, document!
    if not isinstance(result, pt.Class) and result is not None:
        # Set the docstr which was previously not set (faked modules don't
        # contain it).
        doc = '"""%s"""' % obj.__doc__  # TODO need escapes.
        suite = result.children[-1]
        string = pt.String(pt.zero_position_modifier, doc, (0, 0), '')
        new_line = pt.Whitespace('\n', (0, 0), '')
        docstr_node = pt.Node('simple_stmt', [string, new_line])
        suite.children.insert(2, docstr_node)
        return result
Ejemplo n.º 3
0
def get_faked(module, obj, name=None):
    obj = obj.__class__ if is_class_instance(obj) else obj
    result = _faked(module, obj, name)
    if result is None or isinstance(result, pt.Class):
        # We're not interested in classes. What we want is functions.
        return None
    else:
        # Set the docstr which was previously not set (faked modules don't
        # contain it).
        doc = '"""%s"""' % obj.__doc__  # TODO need escapes.
        suite = result.children[-1]
        string = pt.String(pt.zero_position_modifier, doc, (0, 0), '')
        new_line = pt.Whitespace('\n', (0, 0), '')
        docstr_node = pt.Node('simple_stmt', [string, new_line])
        suite.children.insert(2, docstr_node)
        return result