77__all__ = ["BreakpointManager" , "Breakpoint" ]
88
99import os .path as osp
10+ from collections import defaultdict
1011from types import CodeType , ModuleType
1112from typing import Optional
1213from xdis import load_module
@@ -138,8 +139,8 @@ def __init__(self):
138139 # class unless it is put inside __init__
139140
140141 self .bpbynumber : list = [None ]
141- self .bplist = {}
142- self .code_list = {}
142+ self .bplist = defaultdict ( list )
143+ self .code_list = defaultdict ( list )
143144 return
144145
145146 def bpnumbers (self ):
@@ -174,7 +175,7 @@ def add_breakpoint(
174175 offset : int = - 1 ,
175176 temporary : bool = False ,
176177 condition : Optional [str ] = None ,
177- func = None ,
178+ func_or_code = None ,
178179 ):
179180 """
180181 Add a breakpoint in ``filename`` at line number ``lineno``.
@@ -188,41 +189,33 @@ def add_breakpoint(
188189 filename = osp .realpath (filename )
189190
190191 assert (
191- isinstance (filename , str ) or func is not None
192+ isinstance (filename , str ) or func_or_code is not None
192193 ), "You must either supply a filename or give a line number"
193194
194- if isinstance (func , CodeType ):
195- code = func
196- elif isinstance (func , ModuleType ):
197- if hasattr (func , "__cached__" ):
195+ if isinstance (func_or_code , CodeType ):
196+ code = func_or_code
197+ elif isinstance (func_or_code , ModuleType ):
198+ if hasattr (func_or_code , "__cached__" ):
198199 # FIXME: we can probably do better hooking into importlib
199200 # or something lower-level
200- _ , _ , _ , code , _ , _ , _ = load_module (func .__cached__ , fast_load = True , get_code = True )
201+ _ , _ , _ , code , _ , _ , _ = load_module (func_or_code .__cached__ , fast_load = True , get_code = True )
201202 else :
202- print (f"Don't know what to do with frozen module { func } " )
203+ print (f"Don't know what to do with frozen module { func_or_code } " )
203204 return
204- elif hasattr (func , "__code__" ):
205- code = func .__code__
206- elif hasattr (func , "f_code" ):
207- code = func .f_code
205+ elif hasattr (func_or_code , "__code__" ):
206+ code = func_or_code .__code__
207+ elif hasattr (func_or_code , "f_code" ):
208+ code = func_or_code .f_code
208209 else :
209- print (f"Don't know what to do with { func } , { type (func )} " )
210+ print (f"Don't know what to do with { func_or_code } , { type (func_or_code )} " )
210211 return
211212 brkpt = Breakpoint (bpnum , filename , lineno , temporary , condition , code , offset )
212213
213214 # Build the internal lists of breakpoints
214215 self .bpbynumber .append (brkpt )
215- if (filename , lineno ) in self .bplist :
216- self .bplist [filename , lineno ].append (brkpt )
217- else :
218- self .bplist [filename , lineno ] = [brkpt ]
219- pass
220- if func and offset in [None , - 1 ]:
221- if code in self .code_list :
222- self .code_list [code ].append (brkpt )
223- else :
224- self .code_list [code ] = [brkpt ]
225- pass
216+ self .bplist [filename , lineno ].append (brkpt )
217+ if func_or_code and offset in [None , - 1 ]:
218+ self .code_list [code ].append (brkpt )
226219 return brkpt
227220
228221 def delete_all_breakpoints (self ) -> str :
0 commit comments