'Find all assert statements in *mod* and rewrite them.'

def run(self, mod):

    if (not mod.body):
        return
    aliases = [ast.alias(py.builtin.builtins.__name__, '@py_builtins'), ast.alias('_pytest.assertion.rewrite', '@pytest_ar')]
    expect_docstring = True
    pos = 0
    lineno = 0
    for item in mod.body:
        if (expect_docstring and isinstance(item, ast.Expr) and isinstance(item.value, ast.Str)):
            doc = item.value.s
            if ('PYTEST_DONT_REWRITE' in doc):
                return
            lineno += (len(doc) - 1)
            expect_docstring = False
        elif ((not isinstance(item, ast.ImportFrom)) or (item.level > 0) or (item.module != '__future__')):
            lineno = item.lineno
            break
        pos += 1
    imports = [ast.Import([alias], lineno=lineno, col_offset=0) for alias in aliases]
    mod.body[pos:pos] = imports
    nodes = [mod]
    while nodes:
        node = nodes.pop()
        for (name, field) in ast.iter_fields(node):
            if isinstance(field, list):
                new = []
                for (i, child) in enumerate(field):
                    if isinstance(child, ast.Assert):
                        new.extend(self.visit(child))
                    else:
                        new.append(child)
                        if isinstance(child, ast.AST):
                            nodes.append(child)
                setattr(node, name, new)
            elif (isinstance(field, ast.AST) and (not isinstance(field, ast.expr))):
                nodes.append(field)
