Skip to content

Commit 09044dd

Browse files
authored
gh-80744: do not read .pdbrc twice when cwd == $home (#136816)
1 parent b8d3fdd commit 09044dd

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

Lib/pdb.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,22 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
391391
# Read ~/.pdbrc and ./.pdbrc
392392
self.rcLines = []
393393
if readrc:
394+
home_rcfile = os.path.expanduser("~/.pdbrc")
395+
local_rcfile = os.path.abspath(".pdbrc")
396+
394397
try:
395-
with open(os.path.expanduser('~/.pdbrc'), encoding='utf-8') as rcFile:
396-
self.rcLines.extend(rcFile)
397-
except OSError:
398-
pass
399-
try:
400-
with open(".pdbrc", encoding='utf-8') as rcFile:
401-
self.rcLines.extend(rcFile)
398+
with open(home_rcfile, encoding='utf-8') as rcfile:
399+
self.rcLines.extend(rcfile)
402400
except OSError:
403401
pass
404402

403+
if local_rcfile != home_rcfile:
404+
try:
405+
with open(local_rcfile, encoding='utf-8') as rcfile:
406+
self.rcLines.extend(rcfile)
407+
except OSError:
408+
pass
409+
405410
self.commands = {} # associates a command list to breakpoint numbers
406411
self.commands_defining = False # True while in the process of defining
407412
# a command list

Lib/test/test_pdb.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,23 @@ def test_readrc_homedir(self):
40934093
f.write("invalid")
40944094
self.assertEqual(pdb.Pdb().rcLines[0], "invalid")
40954095

4096+
def test_readrc_current_dir(self):
4097+
with os_helper.temp_cwd() as cwd:
4098+
rc_path = os.path.join(cwd, ".pdbrc")
4099+
with open(rc_path, "w") as f:
4100+
f.write("invalid")
4101+
self.assertEqual(pdb.Pdb().rcLines[-1], "invalid")
4102+
4103+
def test_readrc_cwd_is_home(self):
4104+
with os_helper.EnvironmentVarGuard() as env:
4105+
env.unset("HOME")
4106+
with os_helper.temp_cwd() as cwd, patch("os.path.expanduser"):
4107+
rc_path = os.path.join(cwd, ".pdbrc")
4108+
os.path.expanduser.return_value = rc_path
4109+
with open(rc_path, "w") as f:
4110+
f.write("invalid")
4111+
self.assertEqual(pdb.Pdb().rcLines, ["invalid"])
4112+
40964113
def test_header(self):
40974114
stdout = StringIO()
40984115
header = 'Nobody expects... blah, blah, blah'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix issue where ``pdb`` would read a ``.pdbrc`` twice if launched from the home directory

0 commit comments

Comments
 (0)