使用stdint和swig和numpy.i [英] using stdint with swig and numpy.i

查看:217
本文介绍了使用stdint和swig和numpy.i的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于 swig 的Python代码中使用 c inline 的模块。
为此,我想让 numpy 数组在 C 中访问。直到现在我使用C类型 unsigned short ,但我想使用类型 uint16_t



不幸的是, c ++ -functions在使用 stdint.h 类型时不会正确包装。给出的错误是: _setc()只需要2个参数(1给定)。这意味着,函数不被包装以接受 numpy 数组。该错误不会发生,当我使用例如。 无符号short



你有什么想法,我如何可以使swig映射 numpy



interface.i 不工作:

  / * interface.i * / 
extern int __g ;
%}
%includestdint.i
%includenumpy.i

%init%{
import_array
%}
%apply(uint16_t * INPLACE_ARRAY3,int DIM1){(uint16_t * seq,int n1)};
extern int __g();

c ++ function NOT working:

  #includePython.h
#include< stdio.h>
#include< stdint.h>
extern uint16_t * c;
extern int Dc;
extern int Nc [4];
void _setc(uint16_t * seq,int n1,int n2,int n3)
{
c = seq;
Nc [0] = n1;
Nc [1] = n2;
Nc [2] = n3,
}

interface.i 工作:

  / * interface.i * / 
extern int __g
%}
%includestdint.i
%includenumpy.i

%init%{
import_array
%}
%apply(unsigned short * INPLACE_ARRAY3,int DIM1){(unsigned short * seq,int n1)};
extern int __g();

c ++ 函数工作:

  #includePython.h
#include< stdio.h>
#include< stdint.h>
extern unsigned short * c;
extern int Dc;
extern int Nc [4];
void _setc(unsigned short * seq,int n1,int n2,int n3)
{
c = seq;
Nc [0] = n1;
Nc [1] = n2;
Nc [2] = n3;
}


解决方案



我编辑了 numpy.i 适合我的原因:
我用 stdint.h 在行3044中替换旧的 C 类型ff:

  [..] 
/ *%numpy_typemaps()宏的具体实例:每次调用
*以下将所有类型的映射应用到指定的数据类型。
* /
%numpy_typemaps(int8_t,NPY_BYTE,int)
%numpy_typemaps(uint8_t,NPY_UBYTE,int)
%numpy_typemaps(int16_t,NPY_SHORT,int)
% numpy_typemaps(uint16_t,NPY_USHORT,int)
%numpy_typemaps(int32_t,NPY_INT,int)
%numpy_typemaps(uint32_t,NPY_UINT,int)
%numpy_typemaps(long,NPY_LONG,int)
%numpy_typemaps(unsigned long,NPY_ULONG,int)
%numpy_typemaps(int64_t,NPY_LONGLONG,int)
%numpy_typemaps(uint64_t,NPY_ULONGLONG,int)
%numpy_typemaps(float,NPY_FLOAT,int)
%numpy_typemaps(double,NPY_DOUBLE,int)
[..]

我不知道是否有任何人比编辑 numpy.i



Cheers
Jochen p>

I'm developing a module for using c inline in Python code based on swig. For that I would like to make numpy arrays accessible in C. Until now I used C types like unsigned short but I would like to use types like uint16_t from stdint.h to be save whatever compiler my module encounters.

Unfortunately the c++-functions do not get wrapped correctly when using stdint.htypes. The Error given is: _setc() takes exactly 2 arguments (1 given). That means, the function is not wrapped to accept numpy arrays. The error does not occur, when I use e.g. unsigned short.

Do you have any ideas, how I can make swig map numpy arrays into stdint-types?

interface.i NOT working:

/* interface.i */
extern int __g();
%}
%include "stdint.i"
%include "numpy.i"

%init %{
import_array();
%}
%apply (uint16_t* INPLACE_ARRAY3, int DIM1) {(uint16_t* seq, int n1)};
extern int __g();

c++ function NOT working:

#include "Python.h"
#include <stdio.h>
#include <stdint.h>
extern uint16_t* c;
extern int Dc;
extern int Nc[4];
void _setc(uint16_t *seq, int n1, int n2, int n3)
{
    c = seq;
    Nc[0] = n1;
    Nc[1] = n2;
    Nc[2] = n3;
}

interface.i working:

/* interface.i */
extern int __g();
%}
%include "stdint.i"
%include "numpy.i"

%init %{
import_array();
%}
%apply (unsigned short* INPLACE_ARRAY3, int DIM1) {(unsigned short* seq, int n1)};
extern int __g();

c++ function working:

#include "Python.h"
#include <stdio.h>
#include <stdint.h>
extern unsigned short* c;
extern int Dc;
extern int Nc[4];
void _setc(unsigned short *seq, int n1, int n2, int n3)
{
    c = seq;
    Nc[0] = n1;
    Nc[1] = n2;
    Nc[2] = n3;
}

解决方案

Haha, I found some "solution" just a few minutes after I gave up and posted this question.

I edited the numpy.ito fit my cause: I substituted the old C types with stdint.h types in lines 3044 ff:

[..]
/* Concrete instances of the %numpy_typemaps() macro: Each invocation
 * below applies all of the typemaps above to the specified data type.
 */
%numpy_typemaps(int8_t       , NPY_BYTE     , int)
%numpy_typemaps(uint8_t     , NPY_UBYTE    , int)
%numpy_typemaps(int16_t             , NPY_SHORT    , int)
%numpy_typemaps(uint16_t    , NPY_USHORT   , int)
%numpy_typemaps(int32_t               , NPY_INT      , int)
%numpy_typemaps(uint32_t      , NPY_UINT     , int)
%numpy_typemaps(long              , NPY_LONG     , int)
%numpy_typemaps(unsigned long     , NPY_ULONG    , int)
%numpy_typemaps(int64_t         , NPY_LONGLONG , int)
%numpy_typemaps(uint64_t, NPY_ULONGLONG, int)
%numpy_typemaps(float             , NPY_FLOAT    , int)
%numpy_typemaps(double            , NPY_DOUBLE   , int)
[..]

I wonder if anyone has a better idea than editing the numpy.i

Cheers Jochen

这篇关于使用stdint和swig和numpy.i的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆