def _traverse_internal()

in tools/tensorflow_docs/api_generator/traverse.py [0:0]


def _traverse_internal(root, visitors, stack, path):
  """Internal helper for traverse."""
  new_stack = stack + [root]

  # Only traverse modules and classes
  if not inspect.isclass(root) and not inspect.ismodule(root):
    return

  try:
    children = inspect.getmembers(root)
  except ImportError:
    # On some Python installations, some modules do not support enumerating
    # members (six in particular), leading to import errors.
    children = []

  # Break cycles.
  filtered_children = []
  for name, child in children:
    if any(child is item for item in new_stack):  # `in`, but using `is`
      continue
    filtered_children.append((name, child))
  children = filtered_children

  # Apply all callbacks, allowing each to filter the children
  for visitor in visitors:
    children = visitor(path, root, list(children))

  for name, child in children:
    # Break cycles
    child_path = path + (name,)
    _traverse_internal(child, visitors, new_stack, child_path)