Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2eb6eca

Browse files
committed
Issue #18401: pdb tests don't read ~/.pdbrc anymore
Patch by Martin Matusiak and Sam Kimbrel.
1 parent a1fd078 commit 2eb6eca

4 files changed

Lines changed: 78 additions & 30 deletions

File tree

Doc/library/pdb.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ The ``run*`` functions and :func:`set_trace` are aliases for instantiating the
144144
access further features, you have to do this yourself:
145145

146146
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
147-
nosigint=False)
147+
nosigint=False, readrc=True)
148148

149149
:class:`Pdb` is the debugger class.
150150

@@ -160,6 +160,9 @@ access further features, you have to do this yourself:
160160
This allows you to break into the debugger again by pressing :kbd:`Ctrl-C`. If you
161161
want Pdb not to touch the SIGINT handler, set *nosigint* to true.
162162

163+
The *readrc* argument defaults to True and controls whether Pdb will load
164+
.pdbrc files from the filesystem.
165+
163166
Example call to enable tracing with *skip*::
164167

165168
import pdb; pdb.Pdb(skip=['django.*']).set_trace()
@@ -171,6 +174,9 @@ access further features, you have to do this yourself:
171174
The *nosigint* argument. Previously, a SIGINT handler was never set by
172175
Pdb.
173176

177+
.. versionadded:: 3.5
178+
The *readrc* argument.
179+
174180
.. method:: run(statement, globals=None, locals=None)
175181
runeval(expression, globals=None, locals=None)
176182
runcall(function, *args, **kwds)

Lib/pdb.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
directory, it is read in and executed as if it had been typed at the
5353
debugger prompt. This is particularly useful for aliases. If both
5454
files exist, the one in the home directory is read first and aliases
55-
defined there can be overridden by the local file.
55+
defined there can be overridden by the local file. This behavior can be
56+
disabled by passing the "readrc=False" argument to the Pdb constructor.
5657
5758
Aside from aliases, the debugger is not directly programmable; but it
5859
is implemented as a class from which you can derive your own debugger
@@ -135,7 +136,7 @@ def __repr__(self):
135136
class Pdb(bdb.Bdb, cmd.Cmd):
136137

137138
def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
138-
nosigint=False):
139+
nosigint=False, readrc=True):
139140
bdb.Bdb.__init__(self, skip=skip)
140141
cmd.Cmd.__init__(self, completekey, stdin, stdout)
141142
if stdout:
@@ -158,18 +159,19 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
158159

159160
# Read $HOME/.pdbrc and ./.pdbrc
160161
self.rcLines = []
161-
if 'HOME' in os.environ:
162-
envHome = os.environ['HOME']
162+
if readrc:
163+
if 'HOME' in os.environ:
164+
envHome = os.environ['HOME']
165+
try:
166+
with open(os.path.join(envHome, ".pdbrc")) as rcFile:
167+
self.rcLines.extend(rcFile)
168+
except OSError:
169+
pass
163170
try:
164-
with open(os.path.join(envHome, ".pdbrc")) as rcFile:
171+
with open(".pdbrc") as rcFile:
165172
self.rcLines.extend(rcFile)
166173
except OSError:
167174
pass
168-
try:
169-
with open(".pdbrc") as rcFile:
170-
self.rcLines.extend(rcFile)
171-
except OSError:
172-
pass
173175

174176
self.commands = {} # associates a command list to breakpoint numbers
175177
self.commands_doprompt = {} # for each bp num, tells if the prompt

Lib/test/test_pdb.py

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# A test suite for pdb; not very comprehensive at the moment.
22

33
import doctest
4+
import os
45
import pdb
56
import sys
7+
import tempfile
68
import types
79
import unittest
810
import subprocess
@@ -34,7 +36,7 @@ def test_pdb_displayhook():
3436
"""This tests the custom displayhook for pdb.
3537
3638
>>> def test_function(foo, bar):
37-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
39+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
3840
... pass
3941
4042
>>> with PdbTestInput([
@@ -74,7 +76,7 @@ def test_pdb_basic_commands():
7476
... return foo.upper()
7577
7678
>>> def test_function():
77-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
79+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
7880
... ret = test_function_2('baz')
7981
... print(ret)
8082
@@ -173,7 +175,7 @@ def test_pdb_breakpoint_commands():
173175
"""Test basic commands related to breakpoints.
174176
175177
>>> def test_function():
176-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
178+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
177179
... print(1)
178180
... print(2)
179181
... print(3)
@@ -305,7 +307,7 @@ def test_list_commands():
305307
... return foo
306308
307309
>>> def test_function():
308-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
310+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
309311
... ret = test_function_2('baz')
310312
311313
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
@@ -328,7 +330,7 @@ def test_list_commands():
328330
-> ret = test_function_2('baz')
329331
(Pdb) list
330332
1 def test_function():
331-
2 import pdb; pdb.Pdb(nosigint=True).set_trace()
333+
2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
332334
3 -> ret = test_function_2('baz')
333335
[EOF]
334336
(Pdb) step
@@ -391,7 +393,7 @@ def test_post_mortem():
391393
... print('Exception!')
392394
393395
>>> def test_function():
394-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
396+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
395397
... test_function_2()
396398
... print('Not reached.')
397399
@@ -424,7 +426,7 @@ def test_post_mortem():
424426
-> 1/0
425427
(Pdb) list
426428
1 def test_function():
427-
2 import pdb; pdb.Pdb(nosigint=True).set_trace()
429+
2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
428430
3 -> test_function_2()
429431
4 print('Not reached.')
430432
[EOF]
@@ -448,7 +450,7 @@ def test_pdb_skip_modules():
448450
449451
>>> def skip_module():
450452
... import string
451-
... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True).set_trace()
453+
... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace()
452454
... string.capwords('FOO')
453455
454456
>>> with PdbTestInput([
@@ -477,7 +479,7 @@ def test_pdb_skip_modules_with_callback():
477479
>>> def skip_module():
478480
... def callback():
479481
... return None
480-
... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True).set_trace()
482+
... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace()
481483
... mod.foo_pony(callback)
482484
483485
>>> with PdbTestInput([
@@ -518,7 +520,7 @@ def test_pdb_continue_in_bottomframe():
518520
"""Test that "continue" and "next" work properly in bottom frame (issue #5294).
519521
520522
>>> def test_function():
521-
... import pdb, sys; inst = pdb.Pdb(nosigint=True)
523+
... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False)
522524
... inst.set_trace()
523525
... inst.botframe = sys._getframe() # hackery to get the right botframe
524526
... print(1)
@@ -558,7 +560,7 @@ def test_pdb_continue_in_bottomframe():
558560

559561
def pdb_invoke(method, arg):
560562
"""Run pdb.method(arg)."""
561-
getattr(pdb.Pdb(nosigint=True), method)(arg)
563+
getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg)
562564

563565

564566
def test_pdb_run_with_incorrect_argument():
@@ -607,7 +609,7 @@ def test_next_until_return_at_return_event():
607609
... x = 2
608610
609611
>>> def test_function():
610-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
612+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
611613
... test_function_2()
612614
... test_function_2()
613615
... test_function_2()
@@ -673,7 +675,7 @@ def test_pdb_next_command_for_generator():
673675
... yield 2
674676
675677
>>> def test_function():
676-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
678+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
677679
... it = test_gen()
678680
... try:
679681
... if next(it) != 0:
@@ -733,7 +735,7 @@ def test_pdb_return_command_for_generator():
733735
... yield 2
734736
735737
>>> def test_function():
736-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
738+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
737739
... it = test_gen()
738740
... try:
739741
... if next(it) != 0:
@@ -788,7 +790,7 @@ def test_pdb_until_command_for_generator():
788790
... yield 2
789791
790792
>>> def test_function():
791-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
793+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
792794
... for i in test_gen():
793795
... print(i)
794796
... print("finished")
@@ -830,7 +832,7 @@ def test_pdb_next_command_in_generator_for_loop():
830832
... return 1
831833
832834
>>> def test_function():
833-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
835+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
834836
... for i in test_gen():
835837
... print('value', i)
836838
... x = 123
@@ -875,7 +877,7 @@ def test_pdb_next_command_subiterator():
875877
... return x
876878
877879
>>> def test_function():
878-
... import pdb; pdb.Pdb(nosigint=True).set_trace()
880+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
879881
... for i in test_gen():
880882
... print('value', i)
881883
... x = 123
@@ -1025,7 +1027,7 @@ def test_issue13210(self):
10251027
import pdb
10261028
10271029
def start_pdb():
1028-
pdb.Pdb().set_trace()
1030+
pdb.Pdb(readrc=False).set_trace()
10291031
x = 1
10301032
y = 1
10311033
@@ -1054,13 +1056,47 @@ def test_issue16180(self):
10541056
.format(expected, stdout))
10551057

10561058

1059+
def test_readrc_kwarg(self):
1060+
save_home = os.environ['HOME']
1061+
save_dir = os.getcwd()
1062+
script = """import pdb; pdb.Pdb(readrc=False).set_trace()
1063+
1064+
print('hello')
1065+
"""
1066+
del os.environ['HOME']
1067+
try:
1068+
with tempfile.TemporaryDirectory() as dirname:
1069+
os.chdir(dirname)
1070+
with open('.pdbrc', 'w') as f:
1071+
f.write("invalid\n")
1072+
1073+
with open('main.py', 'w') as f:
1074+
f.write(script)
1075+
1076+
cmd = [sys.executable, 'main.py']
1077+
proc = subprocess.Popen(
1078+
cmd,
1079+
stdout=subprocess.PIPE,
1080+
stdin=subprocess.PIPE,
1081+
stderr=subprocess.PIPE,
1082+
)
1083+
self.addCleanup(proc.stdout.close)
1084+
self.addCleanup(proc.stderr.close)
1085+
stdout, stderr = proc.communicate(b'q\n')
1086+
self.assertNotIn("NameError: name 'invalid' is not defined",
1087+
stdout.decode())
1088+
1089+
finally:
1090+
os.environ['HOME'] = save_home
1091+
os.chdir(save_dir)
1092+
10571093
def tearDown(self):
10581094
support.unlink(support.TESTFN)
10591095

10601096

10611097
def load_tests(*args):
10621098
from test import test_pdb
1063-
suites = [unittest.makeSuite(PdbTestCase), doctest.DocTestSuite(test_pdb)]
1099+
suites = [unittest.makeSuite(PdbTestCase)]
10641100
return unittest.TestSuite(suites)
10651101

10661102

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ Core and Builtins
135135
Library
136136
-------
137137

138+
- Issue #18401: Pdb now supports the 'readrc' keyword argument to control
139+
whether .pdbrc files should be read. Patch by Martin Matusiak and
140+
Sam Kimbrel.
141+
138142
- Issue #25969: Update the lib2to3 grammar to handle the unpacking
139143
generalizations added in 3.5.
140144

0 commit comments

Comments
 (0)