class_docstr: Custom finder for the dynamic rewrite system.
  
  It handles modules like `fun_mc.dynamic.<backend>`.
class_docstr_tok: ["Custom","finder","for","the","dynamic","rewrite","system",".","It","handles","modules","like","`","fun_mc.dynamic.","<","backend",">","`","."]
docstr: See base class.
docstr_tok: ["See","base","class","."]
code: def find_spec(self, fullname, path, target=None):
      """See base class."""
      del target  # We don't use this hint.
      root_name_comps = _root_name_comps()
      root_name = _root_name() + ".dynamic"
  
      if DEBUG:
          print("candidate: ", fullname, path)
      # Only handle things starting with .dynamic.
      if not fullname.startswith(root_name):
          return
  
      if DEBUG:
          print("fullname: ", fullname)
          print("path: ", path)
  
      # We cut out the leading components (including 'dynamic', hence the + 1),
      # leaving us with [<backend>, ...].
      module_name_comps = fullname.split(".")[
          len(root_name_comps) + 1 :
      ]
  
      if DEBUG:
          print("module_name_comps: ", module_name_comps)
  
      # This shouldn't really happen, but to be safe we don't handle this case
      # either. This would correspond to doing `import
      # fun_mc.dynamic.<backend>`, which doesn't need rewriting.
      if len(module_name_comps) < 2:
          return
  
      # We don't rewrite these.
      if module_name_comps[1] in ["backend", "util", "tf_on_jax"]:
          return
  
      backend_name = module_name_comps[0]
      orig_module_name = ".".join(
          root_name_comps + module_name_comps[1:]
      )
      if DEBUG:
          print("backend: ", backend_name)
          print("orig_module_name: ", orig_module_name)
      # N.B. this will actually execute package __init__.py files. If those import
      # backend-specific modules, those imports will fail. That's why the
      # __init__.py files in question disable import errors using the
      # backends.util.silence_nonrewritten_import_errors utility.
      orig_spec = importlib.util.find_spec(orig_module_name)
      if orig_spec is None:
          raise ImportError("Cannot import " + orig_module_name)
      is_package = bool(orig_spec.submodule_search_locations)
      orig_loader = orig_spec.loader
      # We use duck-typing here because we don't necesarily need this to be a
      # SourceFileLoader, just that it has this method.
      if not hasattr(orig_loader, "get_data"):
          raise TypeError(
              "{} has an unsupported loader: {}".format(
                  orig_module_name, orig_loader
              )
          )
  
      spec = importlib.machinery.ModuleSpec(
          fullname,
          Loader(
              orig_module_name,
              orig_loader,
              orig_spec.origin,
              backend_name,
          ),  # pylint: disable=abstract-class-instantiated
          origin=orig_spec.origin,
          is_package=is_package,
      )
  
      # We need to modify the spec after construction to set a few attributes.
      # This is allowed as per ModuleSpec docstring.
      if is_package:
          # Otherwise importing from packages fails to work.
          spec.submodule_search_locations = [
              ".".join([path[0], module_name_comps[-1]])
          ]
      # Helps with pdb integration.
      spec.has_location = True
      # We don't cache these rewritten modules.
      spec.cached = False
      if DEBUG:
          print()
      return spec
  
file_path: probability/spinoffs/fun_mc/fun_mc/backends/rewrite.py
imports: import importlib
  import importlib.abc
  import importlib.machinery
  import importlib.util
  import re
  import sys
label_desc: Use image node_id 1 for calling the Finder obj's underlying member method code with example usage: obj.find_spec(fullname, path, target) and returns: spec