We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
inspect_response(response) yields incorrect response in IPython shell
Example case (requires registration at example site, and even then would be hard to use as a use-case; modify to suit your needs): http://pastebin.com/GT8N893q

In the above example, the response.meta printout in after_submit callback does not match that within the inspect_response shell on the second iteration (the first is correct).  It appears that inspect_response has a stale response the second time.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `scrapy/utils/console.py`
Content:
```
1 from functools import wraps
2 from collections import OrderedDict
3 
4 def _embed_ipython_shell(namespace={}, banner=''):
5     """Start an IPython Shell"""
6     try:
7         from IPython.terminal.embed import InteractiveShellEmbed
8         from IPython.terminal.ipapp import load_default_config
9     except ImportError:
10         from IPython.frontend.terminal.embed import InteractiveShellEmbed
11         from IPython.frontend.terminal.ipapp import load_default_config
12 
13     @wraps(_embed_ipython_shell)
14     def wrapper(namespace=namespace, banner=''):
15         config = load_default_config()
16         # Always use .instace() to ensure _instance propagation to all parents
17         # this is needed for <TAB> completion works well for new imports
18         shell = InteractiveShellEmbed.instance(
19             banner1=banner, user_ns=namespace, config=config)
20         shell()
21     return wrapper
22 
23 def _embed_bpython_shell(namespace={}, banner=''):
24     """Start a bpython shell"""
25     import bpython
26     @wraps(_embed_bpython_shell)
27     def wrapper(namespace=namespace, banner=''):
28         bpython.embed(locals_=namespace, banner=banner)
29     return wrapper
30 
31 def _embed_standard_shell(namespace={}, banner=''):
32     """Start a standard python shell"""
33     import code
34     try: # readline module is only available on unix systems
35         import readline
36     except ImportError:
37         pass
38     else:
39         import rlcompleter
40         readline.parse_and_bind("tab:complete")
41     @wraps(_embed_standard_shell)
42     def wrapper(namespace=namespace, banner=''):
43         code.interact(banner=banner, local=namespace)
44     return wrapper
45 
46 DEFAULT_PYTHON_SHELLS = OrderedDict([
47     ('ipython', _embed_ipython_shell),
48     ('bpython', _embed_bpython_shell),
49     ( 'python', _embed_standard_shell),
50 ])
51 
52 def get_shell_embed_func(shells=None, known_shells=None):
53     """Return the first acceptable shell-embed function
54     from a given list of shell names.
55     """
56     if shells is None: # list, preference order of shells
57         shells = DEFAULT_PYTHON_SHELLS.keys()
58     if known_shells is None: # available embeddable shells
59         known_shells = DEFAULT_PYTHON_SHELLS.copy()
60     for shell in shells:
61         if shell in known_shells:
62             try:
63                 # function test: run all setup code (imports),
64                 # but dont fall into the shell
65                 return known_shells[shell]()
66             except ImportError:
67                 continue
68 
69 def start_python_console(namespace=None, banner='', shells=None):
70     """Start Python console bound to the given namespace.
71     Readline support and tab completion will be used on Unix, if available.
72     """
73     if namespace is None:
74         namespace = {}
75 
76     try:
77         shell = get_shell_embed_func(shells)
78         if shell is not None:
79             shell(namespace=namespace, banner=banner)
80     except SystemExit: # raised when using exit() in python code.interact
81         pass
82
```
--- 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()

```

diff --git a/scrapy/utils/console.py b/scrapy/utils/console.py
--- a/scrapy/utils/console.py
+++ b/scrapy/utils/console.py
@@ -15,6 +15,9 @@
         config = load_default_config()
         # Always use .instace() to ensure _instance propagation to all parents
         # this is needed for <TAB> completion works well for new imports
+        # and clear the instance to always have the fresh env
+        # on repeated breaks like with inspect_response()
+        InteractiveShellEmbed.clear_instance()
         shell = InteractiveShellEmbed.instance(
             banner1=banner, user_ns=namespace, config=config)
         shell()

{"golden_diff": "diff --git a/scrapy/utils/console.py b/scrapy/utils/console.py\n--- a/scrapy/utils/console.py\n+++ b/scrapy/utils/console.py\n@@ -15,6 +15,9 @@\n         config = load_default_config()\n         # Always use .instace() to ensure _instance propagation to all parents\n         # this is needed for <TAB> completion works well for new imports\n+        # and clear the instance to always have the fresh env\n+        # on repeated breaks like with inspect_response()\n+        InteractiveShellEmbed.clear_instance()\n         shell = InteractiveShellEmbed.instance(\n             banner1=banner, user_ns=namespace, config=config)\n         shell()\n", "issue": "inspect_response(response) yields incorrect response in IPython shell\nExample case (requires registration at example site, and even then would be hard to use as a use-case; modify to suit your needs): http://pastebin.com/GT8N893q\n\nIn the above example, the response.meta printout in after_submit callback does not match that within the inspect_response shell on the second iteration (the first is correct).  It appears that inspect_response has a stale response the second time.\n\n", "before_files": [{"content": "from functools import wraps\nfrom collections import OrderedDict\n\ndef _embed_ipython_shell(namespace={}, banner=''):\n    \"\"\"Start an IPython Shell\"\"\"\n    try:\n        from IPython.terminal.embed import InteractiveShellEmbed\n        from IPython.terminal.ipapp import load_default_config\n    except ImportError:\n        from IPython.frontend.terminal.embed import InteractiveShellEmbed\n        from IPython.frontend.terminal.ipapp import load_default_config\n\n    @wraps(_embed_ipython_shell)\n    def wrapper(namespace=namespace, banner=''):\n        config = load_default_config()\n        # Always use .instace() to ensure _instance propagation to all parents\n        # this is needed for <TAB> completion works well for new imports\n        shell = InteractiveShellEmbed.instance(\n            banner1=banner, user_ns=namespace, config=config)\n        shell()\n    return wrapper\n\ndef _embed_bpython_shell(namespace={}, banner=''):\n    \"\"\"Start a bpython shell\"\"\"\n    import bpython\n    @wraps(_embed_bpython_shell)\n    def wrapper(namespace=namespace, banner=''):\n        bpython.embed(locals_=namespace, banner=banner)\n    return wrapper\n\ndef _embed_standard_shell(namespace={}, banner=''):\n    \"\"\"Start a standard python shell\"\"\"\n    import code\n    try: # readline module is only available on unix systems\n        import readline\n    except ImportError:\n        pass\n    else:\n        import rlcompleter\n        readline.parse_and_bind(\"tab:complete\")\n    @wraps(_embed_standard_shell)\n    def wrapper(namespace=namespace, banner=''):\n        code.interact(banner=banner, local=namespace)\n    return wrapper\n\nDEFAULT_PYTHON_SHELLS = OrderedDict([\n    ('ipython', _embed_ipython_shell),\n    ('bpython', _embed_bpython_shell),\n    ( 'python', _embed_standard_shell),\n])\n\ndef get_shell_embed_func(shells=None, known_shells=None):\n    \"\"\"Return the first acceptable shell-embed function\n    from a given list of shell names.\n    \"\"\"\n    if shells is None: # list, preference order of shells\n        shells = DEFAULT_PYTHON_SHELLS.keys()\n    if known_shells is None: # available embeddable shells\n        known_shells = DEFAULT_PYTHON_SHELLS.copy()\n    for shell in shells:\n        if shell in known_shells:\n            try:\n                # function test: run all setup code (imports),\n                # but dont fall into the shell\n                return known_shells[shell]()\n            except ImportError:\n                continue\n\ndef start_python_console(namespace=None, banner='', shells=None):\n    \"\"\"Start Python console bound to the given namespace.\n    Readline support and tab completion will be used on Unix, if available.\n    \"\"\"\n    if namespace is None:\n        namespace = {}\n\n    try:\n        shell = get_shell_embed_func(shells)\n        if shell is not None:\n            shell(namespace=namespace, banner=banner)\n    except SystemExit: # raised when using exit() in python code.interact\n        pass\n", "path": "scrapy/utils/console.py"}], "after_files": [{"content": "from functools import wraps\nfrom collections import OrderedDict\n\ndef _embed_ipython_shell(namespace={}, banner=''):\n    \"\"\"Start an IPython Shell\"\"\"\n    try:\n        from IPython.terminal.embed import InteractiveShellEmbed\n        from IPython.terminal.ipapp import load_default_config\n    except ImportError:\n        from IPython.frontend.terminal.embed import InteractiveShellEmbed\n        from IPython.frontend.terminal.ipapp import load_default_config\n\n    @wraps(_embed_ipython_shell)\n    def wrapper(namespace=namespace, banner=''):\n        config = load_default_config()\n        # Always use .instace() to ensure _instance propagation to all parents\n        # this is needed for <TAB> completion works well for new imports\n        # and clear the instance to always have the fresh env\n        # on repeated breaks like with inspect_response()\n        InteractiveShellEmbed.clear_instance()\n        shell = InteractiveShellEmbed.instance(\n            banner1=banner, user_ns=namespace, config=config)\n        shell()\n    return wrapper\n\ndef _embed_bpython_shell(namespace={}, banner=''):\n    \"\"\"Start a bpython shell\"\"\"\n    import bpython\n    @wraps(_embed_bpython_shell)\n    def wrapper(namespace=namespace, banner=''):\n        bpython.embed(locals_=namespace, banner=banner)\n    return wrapper\n\ndef _embed_standard_shell(namespace={}, banner=''):\n    \"\"\"Start a standard python shell\"\"\"\n    import code\n    try: # readline module is only available on unix systems\n        import readline\n    except ImportError:\n        pass\n    else:\n        import rlcompleter\n        readline.parse_and_bind(\"tab:complete\")\n    @wraps(_embed_standard_shell)\n    def wrapper(namespace=namespace, banner=''):\n        code.interact(banner=banner, local=namespace)\n    return wrapper\n\nDEFAULT_PYTHON_SHELLS = OrderedDict([\n    ('ipython', _embed_ipython_shell),\n    ('bpython', _embed_bpython_shell),\n    ( 'python', _embed_standard_shell),\n])\n\ndef get_shell_embed_func(shells=None, known_shells=None):\n    \"\"\"Return the first acceptable shell-embed function\n    from a given list of shell names.\n    \"\"\"\n    if shells is None: # list, preference order of shells\n        shells = DEFAULT_PYTHON_SHELLS.keys()\n    if known_shells is None: # available embeddable shells\n        known_shells = DEFAULT_PYTHON_SHELLS.copy()\n    for shell in shells:\n        if shell in known_shells:\n            try:\n                # function test: run all setup code (imports),\n                # but dont fall into the shell\n                return known_shells[shell]()\n            except ImportError:\n                continue\n\ndef start_python_console(namespace=None, banner='', shells=None):\n    \"\"\"Start Python console bound to the given namespace.\n    Readline support and tab completion will be used on Unix, if available.\n    \"\"\"\n    if namespace is None:\n        namespace = {}\n\n    try:\n        shell = get_shell_embed_func(shells)\n        if shell is not None:\n            shell(namespace=namespace, banner=banner)\n    except SystemExit: # raised when using exit() in python code.interact\n        pass\n", "path": "scrapy/utils/console.py"}]}