Skip to content

Commit bc97744

Browse files
committed
refactor(debugger): some code simplifications
1 parent 3c010d4 commit bc97744

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

Diff for: packages/debugger/src/robotcode/debugger/debugger.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from __future__ import annotations
2-
3-
import asyncio
41
import itertools
52
import os
63
import pathlib
@@ -157,7 +154,7 @@ def __init__(self, thread_id: Any) -> None:
157154
class StackFrameEntry:
158155
def __init__(
159156
self,
160-
parent: Optional[StackFrameEntry],
157+
parent: Optional["StackFrameEntry"],
161158
context: Any,
162159
name: str,
163160
type: str,
@@ -193,7 +190,7 @@ def __init__(
193190
def __repr__(self) -> str:
194191
return f"StackFrameEntry({self.name!r}, {self.type!r}, {self.source!r}, {self.line!r}, {self.column!r})"
195192

196-
def get_first_or_self(self) -> StackFrameEntry:
193+
def get_first_or_self(self) -> "StackFrameEntry":
197194
if self.stack_frames:
198195
return self.stack_frames[0]
199196
return self
@@ -260,14 +257,14 @@ def end_keyword(self, data: running.Keyword, result: result.Keyword) -> None:
260257

261258

262259
class Debugger:
263-
__instance: ClassVar[Optional[Debugger]] = None
260+
__instance: ClassVar[Optional["Debugger"]] = None
264261
__lock: ClassVar = threading.RLock()
265262
__inside_instance: ClassVar = False
266263

267264
_logger = LoggingDescriptor()
268265

269266
@classmethod
270-
def instance(cls) -> Debugger:
267+
def instance(cls) -> "Debugger":
271268
if cls.__instance is not None:
272269
return cls.__instance
273270
with cls.__lock:
@@ -318,7 +315,6 @@ def __init__(self) -> None:
318315
self.terminated = False
319316
self.attached = False
320317
self.path_mappings: List[PathMapping] = []
321-
self.server_loop: Optional[asyncio.AbstractEventLoop] = None
322318

323319
self._keyword_to_evaluate: Optional[Callable[..., Any]] = None
324320
self._evaluated_keyword_result: Any = None
@@ -917,14 +913,12 @@ def end_suite(self, name: str, attributes: Dict[str, Any]) -> None:
917913
status = attributes.get("status", "")
918914

919915
if status == "FAIL":
920-
if self.server_loop:
921-
self.process_end_state(
922-
status,
923-
{"failed_suite"},
924-
"Suite failed.",
925-
f"Suite failed{f': {v}' if (v := attributes.get('message')) else ''}",
926-
)
927-
916+
self.process_end_state(
917+
status,
918+
{"failed_suite"},
919+
"Suite failed.",
920+
f"Suite failed{f': {v}' if (v := attributes.get('message')) else ''}",
921+
)
928922
self.wait_for_running()
929923

930924
source = attributes.get("source")

Diff for: packages/debugger/src/robotcode/debugger/protocol.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
List,
1313
Mapping,
1414
Optional,
15+
Set,
1516
Tuple,
1617
Type,
1718
TypeVar,
@@ -81,6 +82,7 @@ def __init__(self) -> None:
8182
self._received_request_lock = threading.RLock()
8283
self._received_request: OrderedDict[int, asyncio.Future[Any]] = OrderedDict()
8384
self._initialized = False
85+
self._running_handle_message_tasks: Set[asyncio.Future[Any]] = set()
8486

8587
@_logger.call
8688
def send_message(self, message: ProtocolMessage) -> None:
@@ -140,14 +142,10 @@ def _handle_body(self, body: bytes, charset: str) -> None:
140142
)
141143

142144
def _handle_messages(self, iterator: Iterator[ProtocolMessage]) -> None:
143-
def done(f: "asyncio.Future[Any]") -> None:
144-
ex = f.exception()
145-
if ex is not None and not isinstance(ex, asyncio.CancelledError):
146-
self._logger.exception(ex, exc_info=ex)
147-
148145
for m in iterator:
149146
task = asyncio.create_task(self.handle_message(m), name="handle_message")
150-
task.add_done_callback(done)
147+
self._running_handle_message_tasks.add(task)
148+
task.add_done_callback(self._running_handle_message_tasks.discard)
151149

152150
@_logger.call
153151
async def handle_message(self, message: ProtocolMessage) -> None:

Diff for: packages/debugger/src/robotcode/debugger/run.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ def run_debugger(
230230
Debugger.instance().colored_output = app.colored
231231
Debugger.instance().debug = debug
232232
Debugger.instance().set_main_thread(threading.current_thread())
233-
Debugger.instance().server_loop = server.loop
234233

235234
app.verbose("Start the debugger instance")
236235
Debugger.instance().start()
@@ -267,7 +266,7 @@ def run_debugger(
267266
server.protocol.terminate()
268267

269268
if not server.protocol.wait_for_disconnected():
270-
app.warning("Timeout to get disconnected from client")
269+
app.verbose("Timeout to get disconnected from client")
271270

272271
server.loop.stop()
273272

0 commit comments

Comments
 (0)