Skip to content

Commit

Permalink
Merge pull request #661 from casabre/main
Browse files Browse the repository at this point in the history
Adding support for pyvisa_proxy #660
  • Loading branch information
MatthieuDartiailh committed May 3, 2022
2 parents 53a1d41 + b1745ae commit 116862d
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ htmlcov/
.mypy_cache/
pip-wheel-metadata/
.pytest_cache
.venv/
venv/
35 changes: 17 additions & 18 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
repos:
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.5.3
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
language_version: python3.7
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.3
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.782 # Use the sha / tag you want to point at
hooks:
- id: mypy
additional_dependencies: [numpy, typing_extensions]
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.5.3
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.3
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.782 # Use the sha / tag you want to point at
hooks:
- id: mypy
additional_dependencies: [numpy, typing_extensions]
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ PyVISA Changelog
- allow trailing separators in ascii blocks PR #581
Numpy style extraction already handled this case correctly but it was not so
for the builtin parser.
- adding open_resource attribute check for VisaLibraryBase in order to support
custom resource opening #660
- changed constant ControlFlow enum type from IntEnum to IntFlag PR#652
This change also triggers a change in attributes.py to include a new Attribute
class, FlagAttribute where the enum type is declared at IntFlag.
Expand Down
6 changes: 6 additions & 0 deletions docs/source/advanced/backends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ As a **very minimum** set you need:

(you can get the signature below or here :ref:`api_visalibrarybase`)

.. note::

A |ResourceManager| attribute check for
:class:`pyvisa.highlevel.VisaLibraryBase.open_resource` is implemented
in order to support custom opening handling by the new backend implementation.

But of course you cannot do anything interesting with just this. In general you
will also need:

Expand Down
2 changes: 1 addition & 1 deletion pyvisa/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from typing_extensions import Literal

is_64bits = sys.maxsize > 2 ** 32
is_64bits = sys.maxsize > 2**32


def _to_int(x: int) -> int:
Expand Down
47 changes: 25 additions & 22 deletions pyvisa/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3268,7 +3268,6 @@ def open_resource(
Subclass of Resource matching the resource.
"""

if resource_pyclass is None:
info = self.resource_info(resource_name, extended=True)

Expand All @@ -3286,27 +3285,31 @@ def open_resource(
"There is no class defined for %r. Using Resource",
(info.interface_type, info.resource_class),
)

res = resource_pyclass(self, resource_name)
for key in kwargs.keys():
try:
getattr(res, key)
present = True
except AttributeError:
present = False
except errors.InvalidSession:
present = True

if not present:
raise ValueError(
"%r is not a valid attribute for type %s"
% (key, res.__class__.__name__)
)

res.open(access_mode, open_timeout)

for key, value in kwargs.items():
setattr(res, key, value)
if hasattr(self.visalib, "open_resource"):
res = self.visalib.open_resource( # type: ignore
resource_name, access_mode, open_timeout, resource_pyclass, **kwargs
)
else:
res = resource_pyclass(self, resource_name)
for key in kwargs.keys():
try:
getattr(res, key)
present = True
except AttributeError:
present = False
except errors.InvalidSession:
present = True

if not present:
raise ValueError(
"%r is not a valid attribute for type %s"
% (key, res.__class__.__name__)
)

res.open(access_mode, open_timeout)

for key, value in kwargs.items():
setattr(res, key, value)

self._created_resources.add(res)

Expand Down
10 changes: 5 additions & 5 deletions pyvisa/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def do_termchar(self, args):

if not args:
try:
charmap = {u"\r": "CR", u"\n": "LF", u"\r\n": "CRLF", u"\0": "NUL"}
charmap = {"\r": "CR", "\n": "LF", "\r\n": "CRLF", "\0": "NUL"}
chr = self.current.read_termination
if chr in charmap:
chr = charmap[chr]
Expand All @@ -349,10 +349,10 @@ def do_termchar(self, args):
)
else:
charmap = {
"CR": u"\r",
"LF": u"\n",
"CRLF": u"\r\n",
"NUL": u"\0",
"CR": "\r",
"LF": "\n",
"CRLF": "\r\n",
"NUL": "\0",
"None": None,
}
chr = args[0]
Expand Down
36 changes: 36 additions & 0 deletions pyvisa/testsuite/fake-extensions/pyvisa_test_open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
"""
"""
from pyvisa import constants
from pyvisa.highlevel import VisaLibraryBase
from pyvisa.util import LibraryPath


class FakeResource:
def close(self):
pass


class FalseVISALib(VisaLibraryBase):
pass

@staticmethod
def get_library_paths():
return (LibraryPath("unset"),)

def _init(self):
pass

def open_resource(self, *args, **kwargs):
self.open_resource_called = True
return FakeResource()

def open_default_resource_manager(self):
return 1, constants.StatusCode.success

def close(self, session):
pass


WRAPPER_CLASS = FalseVISALib
22 changes: 21 additions & 1 deletion pyvisa/testsuite/test_highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import logging
import os
import sys
from importlib import import_module

import pytest

from pyvisa import constants, highlevel, resources, rname
from pyvisa import ResourceManager, constants, highlevel, resources, rname
from pyvisa.ctwrapper import IVIVisaLibrary

from . import BaseTestCase
Expand Down Expand Up @@ -247,3 +248,22 @@ def test_base_get_library_paths(self):
def test_base_get_debug_info(self):
"""Test the base class implementation of get_debug_info."""
assert len(highlevel.VisaLibraryBase.get_debug_info()) == 1

def test_open_resource_attr(self, caplog):
"""Test handling errors when trying to open a Visa library."""
highlevel._WRAPPERS.clear()

path = os.path.join(os.path.dirname(__file__), "fake-extensions")
sys.path.append(path)
try:
pkg = import_module("pyvisa_test_open")
highlevel.get_wrapper_class("test_open")
rm = ResourceManager("@test_open")
assert rm is not None
finally:
sys.path.remove(path)

instr = rm.open_resource("TCPIP::192.168.0.1::INSTR")
assert isinstance(instr, pkg.FakeResource)
assert rm.visalib.open_resource_called
rm.close()

0 comments on commit 116862d

Please sign in to comment.