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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:49:18+00:00 2026-05-28T03:49:18+00:00

I use SWIG and Numpy. I define a C function called inplace() to process

  • 0

I use SWIG and Numpy. I define a C function called inplace() to process data array fast, and I want to make some error checking (if two arrays have the same dimentions).

I use %rename and %inline in the .i file. As I understand, rename should map the function names’, so every time someone uses inplace, safe_inplace is run and the errors are checked.

But it does not work 🙁 . As far I notice, safe_inplace is not executed, python runs directly inplace without touching the safe version of the function.

# .i

%include "inplace.h"
%rename (inplace) safe_inplace;

%inline %{
    void safe_inplace(int* datain, int in_dx, int in_dy, int in_dz,
                      int* dataout, int out_dx, int out_dy)
    {
        if ((in_dx != out_dx) || (in_dy != out_dy)) { 
            PyErr_Format(PyExc_ValueError, /*... messgage*/) 
            return;
        } 

        inplace( /* .. pass the arguments to original function*/ );
    }

header file:

# .h 

void inplace(int* datain, int in_dx, int in_dy, int in_dz, int* dataout, int out_dx, int out_dy);

Python:

#.py
inplace.inplace(a,b)

The original example that I modify can be found here

  • 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-28T03:49:19+00:00Added an answer on May 28, 2026 at 3:49 am

    Your original approach was close, but you probably want to get SWIG not to wrap the original version of the function at all.

    I’ve put together a slightly simpler example illustrating how this could work. Given the header file:

    void foo();
    

    We want to wrap it such that a slightly modified version is called before invoking the real one.

    The simplest way to do this would be to never actually show SWIG the header file at all for wrapping, only for compiling the wrapper, e.g.:

    %module test
    
    %{
    #include "test.h"
    #include <iostream>
    %}
    
    %rename (foo) foo_safe;
    %inline %{
      void foo_safe() {
        std::cout << "Hello world" << std::endl;
        foo(); // Calls the foo() from test.h, as you'd hope
      }
    %}
    

    If you don’t want to drop the %include (e.g. there’s other things you care about in that header file too) you can do something like:

    %module test
    
    %{
    #include "test.h"
    #include <iostream>
    %}
    
    %rename (unsafe_foo) foo;    
    %include "test.h"
    
    %rename (foo) foo_safe;    
    %inline %{
      void foo_safe() {
        std::cout << "Hello world" << std::endl;
        foo();
      }
    %}
    

    To expose the real implementation of foo as unsafe_foo.

    Or you could use %ignore if there’s no reason for Python users to be able to call unsafe_ignore ever:

    %module test
    
    %{
    #include "test.h"
    #include <iostream>
    %}
    
    %rename (foo) foo_safe;    
    %inline %{
      void foo_safe() {
        std::cout << "Hello world" << std::endl;
        foo();
      }
    %}
    
    %ignore foo;    
    %include "test.h"
    

    Finally it looks like your goal is actually just to run some code prior to the call of the real C function. If that’s the case there’s a few ways you can do that too, for example you can add python code before the real function call is made with pythonprepend:

    %feature("pythonprepend") foo() %{
       print "hello world"
       # check args and raise possibly 
    %}
    

    Or lastly you could use the %exception functionality too, something like (untested):

    %exception inplace {
       // Check args and possibly throw
       $action
    }
    

    $action will be substituted with the real call automatically.

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

Sidebar

Related Questions

Use case: I've just entered insert mode, and typed some text. Now I want
I want to use swig for generation of read-only wrappers for a complex object.
I use C++ and swig to do some calculations. To simplify it, lets assume
I'm trying to use SWIG to create a Octave function. But even the most
I'm trying to figure out how to use SWIG to wrap a c++ function
I've been trying to use SWIG to wrap around a simple library that uses
I'm using SWIG to wrap C++ objects for use in lua, and Im trying
I would like to convert the numpy double array to numpy float array in
I am trying to pass data around the numpy and boost::ublas layers. I have
Before simply answering use SWIG or just write it to a file on disk

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.