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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:09:51+00:00 2026-05-22T18:09:51+00:00

I have recently converted a library, I originally wrote in C++ with Boost Python

  • 0

I have recently converted a library, I originally wrote in C++ with Boost Python wrapping, to C with SWIG wrapping to support more languages.
I switched from C++ to C because the library consists only of a set of functions and I also want the library to be callable from C (without having to compile the whole program with a C++ compiler).
However there is one thing that was not easy to port, a very small subset of the functions needs the ability to report errors back.
In C++/Boost Python that was very elegantly accomplished with throw and exception translation.

What would be the most elegant way (on both the C and wrapped language side) to have a subset of functions report errors?

  • 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-22T18:09:52+00:00Added an answer on May 22, 2026 at 6:09 pm

    This is how I do it. the %{…%} block inserts it’s contents to the wrapper file. The %exception block is SWIG’s exception handling, and runs after each function call to check if there was an error and throw an exception with PyErr_SetString if there was. Then, simply call “set_err(“Error msg”); from your C function if you want to throw an exception.

    %{
        /* Exception helpers */
        static int swig_c_error_num = 0;
        static char swig_c_err_msg[256];
    
        const char *err_occurred()
        {
            if (swig_c_error_num) {
                swig_c_error_num = 0;
                return (const char*)swig_c_err_msg;
            }
            return NULL;
        }
    
        void set_err(const char *msg)
        {
            swig_c_error_num = 1;
            strncpy(swig_c_err_msg, msg, 256);
        }
    %}
    
    %exception {
        const char *err;
        $action
        if (err = err_occurred()) {
            PyErr_SetString(PyExc_RuntimeError, err);
            return NULL;
        }
    }
    

    Alternatively if your C library uses a standard set of return codes you can replace this mechanism with a check of your function’s return code in the %exception block.

    %exception {
        int rc;
        rc = $action;
    
        if (rc == ERR) {
            PyErr_SetString(PyExc_RuntimeError, <err msg>);
            return NULL;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a report, which was recently converted from SSRS2005 to SSRS2208. The report
I have bean that i recently converted over from being a managed-bean to being
I have recently converted 10 JavaScript files into one file, which I then run
I have recently started having problems with TortoiseCVS, or more specifically with plink, the
I have recently begun to use the EF v4 Code Only library for some
We have recently converted an Access Application to store the date on SQL server.
I have a hybrid asp.net web forms / mvc application that I recently converted
Ok, so we recently converted from SVN to Mercurial. We are using TortoiseHG normally.
I have recently converted my IPad application into a Universal Application. I am re-using
I recently converted from MEF to Unity - for various reasons. I previously had

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.