[Python-checkins] bpo-20523: pdb searches for .pdbrc in ~ instead of $HOME (GH-11847)

Miss Islington (bot) webhook-mailer at python.org
Fri Aug 2 18:40:19 EDT 2019


https://github.com/python/cpython/commit/1ff7dd681c7f3e31524bfada6d6d2786d4e37704
commit: 1ff7dd681c7f3e31524bfada6d6d2786d4e37704
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-08-02T15:40:14-07:00
summary:

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 at aperiodic.org>
(cherry picked from commit 7ea9a85f132b32347fcbd2cbe1b553a2e9890b56)

Co-authored-by: Timothy Hopper <tdhopper at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2019-02-15-20-42-36.bpo-20523.rRLrvr.rst
M Lib/pdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/pdb.py b/Lib/pdb.py
index 59b23dfc8bea..11d763994458 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -159,16 +159,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)
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index de6c651b370a..63909e21f246 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -1350,6 +1350,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'
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-02-15-20-42-36.bpo-20523.rRLrvr.rst b/Misc/NEWS.d/next/Core and Builtins/2019-02-15-20-42-36.bpo-20523.rRLrvr.rst
new file mode 100644
index 000000000000..91397c243b9f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-02-15-20-42-36.bpo-20523.rRLrvr.rst	
@@ -0,0 +1,2 @@
+``pdb.Pdb`` supports ~/.pdbrc in Windows 7. Patch by Tim Hopper and Dan 
+Lidral-Porter.
\ No newline at end of file



More information about the Python-checkins mailing list