Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Odd Python / C Library interaction
#1
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:
import DeviceInterface

device_class = DeviceInterface.Device()

print()
def dummy_function_that_does_nothing():
    pass

with device_class:
    pass
This 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:
    pass
These 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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  RTL_Power interaction 25141928 0 256 Apr-08-2019, 07:26 PM
Last Post: 25141928
  Building a class for sqlite3 interaction Nuzvee 5 626 Jan-29-2019, 06:47 AM
Last Post: Nuzvee
  Pygame Button interaction Padalinkiller1 1 3,409 Nov-16-2017, 02:49 PM
Last Post: metulburr
  Interaction with games such as BO3 nmills688 1 854 Oct-16-2017, 08:58 PM
Last Post: nilamo
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 1,719 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:


Users browsing this thread: 1 Guest(s)