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,