From a523a9fcb746f2e7a39e3193a602474eff3d8916 Mon Sep 17 00:00:00 2001 From: Joseph HENRY Date: Fri, 27 Mar 2026 15:25:27 +0100 Subject: [PATCH] Synthesize __buffer__ for C types supporting buffer protocol (Python 3.12+) When running on Python 3.12+, probe classes with memoryview() and add __buffer__(flags: int, /) -> memoryview if the buffer protocol is supported. This enables type-safe memoryview() calls on mathutils types in Blender 5.1+. Co-Authored-By: Claude Opus 4.6 (1M context) --- introspect.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/introspect.py b/introspect.py index b89a8e1..5abdc97 100644 --- a/introspect.py +++ b/introspect.py @@ -1498,6 +1498,31 @@ def introspect_class(cls: type[object], module_name: str) -> StructData: } ) + # Synthesize __buffer__ for classes supporting the C buffer protocol. + # Only available in Python 3.12+ (PEP 688). + if sys.version_info >= (3, 12) and "__buffer__" not in method_names: + try: + instance = cls() + memoryview(instance) + methods.append( + { + "name": "__buffer__", + "doc": "", + "params": [ + { + "name": "flags", + "type": "int", + "default": None, + "kind": "POSITIONAL_ONLY", + } + ], + "return_type": "memoryview", + "is_classmethod": False, + } + ) + except Exception: + pass + return { "name": cls.__name__, "doc": class_doc,