Skip to content

Commit

Permalink
sessions: improve handling of open connection error for TCPIP resources
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthieuDartiailh committed Sep 17, 2024
1 parent 8809a21 commit 7959ea5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
3 changes: 3 additions & 0 deletions pyvisa_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
"""

import logging
from importlib.metadata import PackageNotFoundError, version

LOGGER = logging.getLogger("pyvisa.pyvisa-py")

__version__ = "unknown"
try:
__version__ = version(__name__)
Expand Down
5 changes: 4 additions & 1 deletion pyvisa_py/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ def open(
parsed.interface_type_const, parsed.resource_class
)

sess = cls(session, resource_name, parsed, open_timeout)
try:
sess = cls(session, resource_name, parsed, open_timeout)
except sessions.OpenError as e:
return 0, e.error_code

Check warning on line 171 in pyvisa_py/highlevel.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/highlevel.py#L168-L171

Added lines #L168 - L171 were not covered by tests

return self._register(sess), StatusCode.success

Expand Down
7 changes: 7 additions & 0 deletions pyvisa_py/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
T = TypeVar("T", bound=Type["Session"])


class OpenError(Exception):
"""Custom exception signaling we failed to open a resource."""

def __init__(self, error_code: int = StatusCode.error_resource_not_found):
self.error_code = error_code

Check warning on line 39 in pyvisa_py/sessions.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/sessions.py#L39

Added line #L39 was not covered by tests


class UnknownAttribute(Exception):
"""Custom exception signaling a VISA attribute is not supported."""

Expand Down
55 changes: 37 additions & 18 deletions pyvisa_py/tcpip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
from pyvisa import attributes, constants, errors, rname
from pyvisa.constants import BufferOperation, ResourceAttribute, StatusCode

from . import common
from . import common, LOGGER
from .protocols import hislip, rpc, vxi11
from .sessions import Session, UnknownAttribute, VISARMSession
from .sessions import OpenError, Session, UnknownAttribute, VISARMSession


# Let psutil be optional dependency
try:
Expand Down Expand Up @@ -135,17 +136,25 @@ def after_parsing(self) -> None:
else:
sub_address = self.parsed.lan_device_name
port = 4880
self.interface = hislip.Instrument(
self.parsed.host_address,
open_timeout=(
self.open_timeout * 1000.0
if self.open_timeout is not None
else self.open_timeout
),
timeout=self.timeout,
port=port,
sub_address=sub_address,
)

try:
self.interface = hislip.Instrument(

Check warning on line 141 in pyvisa_py/tcpip.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/tcpip.py#L140-L141

Added lines #L140 - L141 were not covered by tests
self.parsed.host_address,
open_timeout=(
self.open_timeout * 1000.0
if self.open_timeout is not None
else self.open_timeout
),
timeout=self.timeout,
port=port,
sub_address=sub_address,
)
except OSError as e:
LOGGER.exception(

Check warning on line 153 in pyvisa_py/tcpip.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/tcpip.py#L152-L153

Added lines #L152 - L153 were not covered by tests
f"Failed to open HiSLIP connection to {self.parsed.host_address} "
f"on port {port} with lan device name {sub_address}"
)
raise OpenError() from e

Check warning on line 157 in pyvisa_py/tcpip.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/tcpip.py#L157

Added line #L157 was not covered by tests

# initialize the constant attributes
self.attrs[ResourceAttribute.dma_allow_enabled] = constants.VI_FALSE
Expand Down Expand Up @@ -482,7 +491,10 @@ def after_parsing(self) -> None:
try:
self.interface = Vxi11CoreClient(host_address, port, self.open_timeout)
except rpc.RPCError:
raise errors.VisaIOError(constants.VI_ERROR_RSRC_NFOUND)
LOGGER.exception(

Check warning on line 494 in pyvisa_py/tcpip.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/tcpip.py#L494

Added line #L494 was not covered by tests
f"Failed to open VX11 connection to {host_address} on port {port}"
)
raise OpenError()

Check warning on line 497 in pyvisa_py/tcpip.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/tcpip.py#L497

Added line #L497 was not covered by tests

# vxi11 expect all timeouts to be expressed in ms and should be integers
self.lock_timeout = 10000
Expand Down Expand Up @@ -511,7 +523,7 @@ def close(self) -> StatusCode:
try:
self.interface.destroy_link(self.link)
except (errors.VisaIOError, socket.error, rpc.RPCError) as e:
print("Error closing VISA link: {}".format(e))
LOGGER.error("Error closing VISA link: {}".format(e))

Check warning on line 526 in pyvisa_py/tcpip.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/tcpip.py#L526

Added line #L526 was not covered by tests

self.interface.close()
self.link = 0
Expand Down Expand Up @@ -870,9 +882,16 @@ def after_parsing(self) -> None:
else:
port = 1861

self.interface = pyvicp.Client(
self.parsed.host_address, port, timeout=self.timeout
)
try:
self.interface = pyvicp.Client(

Check warning on line 886 in pyvisa_py/tcpip.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/tcpip.py#L885-L886

Added lines #L885 - L886 were not covered by tests
self.parsed.host_address, port, timeout=self.timeout
)
except OSError as e:
LOGGER.exception(

Check warning on line 890 in pyvisa_py/tcpip.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/tcpip.py#L889-L890

Added lines #L889 - L890 were not covered by tests
f"Failed to open VICP connection to {self.parsed.host_address} "
f"on port {port}"
)
raise OpenError() from e

Check warning on line 894 in pyvisa_py/tcpip.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/tcpip.py#L894

Added line #L894 was not covered by tests

# initialize the constant attributes
for name in ("SEND_END_EN", "TERMCHAR", "TERMCHAR_EN"):
Expand Down

0 comments on commit 7959ea5

Please sign in to comment.