Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 779465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:55:52+00:00 2026-05-14T19:55:52+00:00

I’m wanted to convert some of my python code to C++ for speed but

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:55:53+00:00Added an answer on May 14, 2026 at 7:55 pm

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.