Skip to content

Commit

Permalink
Moves list_resources to the library and call it from resource manager
Browse files Browse the repository at this point in the history
PyVISA was leaking memory because a resource was not properly closed.
Until this commit, `list_resources` was implemented in the ResourceManager
and called `find_resources` and `find_next` in the library.

This design is flawed because different backends might implement this
differently. Moreover, it required all backends to implement
`find_resources` and `find_next` instead of more direct code.

See #135
  • Loading branch information
hgrecco committed Apr 16, 2015
1 parent 4e14682 commit c4e6663
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
24 changes: 24 additions & 0 deletions pyvisa/ctwrapper/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,28 @@ def _return_handler(self, ret_value, func, arguments):

return ret_value

def list_resources(self, query='?*::INSTR'):
"""Returns a tuple of all connected devices matching query.
:param query: regular expression used to match devices.
"""

lib = self.lib

resources = []

try:
find_list, return_counter, instrument_description, err = lib.find_resources(self.session, query)
except errors.VisaIOError as e:
if e.error_code == constants.StatusCode.error_resource_not_found:
return tuple()
raise e

resources.append(instrument_description)
for i in range(return_counter - 1):
resources.append(lib.find_next(find_list)[0])

lib.close(find_list)

return tuple(resource for resource in resources)

24 changes: 8 additions & 16 deletions pyvisa/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,13 @@ def install_handler(self, session, event_type, handler, user_handle):
"""
raise NotImplementedError

def list_resources(self, query='?*::INSTR'):
"""Returns a tuple of all connected devices matching query.
:param query: regular expression used to match devices.
"""
raise NotImplementedError

def lock(self, session, lock_type, timeout, requested_key=None):
"""Establishes an access mode to the specified resources.
Expand Down Expand Up @@ -1546,22 +1553,7 @@ def list_resources(self, query='?*::INSTR'):
:param query: regular expression used to match devices.
"""

lib = self.visalib

resources = []

try:
find_list, return_counter, instrument_description, err = lib.find_resources(self.session, query)
except errors.VisaIOError as e:
if e.error_code == constants.StatusCode.error_resource_not_found:
return tuple()
raise e

resources.append(instrument_description)
for i in range(return_counter - 1):
resources.append(lib.find_next(find_list)[0])

return tuple(resource for resource in resources)
return self.visalib.list_resources(query)

def list_resources_info(self, query='?*::INSTR'):
"""Returns a dictionary mapping resource names to resource extended
Expand Down

0 comments on commit c4e6663

Please sign in to comment.