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

  • SEARCH
  • Home
  • 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 405645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:25:09+00:00 2026-05-12T17:25:09+00:00

I’m trying to add a python callback to a C++ library as illustrated: template<typename

  • 0

I’m trying to add a python callback to a C++ library as illustrated:

template<typename T> void doCallback(shared_ptr<T> data) {
   PyObject* pyfunc; //I have this already
   PyObject* args = Py_BuildValue("(O)", data);
   PyEval_CallObject(pyfunc,args);
}

This fails because data hasn’t gone through swig, and isn’t a PyObject.

I tried using:

swigData = SWIG_NewPointerObj((void*)data, NULL, 0);

But because its a template, I don’t really know what to use for the second parameter. Even if I do hard code the ‘correct’ SWIGTYPE, it usually segfaults on PyEval_CallObject.

So my questions are:

  1. Whats the best way to invoke swig
    type wrapping?

  2. Am I even going in the right
    direction here? Directors looked
    promising for implementing a
    callback, but I couldn’t find an
    example of directors with python.

Update: The proper wrapping is getting generated. I have other functions that return shared_ptrs and can call those correctly.

  • 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-12T17:25:10+00:00Added an answer on May 12, 2026 at 5:25 pm

    My first answer misunderstood the question completely, so let’s try this again.

    Your central problem is the free type parameter T in the definition of doCallback. As you point out in your question, there’s no way to make a SWIG object out of a shared_ptr<T> without a concrete value for T: shared_ptr<T> isn’t really a type.

    Thus I think that you have to specialize: for each concrete instantiation of doCallback that the host system uses, provide a template specialization for the target type. With that done, you can generate a Python-friendly wrapping of data, and pass it to your python function. The simplest technique for that is probably:

    swigData = SWIG_NewPointerObj((void*)(data.get()), SWIGType_Whatever, 0);
    

    …though this can only work if your Python function doesn’t save its argument anywhere, as the shared_ptr itself is not copied.

    If you do need to retain a reference to data, you’ll need to use whatever mechanism SWIG usually uses to wrap shared_ptr. If there’s no special-case smart-pointer magic going on, it’s probably something like:

    pythonData = new shared_ptr<Whatever>(data);
    swigData = SWIG_NewPointerObj(pythonData, SWIGType_shared_ptr_to_Whatever, 1);
    

    Regardless, you then you have a Python-friendly SWIG object that’s amenable to Py_BuildValue().

    Hope this helps.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when 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.