33import re
44import sys
55import typing as t
6+ from pathlib import Path
67
78import zmq
89from IPython .core .getipython import get_ipython
2021
2122try :
2223 # This import is required to have the next ones working...
23- from debugpy .server import api # noqa
24+ from debugpy .server import api
2425
2526 from _pydevd_bundle import pydevd_frame_utils # isort: skip
2627 from _pydevd_bundle .pydevd_suspended_frames import ( # isort: skip
@@ -179,10 +180,10 @@ def put_tcp_frame(self, frame):
179180 self .tcp_buffer = ""
180181 self ._reset_tcp_pos ()
181182 return
182- else :
183- self .tcp_buffer = self .tcp_buffer [self .message_pos + self .message_size :]
184- self .log .debug ("QUEUE - slicing tcp_buffer: %s" , self .tcp_buffer )
185- self ._reset_tcp_pos ()
183+
184+ self .tcp_buffer = self .tcp_buffer [self .message_pos + self .message_size :]
185+ self .log .debug ("QUEUE - slicing tcp_buffer: %s" , self .tcp_buffer )
186+ self ._reset_tcp_pos ()
186187
187188 async def get_message (self ):
188189 """Get a message from the queue."""
@@ -256,8 +257,7 @@ async def _handle_init_sequence(self):
256257 await self ._wait_for_response ()
257258
258259 # 4] Waits for attachResponse and returns it
259- attach_rep = await self ._wait_for_response ()
260- return attach_rep
260+ return await self ._wait_for_response ()
261261
262262 def get_host_port (self ):
263263 """Get the host debugpy port."""
@@ -294,11 +294,11 @@ async def send_dap_request(self, msg):
294294 rep = await self ._handle_init_sequence ()
295295 self .wait_for_attach = False
296296 return rep
297- else :
298- rep = await self ._wait_for_response ()
299- self .log .debug ("DEBUGPYCLIENT - returning:" )
300- self .log .debug (rep )
301- return rep
297+
298+ rep = await self ._wait_for_response ()
299+ self .log .debug ("DEBUGPYCLIENT - returning:" )
300+ self .log .debug (rep )
301+ return rep
302302
303303
304304class Debugger :
@@ -363,9 +363,8 @@ def _handle_event(self, msg):
363363 self .stopped_queue .put_nowait (msg )
364364 # Do not forward the event now, will be done in the handle_stopped_event
365365 return
366- else :
367- self .stopped_threads .add (msg ["body" ]["threadId" ])
368- self .event_callback (msg )
366+ self .stopped_threads .add (msg ["body" ]["threadId" ])
367+ self .event_callback (msg )
369368 elif msg ["event" ] == "continued" :
370369 if msg ["body" ]["allThreadsContinued" ]:
371370 self .stopped_threads = set ()
@@ -380,15 +379,14 @@ async def _forward_message(self, msg):
380379
381380 def _build_variables_response (self , request , variables ):
382381 var_list = [var for var in variables if self .accept_variable (var ["name" ])]
383- reply = {
382+ return {
384383 "seq" : request ["seq" ],
385384 "type" : "response" ,
386385 "request_seq" : request ["seq" ],
387386 "success" : True ,
388387 "command" : request ["command" ],
389388 "body" : {"variables" : var_list },
390389 }
391- return reply
392390
393391 def _accept_stopped_thread (self , thread_name ):
394392 # TODO: identify Thread-2, Thread-3 and Thread-4. These are NOT
@@ -416,8 +414,8 @@ def start(self):
416414 """Start the debugger."""
417415 if not self .debugpy_initialized :
418416 tmp_dir = get_tmp_directory ()
419- if not os . path . exists (tmp_dir ):
420- os . makedirs (tmp_dir )
417+ if not Path ( tmp_dir ). exists ():
418+ Path (tmp_dir ). mkdir ( parents = True )
421419 host , port = self .debugpy_client .get_host_port ()
422420 code = "import debugpy;"
423421 code += 'debugpy.listen(("' + host + '",' + port + "))"
@@ -460,14 +458,13 @@ async def dumpCell(self, message):
460458 with open (file_name , "w" , encoding = "utf-8" ) as f :
461459 f .write (code )
462460
463- reply = {
461+ return {
464462 "type" : "response" ,
465463 "request_seq" : message ["seq" ],
466464 "success" : True ,
467465 "command" : message ["command" ],
468466 "body" : {"sourcePath" : file_name },
469467 }
470- return reply
471468
472469 async def setBreakpoints (self , message ):
473470 """Handle a set breakpoints message."""
@@ -487,7 +484,7 @@ async def source(self, message):
487484 """Handle a source message."""
488485 reply = {"type" : "response" , "request_seq" : message ["seq" ], "command" : message ["command" ]}
489486 source_path = message ["arguments" ]["source" ]["path" ]
490- if os . path . isfile (source_path ):
487+ if Path (source_path ). is_file ( ):
491488 with open (source_path , encoding = "utf-8" ) as f :
492489 reply ["success" ] = True
493490 reply ["body" ] = {"content" : f .read ()}
@@ -547,7 +544,7 @@ def accept_variable(self, variable_name):
547544 cond = variable_name not in forbid_list
548545 cond = cond and not bool (re .search (r"^_\d" , variable_name ))
549546 cond = cond and variable_name [0 :2 ] != "_i"
550- return cond
547+ return cond # noqa: RET504
551548
552549 async def variables (self , message ):
553550 """Handle a variables message."""
@@ -557,12 +554,12 @@ async def variables(self, message):
557554 message ["arguments" ]["variablesReference" ]
558555 )
559556 return self ._build_variables_response (message , variables )
560- else :
561- reply = await self ._forward_message (message )
562- # TODO : check start and count arguments work as expected in debugpy
563- reply ["body" ]["variables" ] = [
564- var for var in reply ["body" ]["variables" ] if self .accept_variable (var ["name" ])
565- ]
557+
558+ reply = await self ._forward_message (message )
559+ # TODO : check start and count arguments work as expected in debugpy
560+ reply ["body" ]["variables" ] = [
561+ var for var in reply ["body" ]["variables" ] if self .accept_variable (var ["name" ])
562+ ]
566563 return reply
567564
568565 async def attach (self , message ):
@@ -580,21 +577,20 @@ async def attach(self, message):
580577
581578 async def configurationDone (self , message ):
582579 """Handle a configuration done message."""
583- reply = {
580+ return {
584581 "seq" : message ["seq" ],
585582 "type" : "response" ,
586583 "request_seq" : message ["seq" ],
587584 "success" : True ,
588585 "command" : message ["command" ],
589586 }
590- return reply
591587
592588 async def debugInfo (self , message ):
593589 """Handle a debug info message."""
594590 breakpoint_list = []
595591 for key , value in self .breakpoint_list .items ():
596592 breakpoint_list .append ({"source" : key , "breakpoints" : value })
597- reply = {
593+ return {
598594 "type" : "response" ,
599595 "request_seq" : message ["seq" ],
600596 "success" : True ,
@@ -611,7 +607,6 @@ async def debugInfo(self, message):
611607 "exceptionPaths" : ["Python Exceptions" ],
612608 },
613609 }
614- return reply
615610
616611 async def inspectVariables (self , message ):
617612 """Handle an insepct variables message."""
@@ -665,7 +660,7 @@ async def richInspectVariables(self, message):
665660 }
666661 )
667662 if reply ["success" ]:
668- repr_data , repr_metadata = eval (reply ["body" ]["result" ], {}, {}) # noqa: S307
663+ repr_data , repr_metadata = eval (reply ["body" ]["result" ], {}, {}) # noqa: PGH001
669664
670665 body = {
671666 "data" : repr_data ,
@@ -708,8 +703,7 @@ async def modules(self, message):
708703 if filename and filename .endswith (".py" ):
709704 mods .append ({"id" : i , "name" : module .__name__ , "path" : filename })
710705
711- reply = {"body" : {"modules" : mods , "totalModules" : len (modules )}}
712- return reply
706+ return {"body" : {"modules" : mods , "totalModules" : len (modules )}}
713707
714708 async def process_request (self , message ):
715709 """Process a request."""
0 commit comments