pico-python icon indicating copy to clipboard operation
pico-python copied to clipboard

Fixing bug #180

Open elechapt opened this issue 1 year ago • 2 comments

Catch Exceptions in the _PicoscopeBase() class (picobase.py) that are raised when not connected. They do not convey useful information, as they are typical error happening with the garbage collector and disappearing attributes.

elechapt avatar Jun 26 '23 09:06 elechapt

I'm not sure i agree with the fix.

I think the issue is that:

https://github.com/colinoflynn/pico-python/blob/master/picoscope/ps5000a.py#L172

c_handle.value

could return 0x0 and not Null. therefore we should be "checking" for that in the case of errors and handling that correctly.

hmaarrfk avatar Jun 27 '23 13:06 hmaarrfk

That sure can be a source of error, which raises maybe an OSError. I 'll try to fix that too.

But the AttributeError is very specific, and well documented. We actually should always call explicitly the close() method, and not rely on the __del__ method :

there is no guarantee that __del__() will be called: it's just for memory manager use, not for resource handling.

And after some testing, the real problem with the line you mention is that the next function is doing it's job, the close unit function raises after trying to connect to a given picoscope and failing. Actually, this happens :

  • Try to open a picoscope, _lowLevelOpenUnit failing because it's not connected
  • _lowLevelOpenUnit function raises
  • The object is destroyed, calling __del__, calling close(), calling _lowLevelCloseUnit
  • _lowLevelCloseUnit raises too because c_handle.value is wrong (for a good reason) since the beginning. And we ended up with two raised exception for one information (we're no connected), and that generates ignored exceptions in my code. The fix allow for the last raised exception to be truly ignored, and the AttributeError one to never show up.

elechapt avatar Jun 28 '23 15:06 elechapt