Skip to content

Commit

Permalink
bpo-20523: pdb searches for .pdbrc in ~ instead of $HOME (GH-11847)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
2 people authored and zooba committed Aug 2, 2019
1 parent 8fbece1 commit 7ea9a85
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
14 changes: 6 additions & 8 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,14 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
self.allow_kbdint = False
self.nosigint = nosigint

# Read $HOME/.pdbrc and ./.pdbrc
# Read ~/.pdbrc and ./.pdbrc
self.rcLines = []
if readrc:
if 'HOME' in os.environ:
envHome = os.environ['HOME']
try:
with open(os.path.join(envHome, ".pdbrc")) as rcFile:
self.rcLines.extend(rcFile)
except OSError:
pass
try:
with open(os.path.expanduser('~/.pdbrc')) as rcFile:
self.rcLines.extend(rcFile)
except OSError:
pass
try:
with open(".pdbrc") as rcFile:
self.rcLines.extend(rcFile)
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,19 @@ def test_readrc_kwarg(self):
if save_home is not None:
os.environ['HOME'] = save_home

def test_readrc_homedir(self):
save_home = os.environ.pop("HOME", None)
with support.temp_dir() as temp_dir, patch("os.path.expanduser"):
rc_path = os.path.join(temp_dir, ".pdbrc")
os.path.expanduser.return_value = rc_path
try:
with open(rc_path, "w") as f:
f.write("invalid")
self.assertEqual(pdb.Pdb().rcLines[0], "invalid")
finally:
if save_home is not None:
os.environ["HOME"] = save_home

def test_header(self):
stdout = StringIO()
header = 'Nobody expects... blah, blah, blah'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``pdb.Pdb`` supports ~/.pdbrc in Windows 7. Patch by Tim Hopper and Dan
Lidral-Porter.

0 comments on commit 7ea9a85

Please sign in to comment.