使用ctypes数组作为numpy数组时的PEP 3118警告 [英] PEP 3118 warning when using ctypes array as numpy array

查看:218
本文介绍了使用ctypes数组作为numpy数组时的PEP 3118警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将ctypes数组用作numpy数组时,收到以下警告消息:

I'm getting the following warning message when I try to use a ctypes array as a numpy array:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes, numpy
>>> TenByteBuffer = ctypes.c_ubyte * 10
>>> a = TenByteBuffer()
>>> b = numpy.ctypeslib.as_array(a)
C:\Python27\lib\site-packages\numpy\ctypeslib.py:402: RuntimeWarning: Item size
computed from the PEP 3118 buffer format string does not match the actual item s
ize.
  return array(obj, copy=False)
>>> b
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=uint8)

该代码似乎正常运行。

The code seems to be working, though. Is it a bad idea to ignore this warning?

背景信息:
我正在调用一个实时生成数据的C DLL。我需要传递DLL一系列的缓冲区来保存数据。在等待下一个缓冲区填满的同时,我想用numpy处理最新的缓冲区并保存结果。我正在使用上面的代码生成缓冲区,并且看起来似乎正常,但是我不想在地毯下扫除一个重要问题。

Background: I'm calling a C DLL that produces data in real time. I need to pass the DLL a series of buffers to hold the data. While waiting for the next buffer to fill, I'd like to process the most recent buffer with numpy and save the result. I'm generating the buffers with code like above, and things seem to be working, but I don't want to sweep an important problem under the rug.

推荐答案

这是Python中的错误。 ctypes当前产生无效的PEP 3118类型代码,Numpy注意到:
http://bugs.python.org/ issue10746
http://bugs.python.org/issue10744

It's a bug in Python. ctypes currently produces invalid PEP 3118 type codes, which Numpy notices: http://bugs.python.org/issue10746 http://bugs.python.org/issue10744

当出现这种不一致时,Numpy会跳过使用PEP 3118缓冲区接口,并退回到旧的(过时的)缓冲区接口。

When such an inconsistency is present, Numpy skips using the PEP 3118 buffer interface, and falls back to the old (obsolete) buffer interface. This should work properly.

您可以使用Python的警告模块使警告静音。但是,该警告可能会影响性能。

You can silence the warning using Python's warnings module. However, the warning may have a performance impact.

您还可以尝试通过将ctypes对象包装在 buffer()中来解决此问题。 code>。

You can also try working around the issue by wrapping the ctypes object in buffer().

这篇关于使用ctypes数组作为numpy数组时的PEP 3118警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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