Oct-09-2019, 12:21 PM
I'm running a script from a Linux terminal with Python 3.6.8 and the script started failing when I tried to expand it with a function definition. I whittled it down to the basics and found that the device fails to connect when there is a function definition followed by a print statement in the code, but not when there's a print statement followed by a function definition. It doesn't even matter what's in the print and whether or not the function definition is used.
This code successfully connects to (and disconnects from) the device:
I would expect no difference between the 2 versions of code above. There seems to be something deeper going on in Python or Linux libraries that I don't understand. I suspect a memory issue in the library, but I do not understand how and why Python is exciting the error by changing what I thought was a meaningless part of the code.
Other things I've tried
This code successfully connects to (and disconnects from) the device:
import DeviceInterface device_class = DeviceInterface.Device() print() def dummy_function_that_does_nothing(): pass with device_class: passThis code, which swaps the function definition and print statement, gives a device connection error:
import DeviceInterface device_class = DeviceInterface.Device() def dummy_function_that_does_nothing(): pass print() with device_class: passThese examples are the exact file contents of the script being run (nothing added or omitted for this post). The DeviceInterface module is a ctypes wrapper around a C-based .so library. That library is not mine and I don't have rights to distribute it, but it's using Aravis v0.6.4. The connection failure is caused by a null pointer being returned from a call to arv_camera_new().
I would expect no difference between the 2 versions of code above. There seems to be something deeper going on in Python or Linux libraries that I don't understand. I suspect a memory issue in the library, but I do not understand how and why Python is exciting the error by changing what I thought was a meaningless part of the code.
Other things I've tried
- I've inserted delays in various places, but none had an effect on whether the device successfully connected, so it doesn't seem to be a timing issue as I originally suspected.
- I tried running both versions a number of times, and the problem has been very repeatably linked to the order of the function definition and the print statement (as opposed to being able to randomly connect).
- If I remove the print statement entirely, it succeeds regardless of where I put the function definition.
- I thought it might have to do with garbage collection killing a socket. I tried disabling the garbage collection with gc.disable() at the start of the script, but it didn't change the behavior.
- This code, which adds an additional function definition, successfully connects:
import DeviceInterface device_class = DeviceInterface.Device() def dummy_function_that_does_nothing(): pass print() def dummy_function_that_does_nothing_again(): pass with device_class: pass
- This code, which adds an additional function definition and another print statement, fails to connect:
import DeviceInterface device_class = DeviceInterface.Device() def dummy_function_that_does_nothing(): pass print() def dummy_function_that_does_nothing_again(): pass print() with device_class: pass
- Changing the print statement to print(flush=True) or print(sys.stderr) did not change the functionality. However, print(end="") caused the problem to go away.
- Running python with unbuffered stdin/stdout/stderr (python3 -u odd_behavior_test.py) caused the failure to go away.