有人能告诉我一个非常简单的Python ctypes示例,涉及Unicode字符串包括C代码吗?
说,一种获取Python Unicode字符串并将其传递给C函数的方法,该函数将其与自身连接并将其返回到Python,然后打印出来。
答案 0 :(得分:6)
此程序使用ctypes
从Python调用wcsncat
。它将a
和b
连接到一个缓冲区,该缓冲区不够长,a + b + (null terminator)
可以演示更安全n
版本的连接。
您必须传递create_unicode_buffer()
而不是为非const u"unicode string"
参数传递常规不可变wchar_t*
,否则您可能会遇到分段错误。
如果您需要与之交谈的功能返回UCS-2和sizeof(wchar_t) == 4
,那么您将无法使用unicode_buffer()
,因为它会在wchar_t
之间转换为Python的内部Unicode表示形式。在这种情况下,您可以使用result.create_string_buffer()
和result.decode('UCS2')
的某种组合,或者只创建一个c_short
和u''.join(unichr(c) for c in buffer)
的数组。我必须这样做来调试ODBC驱动程序。
example.py:
#!/usr/bin/env python
#-*- encoding: utf-8 -*-
import sys
from ctypes import *
example = cdll.LoadLibrary(".libs/libexample.so")
example.its_an_example.restype = c_wchar_p
example.its_an_example.argtypes = (c_wchar_p, c_wchar_p, c_uint)
buf = create_unicode_buffer(19) # writable, unlike u"example".
buf[0] = u"\u0000"
a = u"あがぃいぅ ☃ "
b = u"個人 相命理 網上聯盟"
print example.its_an_example(buf, a, len(buf) - len(buf.value) - 1)
print example.its_an_example(buf, b, len(buf) - len(buf.value) - 1)
print buf.value # you may have to .encode("utf-8") before printing
sys.stdout.write(buf.value.encode("utf-8") + "\n")
example.c:
#include <stdlib.h>
#include <wchar.h>
wchar_t *its_an_example(wchar_t *dest, const wchar_t *src, size_t n) {
return wcsncat(dest, src, n);
}
Makefile :(确保缩进是一个制表符,而不是空格):
all:
libtool --mode=compile gcc -g -O -c example.c
libtool --mode=link gcc -g -O -o libexample.la example.lo \
-rpath /usr/local/lib
答案 1 :(得分:0)
未经测试,但我认为这应该有用。
s = "inputstring"
mydll.my_c_fcn.restype = c_char_p
result = mydll.my_c_fcn(s)
print result
至于内存管理,我的理解是你的c代码需要管理它创建的内存。也就是说,它不应该释放输入字符串,但最终需要释放返回字符串。
答案 2 :(得分:0)
from ctypes import *
buffer = create_string_buffer(128)
cdll.msvcrt.strcat(buffer, "blah")
print buffer.value
注意:我理解Python代码很简单,但我正在努力的是C代码。是否需要释放其输入字符串?它的输出字符串是否会被Python代替?
不,您需要自己手动释放缓冲区。人们通常做的是立即从buffer.value复制python字符串,然后释放缓冲区。
你可以发布C代码吗? - 迈克2小时前
#include <string.h>
char* mystrcat(char* buffer) {
strcat(buffer, "blah");
return buffer;
}