Python 2和3之间的类型差异 [英] Differences in ctypes between Python 2 and 3

查看:245
本文介绍了Python 2和3之间的类型差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作的python 2.7程序调用一个DLL。我试图将脚本移植到python 3.2。 DLL调用似乎工作(即调用时没有错误),但返回的数据是没有意义的。



以防万一有用:
- 调用有三个参数:两个int(输入)和一个指向ushort数组(输出)的指针。



我没有成功尝试使用python和numpy数组



任何人都可以枚举Python 2.7和3.2之间的区别,尊重ctypes?



提前感谢



编辑



这是一些示例代码。该DLL是proparyary,所以我没有代码。但是我确实有C头:

  void example(int width,int height,unsigned short * pointer)

python代码是:

  width,height = 40,100 
imagearray = np.zeros((width,height),dtype = np.dtype(np.ushort))
image = np.ascontiguousarray(imagearray)
ptrimage = image.ctypes.data_as(ct.POINTER(ct.c_ushort))
DLL.example(width,height,ptrimage)

这个工作在python 2.7但不是3.2中。



编辑2 p>

如果ctypes中的更改只是Cedric指出的,python 3.2将无法正常工作。所以再看看代码,我发现有一个在函数之前调用的一个准备函数。签名是:

  void prepare(char * table)

在python中,我正在调用:

  table = str aNumber)
DLL.prepare(table)

可能的问题是由于Python字符串处理的变化?

解决方案

在Python 2.7中,字符串默认为字节串。在Python 3.x中,默认情况下它们是unicode。尝试使用 .encode('ascii')在将其转换为 DLL.prepare 之前,将字符串显式设置为字符串。



编辑:

  #another方式说table = str(aNumber).encode('ascii')
table = bytes(str(aNumber),'ascii')
DLL.prepare(table)


I have a working python 2.7 program that calls a DLL. I am trying to port the script to python 3.2. The DLL call seems to work (i.e. there is no error upon calling) but the returned data does not make sense.

Just in case it could be useful: - The call takes three arguments: two int (input) and a pointer to a ushort array (output).

I have tried using both python and numpy arrays without success.

Can anyone enumerate the differences between Python 2.7 and 3.2 respecting ctypes?

Thanks in advance

EDIT

Here is some example code. The DLL is propietary so I do not have the code. But I do have the C header:

void example (int width, int height, unsigned short* pointer)

The python code is:

width, height = 40, 100
imagearray = np.zeros((width,height), dtype=np.dtype(np.ushort))
image = np.ascontiguousarray(imagearray)
ptrimage = image.ctypes.data_as(ct.POINTER(ct.c_ushort))
DLL.example(width, height, ptrimage)

This works in python 2.7 but not in 3.2.

EDIT 2

If the changes in ctypes are only those pointed out by Cedric, it does not make sense that python 3.2 will not work. So looking again at the code, I have found that there is a preparation function called before the function that I am mentioning. The signature is:

void prepare(char *table)

In python, I am calling by:

table = str(aNumber)
DLL.prepare(table)

Is it possible that the problem is due to the change in the Python string handling?

解决方案

In Python 2.7, strings are byte-strings by default. In Python 3.x, they are unicode by default. Try explicitly making your string a byte string using .encode('ascii') before handing it to DLL.prepare.

Edit:

#another way of saying table=str(aNumber).encode('ascii')
table = bytes(str(aNumber), 'ascii')
DLL.prepare(table)

这篇关于Python 2和3之间的类型差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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