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 8882843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:40:36+00:00 2026-06-14T20:40:36+00:00

I want to use an event based Python library in a C application. I

  • 0

I want to use an event based Python library in a C application. I use the offical C-API for embedding Python: http://docs.python.org/2/c-api/index.html#c-api-index

It is no problem to call methods from C and collect return values. However, I don’t know how to do the following:

Several of the python library functions take what I think is a python function pointer as an argument.

Is it possible, when calling these methods from C, to pass a C function pointer so the Python function uses a C function as callback?

If not, how to accomplish having Python use a C callback?

  • 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-06-14T20:40:37+00:00Added an answer on June 14, 2026 at 8:40 pm

    This is harder than one would expect, but it can be done.

    If you have a single C function that you want to provide as a callback, you can use PyCFunction_New to convert it into a Python callable:

    #include <python.h>
    
    static PyObject *my_callback(PyObject *ignore, PyObject *args)
    {
      /* ... */
    }
    
    static struct PyMethodDef callback_descr = {
      "function_name",
      (PyCFunction) my_callback,
      METH_VARARGS,                 /* or METH_O, METH_NOARGS, etc. */
      NULL
    };
    
    static PyObject *py_callback;
    
    ...
    py_callback = PyCFunction_New(&callback_descr, NULL);
    

    This approach won’t work if you want to choose different callbacks at run-time, e.g. to provide a generic c_to_python function that converts a C callback function to a Python callback. In that case, you’ll need to implement an extension type with its own tp_call.

    typedef struct {
      PyObject_HEAD
      static PyObject (*callback)(PyObject *, PyObject *);
    } CallbackObject;
    
    static PyObject *
    callback_call(CallbackObject *self, PyObject *args, PyObject *kwds)
    {
      return self->callback(args, kwds);
    }
    
    static PyTypeObject CallbackType = {
        PyObject_HEAD_INIT(NULL)
        0,                          /*ob_size*/
        "Callback",                 /*tp_name*/
        sizeof(CallbackObject),     /*tp_basicsize*/
        0,                          /*tp_itemsize*/
        0,                          /*tp_dealloc*/
        0,                          /*tp_print*/
        0,                          /*tp_getattr*/
        0,                          /*tp_setattr*/
        0,                          /*tp_compare*/
        0,                          /*tp_repr*/
        0,                          /*tp_as_number*/
        0,                          /*tp_as_sequence*/
        0,                          /*tp_as_mapping*/
        0,                          /*tp_hash */
        (ternaryfunc) callback_call, /*tp_call*/
        0,                          /*tp_str*/
        0,                          /*tp_getattro*/
        0,                          /*tp_setattro*/
        0,                          /*tp_as_buffer*/
        Py_TPFLAGS_DEFAULT,         /*tp_flags*/
    };
    
    PyObject *
    c_to_python(PyObject (*callback)(PyObject *, PyObject *))
    {
      CallbackObject *pycallback = PyObject_New(CallbackObject, &CallbackType);
      if (pycallback)
        pycallback->callback = callback;
      return pycallback;
    }
    

    This code is trivially extended for the to also accept a user_data pointer; just store the user data in the CallbackObject struct.

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

Sidebar

Related Questions

I want to use Event Aggregator to allow communication between presenters on the page.
I want to use boost::timed_wait to wait for an event or to timeout after
I am making a phonegap program and I want to use a .on('tap') event
The documentation says to use: $(input[type='checkbox']).bind( change, function(event, ui) { But I want to
I am using wxPython and I want to use an OpenGL based canvas, but
I want to develop one application where it will automatically (based on some logic)
I want to consider the latest event based on the payload i.e StockTicksData id
I want to use a signals/slots library in a project that doesn't use QT.
I recently downloaded an event calendar from this link: http://www.asp.net/downloads/starter-kits/club/EventCalendar.zip I want to use
I want use this 1 for using Bar code or QR code scanner. I

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.