prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  KeyError trying to reload a system module name
  `KeyError: 'time' (file "/opt/rh/python33/root/usr/lib/python3.3/site-packages/sopel/modules/reload.py", line 62, in f_reload)` should be a sane error message
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `sopel/modules/reload.py`
  Content:
  ```
  1 # coding=utf-8
  2 """
  3 reload.py - Sopel Module Reloader Module
  4 Copyright 2008, Sean B. Palmer, inamidst.com
  5 Licensed under the Eiffel Forum License 2.
  6 
  7 http://sopel.chat
  8 """
  9 from __future__ import unicode_literals, absolute_import, print_function, division
  10 
  11 import collections
  12 import sys
  13 import time
  14 from sopel.tools import iteritems
  15 import sopel.loader
  16 import sopel.module
  17 import subprocess
  18 
  19 
  20 @sopel.module.nickname_commands("reload")
  21 @sopel.module.priority("low")
  22 @sopel.module.thread(False)
  23 def f_reload(bot, trigger):
  24     """Reloads a module, for use by admins only."""
  25     if not trigger.admin:
  26         return
  27 
  28     name = trigger.group(2)
  29     if name == bot.config.core.owner:
  30         return bot.reply('What?')
  31 
  32     if not name or name == '*' or name.upper() == 'ALL THE THINGS':
  33         bot._callables = {
  34             'high': collections.defaultdict(list),
  35             'medium': collections.defaultdict(list),
  36             'low': collections.defaultdict(list)
  37         }
  38         bot.command_groups = collections.defaultdict(list)
  39         bot.setup()
  40         return bot.reply('done')
  41 
  42     if name not in sys.modules:
  43         return bot.reply('%s: not loaded, try the `load` command' % name)
  44 
  45     old_module = sys.modules[name]
  46 
  47     old_callables = {}
  48     for obj_name, obj in iteritems(vars(old_module)):
  49         bot.unregister(obj)
  50 
  51     # Also remove all references to sopel callables from top level of the
  52     # module, so that they will not get loaded again if reloading the
  53     # module does not override them.
  54     for obj_name in old_callables.keys():
  55         delattr(old_module, obj_name)
  56 
  57     # Also delete the setup function
  58     if hasattr(old_module, "setup"):
  59         delattr(old_module, "setup")
  60 
  61     modules = sopel.loader.enumerate_modules(bot.config)
  62     path, type_ = modules[name]
  63     load_module(bot, name, path, type_)
  64 
  65 
  66 def load_module(bot, name, path, type_):
  67     module, mtime = sopel.loader.load_module(name, path, type_)
  68     relevant_parts = sopel.loader.clean_module(module, bot.config)
  69 
  70     bot.register(*relevant_parts)
  71 
  72     # TODO sys.modules[name] = module
  73     if hasattr(module, 'setup'):
  74         module.setup(bot)
  75 
  76     modified = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime))
  77 
  78     bot.reply('%r (version: %s)' % (module, modified))
  79 
  80 
  81 @sopel.module.nickname_commands('update')
  82 def f_update(bot, trigger):
  83     if not trigger.admin:
  84         return
  85 
  86     """Pulls the latest versions of all modules from Git"""
  87     proc = subprocess.Popen('/usr/bin/git pull',
  88                             stdout=subprocess.PIPE,
  89                             stderr=subprocess.PIPE, shell=True)
  90     bot.reply(proc.communicate()[0])
  91 
  92     f_reload(bot, trigger)
  93 
  94 
  95 @sopel.module.nickname_commands("load")
  96 @sopel.module.priority("low")
  97 @sopel.module.thread(False)
  98 def f_load(bot, trigger):
  99     """Loads a module, for use by admins only."""
  100     if not trigger.admin:
  101         return
  102 
  103     name = trigger.group(2)
  104     path = ''
  105     if name == bot.config.core.owner:
  106         return bot.reply('What?')
  107 
  108     if name in sys.modules:
  109         return bot.reply('Module already loaded, use reload')
  110 
  111     mods = sopel.loader.enumerate_modules(bot.config)
  112     if name not in mods:
  113         return bot.reply('Module %s not found' % name)
  114     path, type_ = mods[name]
  115     load_module(bot, name, path, type_)
  116 
  117 
  118 # Catch PM based messages
  119 @sopel.module.commands("reload")
  120 @sopel.module.priority("low")
  121 @sopel.module.thread(False)
  122 def pm_f_reload(bot, trigger):
  123     """Wrapper for allowing delivery of .reload command via PM"""
  124     if trigger.is_privmsg:
  125         f_reload(bot, trigger)
  126 
  127 
  128 @sopel.module.commands('update')
  129 def pm_f_update(bot, trigger):
  130     """Wrapper for allowing delivery of .update command via PM"""
  131     if trigger.is_privmsg:
  132         f_update(bot, trigger)
  133 
  134 
  135 @sopel.module.commands("load")
  136 @sopel.module.priority("low")
  137 @sopel.module.thread(False)
  138 def pm_f_load(bot, trigger):
  139     """Wrapper for allowing delivery of .load command via PM"""
  140     if trigger.is_privmsg:
  141         f_load(bot, trigger)
  142
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/sopel/modules/reload.py b/sopel/modules/reload.py
  --- a/sopel/modules/reload.py
  +++ b/sopel/modules/reload.py
  @@ -40,7 +40,7 @@
           return bot.reply('done')
   
       if name not in sys.modules:
  -        return bot.reply('%s: not loaded, try the `load` command' % name)
  +        return bot.reply('"%s" not loaded, try the `load` command' % name)
   
       old_module = sys.modules[name]
   
  @@ -59,6 +59,8 @@
           delattr(old_module, "setup")
   
       modules = sopel.loader.enumerate_modules(bot.config)
  +    if name not in modules:
  +        return bot.reply('"%s" not loaded, try the `load` command' % name)
       path, type_ = modules[name]
       load_module(bot, name, path, type_)
  
verification_info: {"golden_diff": "diff --git a/sopel/modules/reload.py b/sopel/modules/reload.py\n--- a/sopel/modules/reload.py\n+++ b/sopel/modules/reload.py\n@@ -40,7 +40,7 @@\n         return bot.reply('done')\n \n     if name not in sys.modules:\n-        return bot.reply('%s: not loaded, try the `load` command' % name)\n+        return bot.reply('\"%s\" not loaded, try the `load` command' % name)\n \n     old_module = sys.modules[name]\n \n@@ -59,6 +59,8 @@\n         delattr(old_module, \"setup\")\n \n     modules = sopel.loader.enumerate_modules(bot.config)\n+    if name not in modules:\n+        return bot.reply('\"%s\" not loaded, try the `load` command' % name)\n     path, type_ = modules[name]\n     load_module(bot, name, path, type_)\n", "issue": "KeyError trying to reload a system module name\n`KeyError: 'time' (file \"/opt/rh/python33/root/usr/lib/python3.3/site-packages/sopel/modules/reload.py\", line 62, in f_reload)` should be a sane error message\n\n", "before_files": [{"content": "# coding=utf-8\n\"\"\"\nreload.py - Sopel Module Reloader Module\nCopyright 2008, Sean B. Palmer, inamidst.com\nLicensed under the Eiffel Forum License 2.\n\nhttp://sopel.chat\n\"\"\"\nfrom __future__ import unicode_literals, absolute_import, print_function, division\n\nimport collections\nimport sys\nimport time\nfrom sopel.tools import iteritems\nimport sopel.loader\nimport sopel.module\nimport subprocess\n\n\n@sopel.module.nickname_commands(\"reload\")\n@sopel.module.priority(\"low\")\n@sopel.module.thread(False)\ndef f_reload(bot, trigger):\n    \"\"\"Reloads a module, for use by admins only.\"\"\"\n    if not trigger.admin:\n        return\n\n    name = trigger.group(2)\n    if name == bot.config.core.owner:\n        return bot.reply('What?')\n\n    if not name or name == '*' or name.upper() == 'ALL THE THINGS':\n        bot._callables = {\n            'high': collections.defaultdict(list),\n            'medium': collections.defaultdict(list),\n            'low': collections.defaultdict(list)\n        }\n        bot.command_groups = collections.defaultdict(list)\n        bot.setup()\n        return bot.reply('done')\n\n    if name not in sys.modules:\n        return bot.reply('%s: not loaded, try the `load` command' % name)\n\n    old_module = sys.modules[name]\n\n    old_callables = {}\n    for obj_name, obj in iteritems(vars(old_module)):\n        bot.unregister(obj)\n\n    # Also remove all references to sopel callables from top level of the\n    # module, so that they will not get loaded again if reloading the\n    # module does not override them.\n    for obj_name in old_callables.keys():\n        delattr(old_module, obj_name)\n\n    # Also delete the setup function\n    if hasattr(old_module, \"setup\"):\n        delattr(old_module, \"setup\")\n\n    modules = sopel.loader.enumerate_modules(bot.config)\n    path, type_ = modules[name]\n    load_module(bot, name, path, type_)\n\n\ndef load_module(bot, name, path, type_):\n    module, mtime = sopel.loader.load_module(name, path, type_)\n    relevant_parts = sopel.loader.clean_module(module, bot.config)\n\n    bot.register(*relevant_parts)\n\n    # TODO sys.modules[name] = module\n    if hasattr(module, 'setup'):\n        module.setup(bot)\n\n    modified = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime))\n\n    bot.reply('%r (version: %s)' % (module, modified))\n\n\n@sopel.module.nickname_commands('update')\ndef f_update(bot, trigger):\n    if not trigger.admin:\n        return\n\n    \"\"\"Pulls the latest versions of all modules from Git\"\"\"\n    proc = subprocess.Popen('/usr/bin/git pull',\n                            stdout=subprocess.PIPE,\n                            stderr=subprocess.PIPE, shell=True)\n    bot.reply(proc.communicate()[0])\n\n    f_reload(bot, trigger)\n\n\n@sopel.module.nickname_commands(\"load\")\n@sopel.module.priority(\"low\")\n@sopel.module.thread(False)\ndef f_load(bot, trigger):\n    \"\"\"Loads a module, for use by admins only.\"\"\"\n    if not trigger.admin:\n        return\n\n    name = trigger.group(2)\n    path = ''\n    if name == bot.config.core.owner:\n        return bot.reply('What?')\n\n    if name in sys.modules:\n        return bot.reply('Module already loaded, use reload')\n\n    mods = sopel.loader.enumerate_modules(bot.config)\n    if name not in mods:\n        return bot.reply('Module %s not found' % name)\n    path, type_ = mods[name]\n    load_module(bot, name, path, type_)\n\n\n# Catch PM based messages\n@sopel.module.commands(\"reload\")\n@sopel.module.priority(\"low\")\n@sopel.module.thread(False)\ndef pm_f_reload(bot, trigger):\n    \"\"\"Wrapper for allowing delivery of .reload command via PM\"\"\"\n    if trigger.is_privmsg:\n        f_reload(bot, trigger)\n\n\n@sopel.module.commands('update')\ndef pm_f_update(bot, trigger):\n    \"\"\"Wrapper for allowing delivery of .update command via PM\"\"\"\n    if trigger.is_privmsg:\n        f_update(bot, trigger)\n\n\n@sopel.module.commands(\"load\")\n@sopel.module.priority(\"low\")\n@sopel.module.thread(False)\ndef pm_f_load(bot, trigger):\n    \"\"\"Wrapper for allowing delivery of .load command via PM\"\"\"\n    if trigger.is_privmsg:\n        f_load(bot, trigger)\n", "path": "sopel/modules/reload.py"}], "after_files": [{"content": "# coding=utf-8\n\"\"\"\nreload.py - Sopel Module Reloader Module\nCopyright 2008, Sean B. Palmer, inamidst.com\nLicensed under the Eiffel Forum License 2.\n\nhttp://sopel.chat\n\"\"\"\nfrom __future__ import unicode_literals, absolute_import, print_function, division\n\nimport collections\nimport sys\nimport time\nfrom sopel.tools import iteritems\nimport sopel.loader\nimport sopel.module\nimport subprocess\n\n\n@sopel.module.nickname_commands(\"reload\")\n@sopel.module.priority(\"low\")\n@sopel.module.thread(False)\ndef f_reload(bot, trigger):\n    \"\"\"Reloads a module, for use by admins only.\"\"\"\n    if not trigger.admin:\n        return\n\n    name = trigger.group(2)\n    if name == bot.config.core.owner:\n        return bot.reply('What?')\n\n    if not name or name == '*' or name.upper() == 'ALL THE THINGS':\n        bot._callables = {\n            'high': collections.defaultdict(list),\n            'medium': collections.defaultdict(list),\n            'low': collections.defaultdict(list)\n        }\n        bot.command_groups = collections.defaultdict(list)\n        bot.setup()\n        return bot.reply('done')\n\n    if name not in sys.modules:\n        return bot.reply('\"%s\" not loaded, try the `load` command' % name)\n\n    old_module = sys.modules[name]\n\n    old_callables = {}\n    for obj_name, obj in iteritems(vars(old_module)):\n        bot.unregister(obj)\n\n    # Also remove all references to sopel callables from top level of the\n    # module, so that they will not get loaded again if reloading the\n    # module does not override them.\n    for obj_name in old_callables.keys():\n        delattr(old_module, obj_name)\n\n    # Also delete the setup function\n    if hasattr(old_module, \"setup\"):\n        delattr(old_module, \"setup\")\n\n    modules = sopel.loader.enumerate_modules(bot.config)\n    if name not in modules:\n        return bot.reply('\"%s\" not loaded, try the `load` command' % name)\n    path, type_ = modules[name]\n    load_module(bot, name, path, type_)\n\n\ndef load_module(bot, name, path, type_):\n    module, mtime = sopel.loader.load_module(name, path, type_)\n    relevant_parts = sopel.loader.clean_module(module, bot.config)\n\n    bot.register(*relevant_parts)\n\n    # TODO sys.modules[name] = module\n    if hasattr(module, 'setup'):\n        module.setup(bot)\n\n    modified = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime))\n\n    bot.reply('%r (version: %s)' % (module, modified))\n\n\n@sopel.module.nickname_commands('update')\ndef f_update(bot, trigger):\n    if not trigger.admin:\n        return\n\n    \"\"\"Pulls the latest versions of all modules from Git\"\"\"\n    proc = subprocess.Popen('/usr/bin/git pull',\n                            stdout=subprocess.PIPE,\n                            stderr=subprocess.PIPE, shell=True)\n    bot.reply(proc.communicate()[0])\n\n    f_reload(bot, trigger)\n\n\n@sopel.module.nickname_commands(\"load\")\n@sopel.module.priority(\"low\")\n@sopel.module.thread(False)\ndef f_load(bot, trigger):\n    \"\"\"Loads a module, for use by admins only.\"\"\"\n    if not trigger.admin:\n        return\n\n    name = trigger.group(2)\n    path = ''\n    if name == bot.config.core.owner:\n        return bot.reply('What?')\n\n    if name in sys.modules:\n        return bot.reply('Module already loaded, use reload')\n\n    mods = sopel.loader.enumerate_modules(bot.config)\n    if name not in mods:\n        return bot.reply('Module %s not found' % name)\n    path, type_ = mods[name]\n    load_module(bot, name, path, type_)\n\n\n# Catch PM based messages\n@sopel.module.commands(\"reload\")\n@sopel.module.priority(\"low\")\n@sopel.module.thread(False)\ndef pm_f_reload(bot, trigger):\n    \"\"\"Wrapper for allowing delivery of .reload command via PM\"\"\"\n    if trigger.is_privmsg:\n        f_reload(bot, trigger)\n\n\n@sopel.module.commands('update')\ndef pm_f_update(bot, trigger):\n    \"\"\"Wrapper for allowing delivery of .update command via PM\"\"\"\n    if trigger.is_privmsg:\n        f_update(bot, trigger)\n\n\n@sopel.module.commands(\"load\")\n@sopel.module.priority(\"low\")\n@sopel.module.thread(False)\ndef pm_f_load(bot, trigger):\n    \"\"\"Wrapper for allowing delivery of .load command via PM\"\"\"\n    if trigger.is_privmsg:\n        f_load(bot, trigger)\n", "path": "sopel/modules/reload.py"}]}