views:

98

answers:

2

I'm wanted to convert some of my python code to C++ for speed but it's not as easy as simply making a C++ function and making a few function calls. I have no idea how to get a C++ integer from a python integer object. I have an integer which is an attribute of an object that I want to use. I also have integers which are inside a list in the object which I need to use.

I wanted to test making a C++ extension with this function:

def setup_framebuffer(surface,flip=False):
   #Create texture if not done already
   if surface.texture is None:
      create_texture(surface)
   #Render child to parent
   if surface.frame_buffer is None:
      surface.frame_buffer = glGenFramebuffersEXT(1)
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, c_uint(int(surface.frame_buffer)))
   glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, surface.texture, 0)
   glPushAttrib(GL_VIEWPORT_BIT)
   glViewport(0,0,surface._scale[0],surface._scale[1])
   glMatrixMode(GL_PROJECTION)
   glLoadIdentity() #Load the projection matrix
   if flip:
      gluOrtho2D(0,surface._scale[0],surface._scale[1],0)
   else:
      gluOrtho2D(0,surface._scale[0],0,surface._scale[1])

That function calls create_texture, so I will have to pass that function to the C++ function which I will do with the third argument. This is what I have so far, while trying to follow information on the python documentation:

#include <Python.h>
#include <GL/gl.h>
static PyMethodDef SpamMethods[] = {
   ...
   {"setup_framebuffer",  setup_framebuffer, METH_VARARGS,"Loads a texture from a Surface object to the OpenGL framebuffer."},
   ...
   {NULL, NULL, 0, NULL}        /* Sentinel */
};
static PyObject * setup_framebuffer(PyObject *self, PyObject *args){
   bool flip;
   PyObject *create_texture, *arg_list,*pyflip,*frame_buffer_id;
   if (!PyArg_ParseTuple(args, "OOO", &surface,&pyflip,&create_texture)){
      return NULL;
   }
   if (PyObject_IsTrue(pyflip) == 1){
      flip = true;
   }else{
      flip = false;
   }
   Py_XINCREF(create_texture);
   //Create texture if not done already
   if(texture == NULL){
      arglist = Py_BuildValue("(O)", surface)
      result = PyEval_CallObject(create_texture, arglist);
      Py_DECREF(arglist);
      if (result == NULL){
         return NULL;
      }
      Py_DECREF(result);
   }
   Py_XDECREF(create_texture);
   //Render child to parent
   frame_buffer_id = PyObject_GetAttr(surface, Py_BuildValue("s","frame_buffer"))
   if(surface.frame_buffer == NULL){
      glGenFramebuffersEXT(1,frame_buffer_id);
   }
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, surface.frame_buffer));
   glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, surface.texture, 0);
   glPushAttrib(GL_VIEWPORT_BIT);
   glViewport(0,0,surface._scale[0],surface._scale[1]);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity(); //Load the projection matrix
   if (flip){
      gluOrtho2D(0,surface._scale[0],surface._scale[1],0);
   }else{
      gluOrtho2D(0,surface._scale[0],0,surface._scale[1]);
   }
   Py_INCREF(Py_None);
   return Py_None;
}
PyMODINIT_FUNC initcscalelib(void){
   PyObject *module;
   module = Py_InitModule("cscalelib", Methods);
   if (m == NULL){
      return;
   }
}
int main(int argc, char *argv[]){
   /* Pass argv[0] to the Python interpreter */
   Py_SetProgramName(argv[0]);
   /* Initialize the Python interpreter.  Required. */
   Py_Initialize();
   /* Add a static module */
   initscalelib();
}
+1  A: 

The way to get a C int from a Python integer is with PyInt_AsLong() and a downcast (although you may want to use a C long instead.) To go the other way, you call PyInt_FromLong(). It's not obvious to me where in the code you want to do this, although I do have a couple of other comments about your code:

  • Conceptually, PyObject_IsTrue() returns a boolean. Don't compare it to 1, just use it as a boolean value. However, you should check if it returned an error, which would be -1. (The usual check is < 0.) If you don't check for error returns you end up swallowing the exception, but leaving the exception object hanging around. This is bad.

  • The Py_XINCREF() of create_texture is not necessary. You have a borrowed reference to the args tuple, which in turn owns a reference to the create_texture object. There is no way for create_texture to go away before your function returns. You only need to incref it if you are going to keep it around longer than this functioncall. If you did have to incref it, you wouldn't need to use Py_XINCREF() because it won't ever be NULL. And if you did have to incref it, you would need to remember to decref it in your error return case, as well.

  • Instead of creating an argument tuple just to call PyEval_CallObject(), just call PyObject_CallFunctionObjectArgs(create_texture, arglist, NULL) or PyObject_CallFunction(create_texture, "O", arglist).

  • Py_BuildValue("s", "frame_buffer") is not really the right way to get a Python string for "frame_buffer". A better way is PyString_FromString(). However, both of those return new references, and PyObject_GetAttr() doesn't eat the reference to the attribute name, so you end up leaking that reference. You should use none of these, and instead use PyObject_GetAttrString(), which takes the attribute name as a const char*.

  • Remember to check the return value of all functions that can return an error value (which is almost all Python API functions.) Besides PyTrue_IsTrue() you're also forgetting this for Py_BuildValue() and PyObject_GetAttr().

Thomas Wouters
Thank you. This was very useful. I've managed to get the code to compile fine but there is an import error:ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/cscalelib.so, 2): Symbol not found: _glBindFramebufferEXT Referenced from: /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/cscalelib.so Expected in: dynamic lookupWhy doesn't glBindFramebufferEXT work? I haven't got the foggiest idea.But thank you very much.
Matthew Mitchell
Oh and I compiled on Mac OSX Snow Leopard 10.6.3
Matthew Mitchell
Sounds like you forgot to link to the library that provides that symbol (-lGL, perhaps.)
Thomas Wouters
Thank you. And I should need -lGLU as wel. I don't know how to do this with distutils. I'll take a look and reply as soon as I find out if I manage to.
Matthew Mitchell
No, I can't get it working. I've tried the libraries argument for the Extension function or constructor and that doesn't work for everything I put into it.
Matthew Mitchell
Nor does -framework OpenGL :("linker input file unused because linking not done"Now I remember why I gave up on C++ beforehand.
Matthew Mitchell
It's more of a question about linking to OpenGL on MacOS now, but it still sounds like you're not linking correctly. I don't know about frameworks on MacOS, though, so I can't really be of help, but I would recommend posting a new question with an accurate description and (at least) your setup.py file and the compiler commands it emits.
Thomas Wouters
I'm going to try compiling straight from a gcc command first and then I'll start a new question if it fails.Thank you once again. You have been a great help.
Matthew Mitchell
+1  A: 

May I make a sincere suggestion here? Instead of throwing a hundred lines of code at yourself when trying something new like this, first try the simplest test case possible. Try getting a C++ extension to work that does nothing but pass back the value 123. (Picking 0 or 1 might accidentally work, so I chose an easy to recognize value that is not likely to be an accident.) Then get the extension to double a number you pass in, then add two numbers, etc.

Sneak up on the problem! :-)

Kevin Little
My idea was to learn as much as possible so I can get to converting the code I need without having to learn in small stages.Maybe you are right.Thank you for the answer.
Matthew Mitchell