【发布时间】:2016-05-08 14:54:56
【问题描述】:
有一个 Python 函数,它在 CPython 2.5.3 中运行,但在 Jython 2.5.3 中崩溃。 它是 Apache Pig 中用户定义函数的一部分,它使用 Jython 2.5.3,所以我无法更改它。
输入是一个单字节数组,但实际上是无符号字节,所以我需要转换它。
from StringIO import StringIO
import array
import ctypes
assert isinstance(input, array.array), 'unexpected input parameter'
assert input.typecode == 'b', 'unexpected input type'
buffer = StringIO()
for byte in input:
s_byte = ctypes.c_byte(byte)
s_byte_p = ctypes.pointer(s_byte)
u_byte = ctypes.cast(s_byte_p, ctypes.POINTER(ctypes.c_ubyte)).contents.value
buffer.write(chr(u_byte))
buffer.seek(0)
output = buffer.getvalue()
assert isinstance(output, str)
错误是:
s_byte = ctypes.cast(u_byte_p, ctypes.POINTER(ctypes.c_byte)).contents.value
AttributeError: 'module' object has no attribute 'cast'
我猜 ctypes.cast 函数没有在 Jython 2.5.3 中实现。有解决该问题的方法吗?
谢谢, 史蒂芬
【问题讨论】:
-
可以使用
struct.unpack和struct.pack重新实现该代码,请参阅struct module
标签: casting apache-pig jython ctypes