【问题标题】:extending global c variable array with swig and python用 swig 和 python 扩展全局 c 变量数组
【发布时间】:2012-07-26 10:30:31
【问题描述】:

我在 c 中有一个全局变量数组,我想将它引入 python。我在使用varout typemap 时遇到了困难:

/* example.c */
int foo[] = {0, 1};

这是非常普通的界面:

/* example.i */
%module example
%{     
 extern int foo[2];
%}

%typemap(varout) int foo[] {
  int i;
  //$1, $1_dim0, $1_dim1
  $result = PyList_New($1_dim0);
  for (i = 0; i < $1_dim0; i++) {
    PyObject *o = PyInt_FromLong((double) $1[i]);
    PyList_SetItem($result,i,o);
  }
}

%include "example.c"

当我尝试使用以下 SConstruct 构建它时:

import distutils.sysconfig
env = Environment(SWIGFLAGS='-python -shadow -Wall'.split(),
                  CPPPATH=[distutils.sysconfig.get_python_inc()],
                  SHLIBPREFIX="")
env.SharedLibrary('_example.so', ['example.c', 'example.i'])

$1_dim0特殊变量未填充,导致example_wrap.c中出现以下不可编译代码:

SWIGINTERN PyObject *Swig_var_foo_get(void) {
  PyObject *pyobj = 0;

  {
    int i;
    //foo, , foo_dim1
    pyobj = PyList_New();
    for (i = 0; i < ; i++) {
      PyObject *o = PyInt_FromLong((double) foo[i]);
      PyList_SetItem(pyobj,i,o);
    }
  }
  return pyobj;
}

很明显,类型映射匹配已经发生,但是缺少数组的维数。我错过了什么?对维度进行硬编码确实有效。

一般来说,有没有办法用 swig 扩展全局 cvar 变量?

$ swig -version
SWIG Version 2.0.4
Compiled with g++ [i686-pc-linux-gnu]
Configured options: +pcre
Please see http://www.swig.org for reporting bugs and further information

【问题讨论】:

  • 我展示了如何使您的示例工作,但我有点不清楚您的意思:“一般来说,有没有办法用 swig 扩展全局 cvar 变量?” - 除了修复类型映射之外,还有更多的功能吗?
  • 我想知道是否可以使用__getitem__() 扩展foo 变量类型,使其可以使用[] 编写python 脚本。
  • PyList 不是已经可以下标了吗?

标签: python arrays global-variables swig extend


【解决方案1】:

对于像我这样思考如何处理非简单类型数组的人来说,这是一种方法:

非简单类型:

typedef struct {
    int a;
    float b;
} Foo;

还有一个全局数组:

extern Foo *foov[40];

%typemap(varout) Foo *foov[ANY] {
    int i;
    $result = PyList_New($1_dim0);
    for (i = 0; i < $1_dim0; i++) {
        PyObject *o = SWIG_NewPointerObj($1[i], SWIGTYPE_p_Foo, 0);
        PyList_SetItem($result, i, o);
  }
}

刚刚分享了这个,因为我花了很长时间才发现,这篇文章很有帮助。只需要找出如何分配我的非简单类型的 SWIG 版本——发现埋在这里:

http://www.swig.org/Doc2.0/Python.html#Python_nn64

【讨论】:

    【解决方案2】:

    您的varout 类型映射就快到了。您需要做两处小改动:

    1. 您需要将大小 ANY 添加到 int foo[] 类型映射:

      %typemap(varout) int foo[ANY] {
        int i;
        //$1, $1_dim0, $1_dim1
        $result = PyList_New($1_dim0);
        for (i = 0; i < $1_dim0; i++) {
          PyObject *o = PyInt_FromLong((double) $1[i]);
          PyList_SetItem($result,i,o);
        }
      }
      

      这可确保您的类型映射与(任何)已知大小的数组匹配,而不仅仅是等于 int *foo。

    2. 您需要修改example.c 以使foo 的大小更清晰。就目前而言,它是合法且正确的 C,但推断数组的大小很棘手,除非您碰巧是一个完整的 C 编译器。将其更改为:

      int foo[2] = {0, 1};
      

      足以确保它与 varout 类型映射匹配。

    通过这两项更改,生成的代码可以如您所愿:

    SWIGINTERN PyObject *Swig_var_foo_get(void) {
      PyObject *pyobj = 0;
    
      {
        int i;
        //foo, 2, foo_dim1
        pyobj = PyList_New(2);
        for (i = 0; i < 2; i++) {
          PyObject *o = PyInt_FromLong((double) foo[i]);
          PyList_SetItem(pyobj,i,o);
        }
      }
      return pyobj;
    }
    

    是这些更改在我的机器上生成的。

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多