Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

void* in Constructor not working in multi-argument typemap #614

Closed
boaz001 opened this issue Feb 15, 2016 · 1 comment
Closed

void* in Constructor not working in multi-argument typemap #614

boaz001 opened this issue Feb 15, 2016 · 1 comment

Comments

@boaz001
Copy link

boaz001 commented Feb 15, 2016

When trying to convert a Python string object to a void* and length (for a internal buffer) the typemap I've created is not used in the Constructor and I don't understand why, I think this may even be a bug?

I wish to use it in Python like this:

import example
s = "Hello World!"
example.Example(s)

But this gives:

NotImplementedError: Wrong number or type of arguments for overloaded function 'new_Example'.
  Possible C/C++ prototypes are:
    Example::Example()
    Example::Example(void *const,int const)

Is this expected behaviour? If I change the void* argument to char* the code works as expected. Note that a member-function foo in my example does use the typemap, only the constructor does not seem to like it.

Interface description:

%module example
%{
#include "example.h"
%}

%typemap(in) (void* const data, const int size)
{
  if (!PyString_Check($input))
  {
    PyErr_SetString(PyExc_ValueError, "Expecting a string");
    return NULL;
  }
  $1 = (void*)PyString_AsString($input);
  $2 = PyString_Size($input);
}

%include "example.h"

Header:

class Example
{
public:
  Example() : data_(0), size_(0) {}
  Example(void* const data, const int size)
  {
    data_ = (char*)data;
    size_ = size;
  }
  ~Example() {}

  void foo(void* const data, const int size)
  {}

private:
  char* data_;
  int size_;
};

Compile with:

swig -python -c++ -o example_wrap.cpp example.i
g++ -fPIC -c example.cpp example_wrap.cpp -I/usr/include/python2.6
g++ -shared example.o example_wrap.o -o _example.so
@wsfulton
Copy link
Member

wsfulton commented Apr 2, 2016

You are missing the typecheck typemap, see http://www.swig.org/Doc3.0/Typemaps.html#Typemaps_overloading
Add the following:

%typecheck(SWIG_TYPECHECK_STRING) (void* const data, const int size)
{
  $1 = PyString_Check($input) ? 1 : 0;
}

@wsfulton wsfulton closed this as completed Apr 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants