code: def get_modules(modulename=None):
      
      modulename = compat.ensure_not_unicode(modulename)
      if not modulename:
          try:
              return ([modname for (importer, modname, ispkg)
                       in iter_modules()
                       if not modname.startswith("_")] +
                      list(sys.builtin_module_names))
          except OSError:
              # Bug in Python 2.6, see #275
              return list(sys.builtin_module_names)
      try:
          module = safeimport(modulename)
      except ErrorDuringImport:
          return []
      if module is None:
          return []
      if hasattr(module, "__path__"):
          return [modname for (importer, modname, ispkg)
                  in iter_modules(module.__path__)
                  if not modname.startswith("_")]
      return []
docs: Return a list of modules and packages under modulename.
  
      If modulename is not given, return a list of all top level modules
      and packages.