From d4c6872cccf728f7e7052944dd40c2e8e7f33949 Mon Sep 17 00:00:00 2001 From: saucoide Date: Sat, 19 Jul 2025 15:36:41 +0200 Subject: [PATCH 1/4] gh-80744: do not read .pdbrc twice when cwd == $home --- Lib/pdb.py | 13 ++++++++----- Lib/test/test_pdb.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index fc83728fb6dc94..2a83375d12ce5b 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -378,11 +378,14 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, self.rcLines.extend(rcFile) except OSError: pass - try: - with open(".pdbrc", encoding='utf-8') as rcFile: - self.rcLines.extend(rcFile) - except OSError: - pass + if os.path.join( + os.path.abspath(os.curdir), ".pdbrc" + ) != os.path.expanduser("~/.pdbrc"): + try: + with open(".pdbrc", encoding='utf-8') as rcFile: + self.rcLines.extend(rcFile) + except OSError: + pass self.commands = {} # associates a command list to breakpoint numbers self.commands_defining = False # True while in the process of defining diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 6b74e21ad73d1a..0b86a1a2204fb8 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -3935,6 +3935,25 @@ def test_readrc_homedir(self): f.write("invalid") self.assertEqual(pdb.Pdb().rcLines[0], "invalid") + def test_readrc_current_dir(self): + with os_helper.temp_cwd() as cwd: + rc_path = os.path.join(cwd, ".pdbrc") + with open(rc_path, "w") as f: + f.write("invalid") + self.assertEqual(pdb.Pdb().rcLines[0], "invalid") + self.assertEqual(len(pdb.Pdb().rcLines), 1) + + def test_readrc_home_twice(self): + with os_helper.EnvironmentVarGuard() as env: + env.unset("HOME") + with os_helper.temp_cwd() as cwd, patch("os.path.expanduser"): + rc_path = os.path.join(cwd, ".pdbrc") + os.path.expanduser.return_value = rc_path + with open(rc_path, "w") as f: + f.write("invalid") + self.assertEqual(pdb.Pdb().rcLines, ["invalid"]) + self.assertEqual(len(pdb.Pdb().rcLines), 1) + def test_header(self): stdout = StringIO() header = 'Nobody expects... blah, blah, blah' From 53613ef3f8d4f809a72e19a0ba663e4a89735b14 Mon Sep 17 00:00:00 2001 From: saucoide Date: Wed, 23 Jul 2025 13:55:42 +0200 Subject: [PATCH 2/4] simplify path comparison --- Lib/pdb.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 2a83375d12ce5b..729f1e26add1c9 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -378,9 +378,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, self.rcLines.extend(rcFile) except OSError: pass - if os.path.join( - os.path.abspath(os.curdir), ".pdbrc" - ) != os.path.expanduser("~/.pdbrc"): + if os.path.abspath(".pdbrc") != os.path.expanduser("~/.pdbrc"): try: with open(".pdbrc", encoding='utf-8') as rcFile: self.rcLines.extend(rcFile) From 623d54db792139f6357f58ebf61ea0bb7f2ee25a Mon Sep 17 00:00:00 2001 From: saucoide Date: Thu, 24 Jul 2025 10:40:39 +0200 Subject: [PATCH 3/4] rename test --- Lib/test/test_pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 0b86a1a2204fb8..14ac144f209d58 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -3943,7 +3943,7 @@ def test_readrc_current_dir(self): self.assertEqual(pdb.Pdb().rcLines[0], "invalid") self.assertEqual(len(pdb.Pdb().rcLines), 1) - def test_readrc_home_twice(self): + def test_readrc_cwd_is_home(self): with os_helper.EnvironmentVarGuard() as env: env.unset("HOME") with os_helper.temp_cwd() as cwd, patch("os.path.expanduser"): From b8bb7a91dee9e92b68f82158a1f6e3474a086f42 Mon Sep 17 00:00:00 2001 From: saucoide Date: Thu, 24 Jul 2025 10:41:00 +0200 Subject: [PATCH 4/4] simplify asserts --- Lib/test/test_pdb.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 14ac144f209d58..d16475116e1a46 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -3940,8 +3940,7 @@ def test_readrc_current_dir(self): rc_path = os.path.join(cwd, ".pdbrc") with open(rc_path, "w") as f: f.write("invalid") - self.assertEqual(pdb.Pdb().rcLines[0], "invalid") - self.assertEqual(len(pdb.Pdb().rcLines), 1) + self.assertEqual(pdb.Pdb().rcLines[-1], "invalid") def test_readrc_cwd_is_home(self): with os_helper.EnvironmentVarGuard() as env: @@ -3952,7 +3951,6 @@ def test_readrc_cwd_is_home(self): with open(rc_path, "w") as f: f.write("invalid") self.assertEqual(pdb.Pdb().rcLines, ["invalid"]) - self.assertEqual(len(pdb.Pdb().rcLines), 1) def test_header(self): stdout = StringIO()