Skip to content

Commit 3d32c4f

Browse files
committed
IPython refuses to update the namespace. fix scrapy#396
IPython embedding code borrowed from pallets/werkzeug#85
1 parent b1d8919 commit 3d32c4f

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

scrapy/utils/console.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11

2-
def start_python_console(namespace=None, noipython=False):
2+
def start_python_console(namespace=None, noipython=False, banner=''):
33
"""Start Python console binded to the given namespace. If IPython is
44
available, an IPython console will be started instead, unless `noipython`
55
is True. Also, tab completion will be used on Unix systems.
66
"""
77
if namespace is None:
88
namespace = {}
9+
910
try:
1011
try: # use IPython if available
1112
if noipython:
12-
raise ImportError
13-
import IPython
13+
raise ImportError()
14+
1415
try:
15-
IPython.embed(user_ns=namespace)
16-
except AttributeError:
17-
shell = IPython.Shell.IPShellEmbed(argv=[], user_ns=namespace)
18-
shell()
16+
from IPython.frontend.terminal.embed import InteractiveShellEmbed
17+
sh = InteractiveShellEmbed(banner1=banner)
18+
except ImportError:
19+
from IPython.Shell import IPShellEmbed
20+
sh = IPShellEmbed(banner=banner)
21+
22+
sh(global_ns={}, local_ns=namespace)
1923
except ImportError:
2024
import code
2125
try: # readline module is only available on unix systems
@@ -25,6 +29,6 @@ def start_python_console(namespace=None, noipython=False):
2529
else:
2630
import rlcompleter
2731
readline.parse_and_bind("tab:complete")
28-
code.interact(banner='', local=namespace)
32+
code.interact(banner=banner, local=namespace)
2933
except SystemExit: # raised when using exit() in python code.interact
3034
pass

0 commit comments

Comments
 (0)