8000 bpo-20523: pdb searches for .pdbrc in ~ instead of $HOME (GH-11847) · python/cpython@1ff7dd6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ff7dd6

Browse files
bpo-20523: pdb searches for .pdbrc in ~ instead of $HOME (GH-11847)
Previously pdb checked the $HOME environmental variable to find the user .pdbrc. If $HOME is not set, the user .pdbrc would not be found. Change pdb to use `os.path.expanduser('~')` to determine the user's home directory. Thus, if $HOME is not set (as in tox or on Windows), os.path.expanduser('~') falls back on other techniques for locating the user's home directory. This follows pip's implementation for loading .piprc. Co-authored-by: Dan Lidral-Porter <dlp@aperiodic.org> (cherry picked from commit 7ea9a85) Co-authored-by: Timothy Hopper <tdhopper@users.noreply.github.com>
1 parent 8990ac0 commit 1ff7dd6

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

Lib/pdb.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,14 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
159159
self.allow_kbdint = False
160160
self.nosigint = nosigint
161161

162-
# Read $HOME/.pdbrc and ./.pdbrc
162+
# Read ~/.pdbrc and ./.pdbrc
163163
self.rcLines = []
164164
if readrc:
165-
if 'HOME' in os.environ:
166-
envHome = os.environ['HOME']
167-
try:
168-
with open(os.path.join(envHome, ".pdbrc")) as rcFile:
169-
self.rcLines.extend(rcFile)
170-
except OSError:
171-
pass
165+
try:
166+
with open(os.path.expanduser('~/.pdbrc')) as rcFile:
167+
self.rcLines.extend(rcFile)
168+
except OSError:
169+
pass
172170
try:
173171
with open(".pdbrc") as rcFile:
174172
self.rcLines.extend(rcFile)

Lib/test/test_pdb.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,19 @@ def test_readrc_kwarg(self):
13501350
if save_home is not None:
13511351
os.environ['HOME'] = save_home
13521352

1353+
def test_readrc_homedir(self):
1354+
save_home = os.environ.pop("HOME", None)
1355+
with support.temp_dir() as temp_dir, patch("os.path.expanduser"):
1356+
rc_path = os.path.join(temp_dir, ".pdbrc")
1357+
os.path.expanduser.return_value = rc_path
1358+
try:
1359+
with open(rc_path, "w") as f:
1360+
f.write("invalid")
1361+
self.assertEqual(pdb.Pdb().rcLines[0], "invalid")
1362+
finally:
1363+
if save_home is not None:
1364+
os.environ["HOME"] = save_home
1365+
13531366
def test_header(self):
13541367
stdout = StringIO()
13551368
header = 'Nobody expects... blah, blah, blah'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``pdb.Pdb`` supports ~/.pdbrc in Windows 7. Patch by Tim Hopper and Dan
2+
Lidral-Porter.

0 commit comments

Comments
 (0)
0