diff --git a/Lib/pdb.py b/Lib/pdb.py index f5d33c27fc1d91..1008b1c181a63e 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -162,18 +162,18 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, # Read $HOME/.pdbrc and ./.pdbrc self.rcLines = [] if readrc: + readrcFiles = [] if 'HOME' in os.environ: envHome = os.environ['HOME'] + readrcFiles.append(os.path.join(envHome, ".pdbrc")) + if not readrcFiles or envHome != os.getcwd(): + readrcFiles.append(".pdbrc") + for rcPath in readrcFiles: try: - with open(os.path.join(envHome, ".pdbrc")) as rcFile: + with open(rcPath) as rcFile: self.rcLines.extend(rcFile) except OSError: pass - try: - with open(".pdbrc") as rcFile: - self.rcLines.extend(rcFile) - except OSError: - pass self.commands = {} # associates a command list to breakpoint numbers self.commands_doprompt = {} # for each bp num, tells if the prompt diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 56d823249544ae..bc10ff3b91918f 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1337,6 +1337,33 @@ def test_readrc_kwarg(self): if save_home is not None: os.environ['HOME'] = save_home + def test_readrc_home_twice(self): + script = textwrap.dedent(""" + import pdb; pdb.Pdb(readrc=True).set_trace() + + print('hello') + """) + + with support.temp_cwd() as cur_dir: + with patch.dict(os.environ, {'HOME': cur_dir}): + with open('.pdbrc', 'w') as f: + f.write("invalid\n") + + with open('main.py', 'w') as f: + f.write(script) + + cmd = [sys.executable, 'main.py'] + proc = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + with proc: + stdout, stderr = proc.communicate(b'q\n') + output = stdout.decode() + self.assertEqual(output.count('NameError'), 1) + def test_header(self): stdout = StringIO() header = 'Nobody expects... blah, blah, blah' diff --git a/Misc/NEWS.d/next/Library/2019-04-15-04-09-04.bpo-36563.4x71uX.rst b/Misc/NEWS.d/next/Library/2019-04-15-04-09-04.bpo-36563.4x71uX.rst new file mode 100644 index 00000000000000..eebc6f42dfcb4e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-04-15-04-09-04.bpo-36563.4x71uX.rst @@ -0,0 +1 @@ +.pdbrc is not read twice anymore when the current directory is $HOME.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: