Re: Python bindings and a suggestion for improvement

Tomislav Ivek <[email protected]>
Newsgroups gmane.linux.hardware.gpib.general
Message-ID <CAAgNE5kqicgcebNr6NevqOounkUdzv06_JmMs17fj0DwwQYQ8g@mail.gmail.com>
Hi Frank, thanks for the reply. Attached is the patch that introduces a
safe Gpib.Gpib.close() which will ensure proper cleanup. Please take a look.

Cheers,
Tomislav


On Wed, Nov 28, 2018 at 11:15 AM Frank Mori Hess <[email protected]> wrote:

> On Tue, Nov 27, 2018 at 4:19 PM Tomislav Ivek <[email protected]>
> wrote:
> >
> > The pure-Python VISA backend pyvisa-py has recently stumbled upon a
> double-close situation with linux-gpib's Python class Gpib.Gpib:
> https://github.com/pyvisa/pyvisa-py/pull/171 This bug is related to a
> design point in Gpib.Gpib which cleans up its GPIB handle only on Python's
> GC cycle. This is not guaranteed to run when the programmer expects it to
> and in extreme cases might even leak resources. It appears Gpib.Gpib has no
> other user-facing method to close its GPIB handle.
> >
> > This can be resolved by adding a Gpib.close() method for user-controlled
> cleanup. I hav ealready tested the fix in gpib_ctypes, a cross-platform
> GPIB binding that aims to be compatible with linux-gpib (
> https://pypi.org/project/gpib-ctypes/). I would be happy to send a patch
> for linux-gpib's Python lib.
> >
> > Please let me know what you think about that.
>
>
> I'm no python expert, but your change sounds reasonable to me.
>

_______________________________________________
Linux-gpib-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-gpib-general
Gpib_py_close.patch (text/x-patch, 619 B)
Index: linux-gpib-user/language/python/Gpib.py
===================================================================
--- linux-gpib-user/language/python/Gpib.py	(revision 1766)
+++ linux-gpib-user/language/python/Gpib.py	(working copy)
@@ -28,13 +28,17 @@
 
 	# automatically close descriptor when instance is deleted
 	def __del__(self):
-		if self._own:
-			gpib.close(self.id)
+		self.close()
 
 	def __repr__(self):
 		return "%s(%d)" % (self.__class__.__name__, self.id)
 
 
+	def close(self):
+		if self._own:
+			gpib.close(self.id)
+			self._own = False
+
 	def command(self,str):
 		gpib.command(self.id, str)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.