Skip to content

Commit f6377cc

Browse files
committed
handle cancellations separately, avoid recursing on get_error
fixes #14 (probably)
1 parent d412a99 commit f6377cc

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

src/scpi/scpi.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async def get_error(self) -> Tuple[int, str]:
158158
raise RuntimeError("Recursion on get_error detected")
159159
try:
160160
self._checking_error = True
161-
response = await self.ask("SYST:ERR?")
161+
response = await self.ask("SYST:ERR?", auto_check_error=False)
162162
match = ERROR_RE.search(response)
163163
if not match:
164164
# PONDER: Make our own exceptions ??
@@ -176,7 +176,12 @@ async def check_error(self, prev_command: str = "") -> None:
176176
raise CommandError(prev_command, code, errstr)
177177

178178
async def command(
179-
self, command: str, cmd_timeout: float = COMMAND_DEFAULT_TIMEOUT, abort_on_timeout: bool = True
179+
self,
180+
command: str,
181+
cmd_timeout: float = COMMAND_DEFAULT_TIMEOUT,
182+
abort_on_timeout: bool = True,
183+
*,
184+
auto_check_error: bool = True,
180185
) -> None:
181186
"""Sends a command, does not wait for response"""
182187
try:
@@ -188,13 +193,16 @@ async def _command(command: str) -> None:
188193
await self.transport.send_command(command)
189194

190195
await asyncio.wait_for(_command(command), timeout=cmd_timeout)
191-
except (asyncio.TimeoutError, asyncio.CancelledError) as err:
196+
except asyncio.TimeoutError as err:
192197
# check for the actual error if available
193-
await self.check_error(command)
198+
if auto_check_error:
199+
await self.check_error(command)
194200
if abort_on_timeout:
195201
await self.abort_command()
196202
# re-raise the timeout if no other error found
197203
raise err
204+
except asyncio.CancelledError:
205+
LOGGER.info("Cancelled")
198206
# other errors are allowed to bubble-up as-is
199207

200208
async def safe_command(self, command: str, *args: Any, **kwargs: Any) -> None:
@@ -203,7 +211,12 @@ async def safe_command(self, command: str, *args: Any, **kwargs: Any) -> None:
203211
await self.check_error(command)
204212

205213
async def ask(
206-
self, command: str, cmd_timeout: float = COMMAND_DEFAULT_TIMEOUT, abort_on_timeout: bool = True
214+
self,
215+
command: str,
216+
cmd_timeout: float = COMMAND_DEFAULT_TIMEOUT,
217+
abort_on_timeout: bool = True,
218+
*,
219+
auto_check_error: bool = True,
207220
) -> str:
208221
"""Send a command and waits for response, returns the response"""
209222
try:
@@ -216,13 +229,18 @@ async def _ask(command: str) -> str:
216229
return await self.transport.get_response()
217230

218231
return await asyncio.wait_for(_ask(command), timeout=cmd_timeout)
219-
except (asyncio.TimeoutError, asyncio.CancelledError) as err:
232+
except asyncio.TimeoutError as err:
220233
# check for the actual error if available
221-
await self.check_error(command)
234+
if auto_check_error:
235+
await self.check_error(command)
222236
if abort_on_timeout:
223237
self.abort_command()
224238
# re-raise the timeout if no other error found
225239
raise err
240+
except asyncio.CancelledError:
241+
LOGGER.info("Cancelled")
242+
# gotta return something or raise an error
243+
raise
226244
# other errors are allowed to bubble-up as-is
227245

228246
async def safe_ask(self, command: str, *args: Any, **kwargs: Any) -> str:

0 commit comments

Comments
 (0)