Pulse-Eight / libcec

USB CEC Adapter communication Library http://libcec.pulse-eight.com/
Other
714 stars 287 forks source link

Python library sometimes raises UnboundLocalError when opening device #586

Closed PureTryOut closed 2 years ago

PureTryOut commented 2 years ago

Using libcec 6.0.2.

Relevant code:

cecconfig = cec.libcec_configuration()    
cecconfig.strDeviceName = "libcec"    
cecconfig.bActivateSource = 0    
cecconfig.deviceTypes.Add(cec.CEC_DEVICE_TYPE_RECORDING_DEVICE)    
cecconfig.clientVersion = cec.LIBCEC_VERSION_CURRENT    

cecconfig.SetKeyPressCallback(keypress_callback)    

lib = cec.ICECAdapter.Create(cecconfig)    

# Wait till we get a CEC device    
print("Detecting CEC adapters...")    
returned_adapter = None    
while returned_adapter is None:    
    adapters = lib.DetectAdapters()    
    for adapter in adapters:    
        print("Found a CEC adapter:")    
        print("Port:    " + adapter.strComName)    
        print("Vendor:  " + hex(adapter.iVendorId))    
        print("Product: " + hex(adapter.iProductId))    
        returned_adapter = adapter.strComName    

try:    
    lib.Open(returned_adapter)    
    print("Connection opened")    

    main_loop()    
except Exception as ex:    
    print(f"Failure while connecting to the adapter. {type(ex)}")

Most of the time calling lib.Open(returned_adapter) succeeds but sometimes it fails and I get a UnboundLocalError exception. A quick internet search tells me this means a local variable is referenced before it has been assigned. This seems to be a libcec bug?

PureTryOut commented 2 years ago

Oh actually never mind, this was a case of me calling an unassigned variable in the main_loop() function. Derp