Thursday, April 4, 2013

Installing pyside-tools on Fedora 14

I was trying to do some tinkering with pyside in Fedora 14 today, but I came across a frustrating error when trying to launch designer-qt4:


undefined symbol: gst_x_overlay_set_window_handle

I needed to downgrade to phonon 4.4.2 from 4.5
Here are the steps I used to get around it:

  1. sudo yum remove phonon
  2. Disable the yum update repo in /etc/yum.repos.d/fedora-updates.repo
  3. sudo yum install phonon
  4. Reenable the yum update repo
  5. sudo yum install pyside-tools
  6. relaunched designer-qt4 and it works as expected


Now I can continue developing in pyside...

Wednesday, February 13, 2013

Copying byte string from a C function to a Python string using ctypes


Disclaimer: I am no Python expert so if anyone has a better solution then let me know.

I was writing a script that would retrieve a byte string from a C library and copy it to a python string object.   I wanted to take those bytes and put them into a Python string object.
Per the ctypes documentation, setting the restype of the function to c_char_p would have simply returned a null-terminated string; a c_void_p is needed instead.

Sample C Function

extern "C" char* createBuffer(int* numBytes)
{
    char* myArray = new char[10000];
    //I'm just using memset to set the data.  
    //Of course your function will fill this array some other way
    memset(myArray, 0, 10000);       
    *numBytes = 10000;
    return myArray;
}

Python Code


from ctypes import *

libMyCLib = cdll.LoadLibrary('libMyCLib.so')
createBufferFunc = libMyCLib.createBuffer

createBufferFunc.restype = c_void_p
bufLen = c_int()

bufferPtr = createBufferFunc(byref(bufLen))

stringBuffer = create_string_buffer(bufLen.value)
memmove(stringBuffer, bufferPtr, bufLen.value)
bufferDataStr = stringBuffer[:]
#bufferDataStr is the python string object containing the buffer data