Skip to content

Commit

Permalink
Avoid instantiating IPython on import
Browse files Browse the repository at this point in the history
Fixes #223.
  • Loading branch information
adamchainz authored and gotcha committed Mar 16, 2021
1 parent 5bb1e86 commit 706782d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
46 changes: 24 additions & 22 deletions ipdb/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,36 @@
import ConfigParser as configparser


shell = get_ipython()
if shell is None:
# Not inside IPython
# Build a terminal app in order to force ipython to load the
# configuration
ipapp = TerminalIPythonApp()
# Avoid output (banner, prints)
ipapp.interact = False
ipapp.initialize(['--no-term-title'])
shell = ipapp.shell
else:
# Running inside IPython

# Detect if embed shell or not and display a message
if isinstance(shell, InteractiveShellEmbed):
sys.stderr.write(
"\nYou are currently into an embedded ipython shell,\n"
"the configuration will not be loaded.\n\n"
)
def _get_debugger_cls():
shell = get_ipython()
if shell is None:
# Not inside IPython
# Build a terminal app in order to force ipython to load the
# configuration
ipapp = TerminalIPythonApp()
# Avoid output (banner, prints)
ipapp.interact = False
ipapp.initialize(["--no-term-title"])
shell = ipapp.shell
else:
# Running inside IPython

# Detect if embed shell or not and display a message
if isinstance(shell, InteractiveShellEmbed):
sys.stderr.write(
"\nYou are currently into an embedded ipython shell,\n"
"the configuration will not be loaded.\n\n"
)

# Let IPython decide about which debugger class to use
# This is especially important for tools that fiddle with stdout
debugger_cls = shell.debugger_cls
# Let IPython decide about which debugger class to use
# This is especially important for tools that fiddle with stdout
return shell.debugger_cls


def _init_pdb(context=None, commands=[]):
if context is None:
context = os.getenv("IPDB_CONTEXT_SIZE", get_context_from_config())
debugger_cls = _get_debugger_cls()
try:
p = debugger_cls(context=context)
except TypeError:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@
from ipdb.__main__ import main


@patch('ipdb.__main__.debugger_cls')
@patch('ipdb.__main__._get_debugger_cls')
class OptsTest(unittest.TestCase):
def set_argv(self, *argv):
argv_patch = patch('ipdb.__main__.sys.argv', argv)
argv_patch.start()
self.addCleanup(argv_patch.stop)

@patch('ipdb.__main__.sys.version_info', (3, 7))
def test_debug_module_script(self, debugger_cls):
def test_debug_module_script(self, get_debugger_cls):
module_name = 'my_buggy_module'
self.set_argv('ipdb', '-m', module_name)

main()

debugger = debugger_cls.return_value
debugger = get_debugger_cls.return_value.return_value
debugger._runmodule.assert_called_once_with(module_name)

@patch('ipdb.__main__.os.path.exists')
def test_debug_script(self, exists, debugger_cls):
def test_debug_script(self, exists, get_debugger_cls):
script_name = 'my_buggy_script'
self.set_argv('ipdb', script_name)

main()

debugger = debugger_cls.return_value
debugger = get_debugger_cls.return_value.return_value
debugger._runscript.assert_called_once_with(script_name)

def test_option_m_fallback_on_py36(self, debugger_cls):
def test_option_m_fallback_on_py36(self, get_debugger_cls):
self.set_argv('ipdb', '-m', 'my.module')
with patch('ipdb.__main__.sys.version_info', (3, 6)):
with self.assertRaises(GetoptError):
Expand Down

0 comments on commit 706782d

Please sign in to comment.