Skip to content

Commit 91ce8ce

Browse files
committed
more coderabbit suggestions
1 parent bb5ffcf commit 91ce8ce

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

ipdb/__main__.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
__version__ = "0.13.14.dev0"
1414

1515
from IPython import get_ipython
16-
from IPython.core.application import ProfileDir
1716
from IPython.core.debugger import BdbQuit_excepthook
18-
from IPython.core.profiledir import ProfileDirError
17+
from IPython.core.profiledir import ProfileDir, ProfileDirError
1918
from IPython.paths import get_ipython_dir
2019
from IPython.terminal.ipapp import TerminalIPythonApp
2120
from IPython.terminal.embed import InteractiveShellEmbed
@@ -33,15 +32,50 @@ def _get_debugger_cls(ipython_profile="default"):
3332
# Build a terminal app in order to force ipython to load the
3433
# configuration
3534
ipython_dir = get_ipython_dir()
35+
profile_dir = None
36+
37+
# First, try to find the requested profile
3638
try:
3739
profile_dir = ProfileDir.find_profile_dir_by_name(
3840
ipython_dir=ipython_dir,
3941
name=ipython_profile,
4042
)
41-
except ProfileDirError: # fallback to default-profile
42-
profile_dir = ProfileDir.find_profile_dir_by_name(
43-
ipython_dir=ipython_dir,
44-
)
43+
except ProfileDirError:
44+
# Profile doesn't exist, try to create it
45+
try:
46+
profile_dir = ProfileDir.create_profile_dir_by_name(
47+
ipython_dir=ipython_dir,
48+
name=ipython_profile,
49+
)
50+
except (ProfileDirError, Exception):
51+
# Creation failed, fallback to default profile
52+
profile_dir = None
53+
54+
# If we still don't have a profile_dir, try the default profile
55+
if profile_dir is None:
56+
try:
57+
# Try to find default profile
58+
profile_dir = ProfileDir.find_profile_dir_by_name(
59+
ipython_dir=ipython_dir,
60+
name='default',
61+
)
62+
except ProfileDirError:
63+
# Default doesn't exist either, create it
64+
try:
65+
profile_dir = ProfileDir.create_profile_dir_by_name(
66+
ipython_dir=ipython_dir,
67+
name='default',
68+
)
69+
except (ProfileDirError, Exception):
70+
# Last resort: create profile without name
71+
profile_dir = ProfileDir.create_profile_dir_by_name(
72+
ipython_dir=ipython_dir,
73+
)
74+
75+
# Ensure we have a profile_dir before creating TerminalIPythonApp
76+
if profile_dir is None:
77+
raise RuntimeError("Unable to create or find any IPython profile directory")
78+
4579
ipapp = TerminalIPythonApp(profile_dir=profile_dir)
4680

4781
# Avoid output (banner, prints)

tests/test_config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ def test_missing_profile_setup(self):
405405
os.unlink(self.setup_filename)
406406
write_lines_to_file(
407407
self.pyproject_filename,
408-
""
408+
[]
409409
)
410410

411411
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
412412
profile_name = get_ipython_profile_from_config()
413-
assert profile_name == "default"
413+
self.assertEqual(profile_name, "default")
414414

415415
def test_default_profile_setup(self):
416416
"""
@@ -431,7 +431,7 @@ def test_default_profile_setup(self):
431431

432432
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
433433
profile_name = get_ipython_profile_from_config()
434-
assert profile_name == "default"
434+
self.assertEqual(profile_name, "default")
435435

436436
def test_non_default_profile_setup(self):
437437
"""
@@ -451,4 +451,4 @@ def test_non_default_profile_setup(self):
451451

452452
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
453453
profile_name = get_ipython_profile_from_config()
454-
assert profile_name == "foo"
454+
self.assertEqual(profile_name, "foo")

0 commit comments

Comments
 (0)