# Buggy code
def concat_string(node, stop=None):
    """Builds a string from a ast.BinOp chain.

    This will build a string from a series of ast.Str 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.

    :param node: (ast.Str or ast.BinOp) The node to process
    :param stop: (ast.Str or ast.BinOp) Optional base node to stop at
    :returns: (Tuple) the root node of the expression, the string value
    """

    def _get(node, bits, stop=None):
        if node != stop:
            bits.append(
                _get(node.left, bits, stop)
                if isinstance(node.left, ast.BinOp)
                else node.left
            )
            bits.append(
                _get(node.right, bits, stop)
                if isinstance(node.right, ast.BinOp)
                else node.right
            )

    bits = [node]
    while isinstance(node.parent, ast.BinOp):
        node = node.parent
    if isinstance(node, ast.BinOp):
        _get(node, bits, stop)
    return (node, " ".join([x.s for x in bits if isinstance(x, ast.Str)]))


# Traceback (AttributeError)
from bandit.core.config import BanditConfig
from bandit.core.meta_ast import BanditMetaAst
from bandit.core.metrics import Metrics
from bandit.core.node_visitor import BanditNodeVisitor
from bandit.core.test_set import BanditTestSet
from pyflakes.checker import Checker
import ast
profile = {}
bnv = BanditNodeVisitor(
...     'filename',
...     BanditMetaAst(),
...     BanditTestSet(BanditConfig(), profile=profile),
...     False,
...     [],
...     Metrics(),
... )
tree = ast.parse("""def test():
...     try:
...         x = 5
...         if True:
...             x = 10  # noqa: F841
...     except AttributeError:
...         pass
... """)
bnv.generic_visit(tree)
Checker(tree=tree)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 673, in __init__
self.runDeferred(self._deferredFunctions)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 710, in runDeferred
handler()
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1474, in runFunction
self.handleChildren(node, omit='decorator_list')
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1073, in handleChildren
self.handleNode(node, tree)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1120, in handleNode
handler(node)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1623, in TRY
self.handleNode(child, node)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1120, in handleNode
handler(node)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1073, in handleChildren
self.handleNode(node, tree)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1120, in handleNode
handler(node)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1073, in handleChildren
self.handleNode(node, tree)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1120, in handleNode
handler(node)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1353, in NAME
self.handleNodeStore(node)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1017, in handleNodeStore
self.addBinding(node, binding)
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 876, in addBinding
not self.differentForks(node, existing.source)):
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 857, in differentForks
if self.descendantOf(lnode, items, ancestor) ^ \
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 835, in descendantOf
if self.getCommonAncestor(node, a, stop):
File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 827, in getCommonAncestor
if (lnode.depth > rnode.depth):
AttributeError: 'ExceptHandler' object has no attribute 'depth'

# Fixed code
def concat_string(node, stop=None):
    """Builds a string from a ast.BinOp chain.

    This will build a string from a series of ast.Str 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.

    :param node: (ast.Str or ast.BinOp) The node to process
    :param stop: (ast.Str or ast.BinOp) Optional base node to stop at
    :returns: (Tuple) the root node of the expression, the string value
    """

    def _get(node, bits, stop=None):
        if node != stop:
            bits.append(
                _get(node.left, bits, stop)
                if isinstance(node.left, ast.BinOp)
                else node.left
            )
            bits.append(
                _get(node.right, bits, stop)
                if isinstance(node.right, ast.BinOp)
                else node.right
            )

    bits = [node]
    while isinstance(node._bandit_parent, ast.BinOp):
        node = node._bandit_parent
    if isinstance(node, ast.BinOp):
        _get(node, bits, stop)
    return (node, " ".join([x.s for x in bits if isinstance(x, ast.Str)]))