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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:42:31+00:00 2026-05-24T03:42:31+00:00

The current boost::python example for translating a specific C++ exception into python goes like

  • 0

The current boost::python example for translating a specific C++ exception into python goes like this:

void translate (const MyException& e) {
   PyErr_SetString(PyExc_RuntimeError, e.what());
}

boost::python::register_exception_translator<MyException>(translate);

Unfortunately, this requires that we write a specific function for each of our exceptions. We tried to simplify this, by writing a generalized exception translator:

#include <boost/python.hpp>

// Generalized exception translator for Boost Python
template <typename T> struct GeneralizedTranslator {

  public:

    void operator()(const T& cxx_except) const {
      PyErr_SetString(m_py_except, cxx_except.what());
    }

    GeneralizedTranslator(PyObject* py_except): m_py_except(py_except) {
      boost::python::register_exception_translator<T>(this);
    }

    GeneralizedTranslator(const GeneralizedTranslator& other): m_py_except(other.m_py_except) {
      //attention: do not re-register the translator!
    }

  private:

    PyObject* m_py_except;

};

//allows for a simple translation declaration, removes scope problem
template <typename T> void translate(PyObject* e) {
  ExceptionTranslator<T> my_translator(e);
}

Would that bit of code work, you could wrap exceptions that implement “what()” like this:

BOOST_PYTHON_MODULE(libtest)
{
  translate<std::out_of_range>(PyExc_RuntimeError);
}

Unfortunately, it seems boost::python will call the “translation” code as a function inside “boost/python/detail/translate_exception.hpp” (line 61):

translate(e);

In our generalized exception handler this would be a call to GeneralizedTranslator::operator() and that will not work for g++, giving:

error: ‘translate’ cannot be used as a function

Is there a correct way to write this?

  • 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-24T03:42:31+00:00Added an answer on May 24, 2026 at 3:42 am

    You’re passing the this pointer as the translate function, and this fails because a pointer to an object can’t be called as a function. If you pass *this instead this should work (note that this will copy-construct the GeneralizedTranslator object). Or you can move the registration out of the constructor, and call

    register_exception_translator<std::out_of_range>(GeneralizedTranslator<std::out_of_range>(PyExc_RuntimeError));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie in boost.python and i'm getting this error that I would like
how can I get current time with library boost. I can do this: ptime
According to the draft C++0x standard, this code: void simplethread() { boost::thread t(someLongRunningFunction); //
Alright, this is my current code snippet: namespace bai = boost::asio::ip; bai::tcp::socket tcp_connect(std::string hostname,
My current project is in Rails. Coming from a Symfony (PHP) and Django (Python)
My current position is this: if I thoroughly test my ASP.NET applications using web
I've tried the following code with both normal ifstreams and the current boost:iostream I'm
I'm getting path to current directory with boost filesystem, then checking if the directory
In my current project I am using boost::shared_ptr quite extensively. Recently my fellow team
I'm currently using boost::intrusive_ptr together with my GUI classes. Although this is more or

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.