我正在寻找Python C-API PyObject_CheckBuffer的Python等价物.
即我想检查一个对象是否支持缓冲协议,但是来自Python.
use*_*ica 13
我认为你应该使用标准的try-it-and-see-it-it-works技术:
# New-style buffer API, for Python 2.7 and 3.x.
# PyObject_CheckBuffer uses the new-style API.
# 2.6 also has the new-style API, but no memoryview,
# so you can't use it or check compatibility from Python code.
try:
memoryview(thing)
except TypeError:
# Doesn't support it!
# Old-style API. Doesn't exist in 3.x.
# Not quite equivalent to PyObject_CheckBuffer.
try:
buffer(thing)
except TypeError:
# Doesn't support it!
Run Code Online (Sandbox Code Playgroud)