7070 ("PyArg_ValidateKeywordArguments" , "O" , "i" ),
7171 ("PyBool_Check" , "O" , "i" ),
7272 ("PyBool_FromLong" , "l" , "N" ),
73+ ("PyBuffer_IsContiguous" , "y*c" , "i" ),
74+ ("PyBuffer_Release" , "y*" , "" ),
7375 ("PyByteArray_AS_STRING" , "O" , "y" ),
7476 ("PyByteArray_AsString" , "O" , "y" ),
7577 ("PyByteArray_Check" , "O" , "i" ),
869871 "O" : "object" ,
870872 "u" : "str" ,
871873 "y" : "bytes" ,
874+ "y*" : "bytes" ,
872875}
873876
874877RETURN_TYPES_PYI = {
902905 "O" : "PyObject*" ,
903906 "u" : "wchar_t*" ,
904907 "y" : "char*" ,
908+ "y*" : "Py_buffer*" ,
905909}
906910
907911RETURN_TYPES_C = {
938942 "y" : "return PyBytes_FromString(result);" ,
939943}
940944
945+ EXTRAS = {"*" }
946+
941947FUNCTIONDEF = """\
942948 static PyObject* capi_{}(PyObject* Py_UNUSED(self), PyObject* {}) {{
943949{}
11181124
11191125def build_stub (api : str , arg_types : str , return_type : str ) -> str :
11201126
1127+ preprocessed_args : typing .List [str ] = []
1128+ for index , code in enumerate (arg_types ):
1129+ if code in EXTRAS :
1130+ preprocessed_args [- 1 ] = arg_types [index - 1 ] + code
1131+ else :
1132+ preprocessed_args .append (code )
1133+
11211134 args_str = ", " .join (
11221135 "__{}: {}" .format (arg , ARG_TYPES_PYI [annotation ])
1123- for arg , annotation in enumerate (arg_types )
1136+ for arg , annotation in enumerate (preprocessed_args )
11241137 )
11251138 return_str = RETURN_TYPES_PYI [return_type ]
11261139
@@ -1136,11 +1149,18 @@ def build_definition(api: str, arg_types: str, return_type: str) -> str:
11361149
11371150 body : typing .List [str ] = []
11381151
1152+ preprocessed_args : typing .List [str ] = []
1153+ for index , code in enumerate (arg_types ):
1154+ if code in EXTRAS :
1155+ preprocessed_args [- 1 ] = arg_types [index - 1 ] + code
1156+ else :
1157+ preprocessed_args .append (code )
1158+
11391159 if arg_types and arg_types != "O" :
11401160 body .append ("" )
11411161 body .extend (
11421162 "{} arg{};" .format (ARG_TYPES_C [annotation ], arg )
1143- for arg , annotation in enumerate (arg_types )
1163+ for arg , annotation in enumerate (preprocessed_args )
11441164 )
11451165 if return_type :
11461166 body .append ("" )
@@ -1154,15 +1174,19 @@ def build_definition(api: str, arg_types: str, return_type: str) -> str:
11541174 api ,
11551175 len (arg_types ),
11561176 len (arg_types ),
1157- "" .join (", &arg{}" .format (arg ) for arg in range (len (arg_types ))),
1177+ "" .join (
1178+ ", &arg{}" .format (arg ) for arg in range (len (preprocessed_args ))
1179+ ),
11581180 )
11591181 )
11601182 else :
11611183 body .append (
11621184 'if (!PyArg_ParseTuple(args, "{}:{}"{})) {{' .format (
11631185 arg_types ,
11641186 api ,
1165- "" .join (", &arg{}" .format (arg ) for arg in range (len (arg_types ))),
1187+ "" .join (
1188+ ", &arg{}" .format (arg ) for arg in range (len (preprocessed_args ))
1189+ ),
11661190 )
11671191 )
11681192 body .append (INDENT + "return NULL;" )
@@ -1173,7 +1197,7 @@ def build_definition(api: str, arg_types: str, return_type: str) -> str:
11731197 body .append (
11741198 "result = {}({});" .format (
11751199 api ,
1176- ", " .join ("arg{}" .format (arg ) for arg in range (len (arg_types )))
1200+ ", " .join ("arg{}" .format (arg ) for arg in range (len (preprocessed_args )))
11771201 if arg_types != "O"
11781202 else "arg" ,
11791203 )
@@ -1183,7 +1207,7 @@ def build_definition(api: str, arg_types: str, return_type: str) -> str:
11831207 body .append (
11841208 "{}({});" .format (
11851209 api ,
1186- ", " .join ("arg{}" .format (arg ) for arg in range (len (arg_types )))
1210+ ", " .join ("arg{}" .format (arg ) for arg in range (len (preprocessed_args )))
11871211 if arg_types != "O"
11881212 else "arg" ,
11891213 )
0 commit comments