summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLisandro Dalcin <dalcinl@gmail.com>2023-05-04 12:09:34 +0400
committerGitHub <noreply@github.com>2023-05-04 10:09:34 +0200
commit918f78228b87c2d56f67b4fc634393fc3b8d2586 (patch)
treeda92b2156f54ba238f5506e1265294db29728f0d
parente11ff38357fb928cbc207184e2dd1c60c2344571 (diff)
downloadcython-918f78228b87c2d56f67b4fc634393fc3b8d2586.tar.gz
Support PyBufferProcs with Limited C-API under Py3.9+ or Py3.11+ (GH-5422)
-rw-r--r--Cython/Compiler/Nodes.py6
-rw-r--r--Cython/Compiler/TypeSlots.py7
2 files changed, 8 insertions, 5 deletions
diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py
index 877e7b928..230226298 100644
--- a/Cython/Compiler/Nodes.py
+++ b/Cython/Compiler/Nodes.py
@@ -5566,10 +5566,12 @@ class CClassDefNode(ClassDefNode):
typeptr_cname, buffer_slot.slot_name,
))
code.putln("}")
+ code.putln("#elif defined(Py_bf_getbuffer) && defined(Py_bf_releasebuffer)")
+ code.putln("/* PY_VERSION_HEX >= 0x03090000 || Py_LIMITED_API >= 0x030B0000 */")
code.putln("#elif defined(_MSC_VER)")
- code.putln("#pragma message (\"The buffer protocol is not supported in the Limited C-API.\")")
+ code.putln("#pragma message (\"The buffer protocol is not supported in the Limited C-API < 3.11.\")")
code.putln("#else")
- code.putln("#warning \"The buffer protocol is not supported in the Limited C-API.\"")
+ code.putln("#warning \"The buffer protocol is not supported in the Limited C-API < 3.11.\"")
code.putln("#endif")
code.globalstate.use_utility_code(
diff --git a/Cython/Compiler/TypeSlots.py b/Cython/Compiler/TypeSlots.py
index e2b692481..115e3e754 100644
--- a/Cython/Compiler/TypeSlots.py
+++ b/Cython/Compiler/TypeSlots.py
@@ -268,6 +268,10 @@ class SlotDescriptor(object):
if value == "0":
return
preprocessor_guard = self.preprocessor_guard_code()
+ if not preprocessor_guard:
+ if self.py3 and self.slot_name.startswith('bf_'):
+ # The buffer protocol requires Limited API 3.11, so check if the spec slots are available.
+ preprocessor_guard = "#if defined(Py_%s)" % self.slot_name
if preprocessor_guard:
code.putln(preprocessor_guard)
code.putln("{Py_%s, (void *)%s}," % (self.slot_name, value))
@@ -628,9 +632,6 @@ class SuiteSlot(SlotDescriptor):
code.putln("#endif")
def generate_spec(self, scope, code):
- if self.slot_name == "tp_as_buffer":
- # Cannot currently support the buffer protocol in the limited C-API.
- return
for slot in self.sub_slots:
slot.generate_spec(scope, code)