【发布时间】:2018-05-05 00:56:36
【问题描述】:
考虑一下这个小小的 swig mcve:
example.h
void init(int width, int height);
void dump(const unsigned char *buffer,int pitch);
example.c
#include <stdio.h>
void init(int width, int height) {
printf("Initializing width=%d height=%d", width, height);
}
void dump(const unsigned char *buffer,int pitch) {
for(int i=0;i<pitch;i++) {
printf("%d\n", buffer[i]);
}
}
example.i
%module example
%{
#include "example.h"
%}
%include "example.h"
setup.py
from distutils.core import setup, Extension
example_module = Extension('_example',
sources=['example.i', 'example_wrap.c', 'example.c'],
swig_opts = [],
include_dirs = ["."],
)
setup(name='example',
version='0.1',
author="BPL",
description="""Mcve stackoverflow""",
ext_modules=[example_module],
py_modules=["example"]
)
test.py
import struct
import example as swig_thing
count = 256
width = 8
height = 4
swig_thing.init(width, height)
for frame in range(count):
print(f"frame {frame}")
data = []
for y in range(height):
for x in range(width):
data.append(0x00FF0000)
_buffer = struct.pack(f'{len(data)}L', *data)
swig_thing.dump(_buffer, width*4)
如果我运行 python setup.py build_ext --inplace 然后我尝试运行 test.py 我会收到以下错误:
TypeError: in method 'dump', argument 1 of type 'unsigned char const *'
问题,如何避免上述错误?
【问题讨论】: