【问题标题】:A SWIG multi-argument typemap apply to functions, but not to the constructor if there's multiple constructorsSWIG 多参数类型映射适用于函数,但如果有多个构造函数,则不适用于构造函数
【发布时间】:2018-12-21 16:14:18
【问题描述】:

我正在尝试在 C++ 中制作四分之一浮点类型,并使用 SWIG 将其包装到 Python 中。这是一个正在进行的工作。问题是 %typemap(in) (int dim_count, int* shape) 不适用于构造函数,如果有多个构造函数。

如果我只使用一个构造函数,它看起来像这样:

Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import logchar
>>> logchar.floatTensor(1.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "logchar.py", line 263, in __init__
    this = _logchar.new_floatTensor(dim_count)
ValueError: Expecting a list
>>> logchar.floatTensor([1])
<logchar.floatTensor; proxy of <Swig Object of type 'logchar::Tensor< float > *' at 0x7f1fa68da390> >
>>>

但是对于多个构造函数,我会遇到这样的混乱:

Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from logchar import *
>>> a=quarterTensor([1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "logchar.py", line 230, in __init__
    this = _logchar.new_quarterTensor(*args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'new_quarterTensor'.
  Possible C/C++ prototypes are:
    logchar::Tensor< logchar::quarter >::Tensor(logchar::quarter)
    logchar::Tensor< logchar::quarter >::Tensor(int,int *)
    logchar::Tensor< logchar::quarter >::Tensor(logchar::Tensor< logchar::quarter > const &)

>>> num=quarter(1.0)
>>> num
1.000000
>>> a=quarterTensor(num)
>>> a._reshape([1,1,1])
>>>

在 C++ 中:

namespace logchar {
    template <typename T> class Tensor {
        public:
            //Tensor(T scalar);
                                //causes trouble
            Tensor(const int dim_count, int* shape);
            //Tensor(const Tensor<T>& from);
                                //causes trouble
            ~Tensor();
            Tensor<T> reshape(const int dim_count, int* shape) const;
            int get_dim_count() const;
            void get_shape(const int dim_count, int* shape) const;
            std::shared_ptr<T> operator*() const;
            void _reshape(const int dim_count, int* shape);
        private:
            int dim_count;
            int* shape;
            int size_flattened;
            std::shared_ptr<T> buffer;
    } ;

在 SWIG 中:

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

%typemap(in) (const int dim_count, int *shape) {
  if (!PyList_Check($input)) {
    PyErr_SetString(PyExc_ValueError, "Expecting a list");
    SWIG_fail;
  }
  $1 = PyList_Size($input);
  $2 = new int[$1];
  for (int i = 0; i < $1; i++) {
    PyObject *s = PyList_GetItem($input,i);
    if (!PyInt_Check(s)) {
      delete[] $2;
      PyErr_SetString(PyExc_ValueError, "List items must be integers");
      SWIG_fail;
    }
    $2[i] = PyInt_AsLong(s);
  }
 }

%typemap(freearg) int *shape {
  if ($1) {
    delete[] $1;
  }
 }

%include "logchar.h"

%template(quarterTensor) logchar::Tensor<logchar::quarter>;
%template(floatTensor) logchar::Tensor<float>;
%template(doubleTensor) logchar::Tensor<double>;

%include exception.i

%exception {
    try {
        $function
    } catch(const char* e) {
        SWIG_exception(SWIG_RuntimeError, e);
    }
}

%include "carrays.i"
%array_class(int, intArray);

我找到的唯一相关链接。没想到答案。 http://swig.10945.n7.nabble.com/multi-argument-typemap-and-default-parameter-in-python-td6721.html

【问题讨论】:

  • 您需要将您的类型映射放在您的%include "logchar.h" 之前,否则它不会被应用。这应该可以帮助您入门。
  • 嗯,我做到了,然后构建它。它仍然不起作用。我已经发布了新代码。
  • 但是我遇到了 numpy.i 的问题。我认为自己写会更容易。
  • 首先,把它放在中间没有帮助。其次,您的第一个建议很有帮助,并且生成了代码 ,因为编译器抛出了有关 PyInt_Int 的错误,该错误必须用 PyInt_AsLong 替换。我检查了文件大小。第一个和第二个之间没有区别,但在第一个和最后一个之间。
  • 实际原因似乎是 SWIG 没有将类型映射应用于构造函数。我得检查一下。编辑:这是真的。

标签: c++ python-2.7 swig


【解决方案1】:

前面的两个答案都没有解决我的问题。但是解决方案在我所说的唯一相关的链接中可用。解决方案是使用类型检查。

%typecheck(SWIG_TYPECHECK_INT32_ARRAY) (const int dim_count, int *shape) {
  $1 = PyList_Check($input) ? 1 : 0;
}

https://github.com/swig/swig/issues/614 - 这是类似的问题。

来自 SWIG documentation

为了支持动态调度,SWIG 首先定义了一个通用类型 层次结构如下:

Symbolic Name                   Precedence Value
------------------------------  ------------------
SWIG_TYPECHECK_POINTER           0  
SWIG_TYPECHECK_VOIDPTR           10 
SWIG_TYPECHECK_BOOL              15 
SWIG_TYPECHECK_UINT8             20 
SWIG_TYPECHECK_INT8              25 
SWIG_TYPECHECK_UINT16            30 
SWIG_TYPECHECK_INT16             35 
SWIG_TYPECHECK_UINT32            40 
SWIG_TYPECHECK_INT32             45 
SWIG_TYPECHECK_UINT64            50 
SWIG_TYPECHECK_INT64             55 
SWIG_TYPECHECK_UINT128           60 
SWIG_TYPECHECK_INT128            65 
SWIG_TYPECHECK_INTEGER           70 
SWIG_TYPECHECK_FLOAT             80 
SWIG_TYPECHECK_DOUBLE            90 
SWIG_TYPECHECK_COMPLEX           100 
SWIG_TYPECHECK_UNICHAR           110 
SWIG_TYPECHECK_UNISTRING         120 
SWIG_TYPECHECK_CHAR              130 
SWIG_TYPECHECK_STRING            140 
SWIG_TYPECHECK_BOOL_ARRAY        1015 
SWIG_TYPECHECK_INT8_ARRAY        1025 
SWIG_TYPECHECK_INT16_ARRAY       1035 
SWIG_TYPECHECK_INT32_ARRAY       1045 
SWIG_TYPECHECK_INT64_ARRAY       1055 
SWIG_TYPECHECK_INT128_ARRAY      1065 
SWIG_TYPECHECK_FLOAT_ARRAY       1080 
SWIG_TYPECHECK_DOUBLE_ARRAY      1090 
SWIG_TYPECHECK_CHAR_ARRAY        1130 
SWIG_TYPECHECK_STRING_ARRAY      1140

【讨论】:

    【解决方案2】:

    解决办法

    test.i:

    %module example
    %{
      #include "test.h"
    %}
    
    %typemap(in) (const int dim_count, int *shape) {
      int i;
      if (!PyList_Check($input)) {
        PyErr_SetString(PyExc_ValueError, "Expecting a list");
        return NULL;
      }
      $1 = PyList_Size($input);
      $2 = (int *) malloc(($1)*sizeof(int));
      for (i = 0; i < $1; i++) {
        PyObject *s = PyList_GetItem($input,i);
        if (!PyInt_Check(s)) {
          free($2);
          PyErr_SetString(PyExc_ValueError, "List items must be integers");
          return NULL;
        }
        $2[i] = PyInt_AsLong(s);
      }
     }
    
    %typemap(freearg) (const int dim_count, int *shape) {
      if ($2) {
        free($2);
      }
     }
    
    %include "test.h"
    

    测试.h

    #pragma once
    
    class B {
     public:
      B(const int dim_count, int *shape);
    };
    

    test.cpp:

    #include <cstdio>
    #include "test.h"
    
    B::B(const int dim_count, int *shape) {
      printf("First element: %d\n", shape[0]);
    }
    

    setup.py:

    from distutils.core import setup, Extension
    
    setup(name="example",
          py_modules=['example'],
          ext_modules=[Extension("_example",
                         ["test.i","test.cpp"],
                         swig_opts=['-c++'],
        extra_compile_args=['--std=c++11']
                      )]
    )
    

    构建使用:

    python setup.py build_ext --inplace
    

    【讨论】:

    • 不客气。类型映射适用于所有函数,包括构造函数
    • 模板也可以使用(我一直在使用它们)。但是嵌套模板存在一些问题。我的建议是避免嵌套模板
    • 内存有问题。
    • 尝试用 new/delete[] 替换 malloc/free,我注意到你的 freearg 不起作用。 free(): 无效指针 - 这就是我得到的。使用了默认的 freearg。
    • 在这里工作正常,例如在创建这样的对象时。 b = example.B([1,2,3])。如果替换malloc/free,请记得正确使用new[]delete []。下一次,提供一个完整的例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多