Skip to content

Commit

Permalink
fix: Update close method to catch VisaIOErrors.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwagoner committed Feb 7, 2024
1 parent aabef07 commit c1e4cd9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ repos:
- id: remove-tabs
- id: forbid-tabs
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.27.4
rev: 0.28.0
hooks:
- id: check-readthedocs
- id: check-dependabot
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Things to be included in the next release go here.
### Fixed

- Fixed the code that detects VISA resource expressions to be able to detect SOCKET resource expressions properly.
- Fixed PI device close method to catch VisaIOErrors and throw a warning closing a PI device connection.

______________________________________________________________________

Expand Down
9 changes: 8 additions & 1 deletion src/tm_devices/drivers/pi/pi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import socket
import time
import warnings

from abc import ABC, abstractmethod
from contextlib import contextmanager
Expand All @@ -13,6 +14,7 @@

from packaging.version import Version
from pyvisa import constants as visa_constants
from pyvisa import VisaIOError

from tm_devices.drivers.device import Device
from tm_devices.drivers.pi._ieee488_2_commands import IEEE4882Commands
Expand Down Expand Up @@ -816,7 +818,12 @@ def _cleanup(self) -> None:

def _close(self) -> None:
"""Close this device and all its used resources and components."""
self._visa_resource.close()
try:
self._visa_resource.close()
except VisaIOError as error:
warnings.warn(
f"Error encountered while closing the visa resource: \n {error}", stacklevel=2
)
self._visa_resource = None # pyright: ignore[reportAttributeAccessIssue]
self._is_open = False

Expand Down
13 changes: 13 additions & 0 deletions tests/test_pi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,16 @@ def test_pi_device( # noqa: PLR0915
stdout = capsys.readouterr().out
assert f"VISA timeout set to: {old_timeout}ms" in stdout
assert scope.visa_timeout == old_timeout

# Test closing a device that is powered off
with mock.patch(
"pyvisa.resources.resource.Resource.close",
mock.MagicMock(side_effect=visa.VisaIOError(123)),
), pytest.warns(Warning):
scope._close() # noqa: SLF001
assert scope._visa_resource is None # noqa: SLF001
assert not scope._is_open # noqa: SLF001

# Re-open the device for device manager teardown
with mock.patch.dict("os.environ", {"TM_DEVICES_UNIT_TESTS_REBOOT_ALLOW": "true"}, clear=True):
assert scope._open() # noqa: SLF001

0 comments on commit c1e4cd9

Please sign in to comment.