Skip to content

Commit 7f4333d

Browse files
committed
Add PyBuffer APIs.
1 parent 36e9cad commit 7f4333d

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

generate.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
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"),
@@ -869,6 +871,7 @@
869871
"O": "object",
870872
"u": "str",
871873
"y": "bytes",
874+
"y*": "bytes",
872875
}
873876

874877
RETURN_TYPES_PYI = {
@@ -902,6 +905,7 @@
902905
"O": "PyObject*",
903906
"u": "wchar_t*",
904907
"y": "char*",
908+
"y*": "Py_buffer*",
905909
}
906910

907911
RETURN_TYPES_C = {
@@ -938,6 +942,8 @@
938942
"y": "return PyBytes_FromString(result);",
939943
}
940944

945+
EXTRAS = {"*"}
946+
941947
FUNCTIONDEF = """\
942948
static PyObject* capi_{}(PyObject* Py_UNUSED(self), PyObject* {}) {{
943949
{}
@@ -1118,9 +1124,16 @@
11181124

11191125
def 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
)

pycapi.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,45 @@ static PyObject* capi_PyBool_FromLong(PyObject* Py_UNUSED(self), PyObject* args)
871871
return result;
872872
}
873873

874+
/* PyBuffer */
875+
876+
static PyObject* capi_PyBuffer_IsContiguous(PyObject* Py_UNUSED(self), PyObject* args) {
877+
878+
Py_buffer* arg0;
879+
char arg1;
880+
881+
int result;
882+
883+
if (!PyArg_ParseTuple(args, "y*c:PyBuffer_IsContiguous", &arg0, &arg1)) {
884+
return NULL;
885+
}
886+
887+
result = PyBuffer_IsContiguous(arg0, arg1);
888+
889+
if (PyErr_Occurred()) {
890+
return NULL;
891+
}
892+
893+
return PyLong_FromLong(result);
894+
}
895+
896+
static PyObject* capi_PyBuffer_Release(PyObject* Py_UNUSED(self), PyObject* args) {
897+
898+
Py_buffer* arg0;
899+
900+
if (!PyArg_ParseTuple(args, "y*:PyBuffer_Release", &arg0)) {
901+
return NULL;
902+
}
903+
904+
PyBuffer_Release(arg0);
905+
906+
if (PyErr_Occurred()) {
907+
return NULL;
908+
}
909+
910+
Py_RETURN_NONE;
911+
}
912+
874913
/* PyByteArray */
875914

876915
static PyObject* capi_PyByteArray_AS_STRING(PyObject* Py_UNUSED(self), PyObject* arg) {
@@ -11715,6 +11754,11 @@ static PyMethodDef CAPIMethods[] = {
1171511754
{"PyBool_Check", capi_PyBool_Check, METH_O, NULL},
1171611755
{"PyBool_FromLong", capi_PyBool_FromLong, METH_VARARGS, NULL},
1171711756

11757+
/* PyBuffer */
11758+
11759+
{"PyBuffer_IsContiguous", capi_PyBuffer_IsContiguous, METH_VARARGS, NULL},
11760+
{"PyBuffer_Release", capi_PyBuffer_Release, METH_VARARGS, NULL},
11761+
1171811762
/* PyByteArray */
1171911763

1172011764
{"PyByteArray_AS_STRING", capi_PyByteArray_AS_STRING, METH_O, NULL},

pycapi.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ def PyArg_ValidateKeywordArguments(__0: object) -> int: ...
7272
def PyBool_Check(__0: object) -> int: ...
7373
def PyBool_FromLong(__0: int) -> typing.Any: ...
7474

75+
# PyBuffer
76+
77+
def PyBuffer_IsContiguous(__0: bytes, __1: bytes) -> int: ...
78+
def PyBuffer_Release(__0: bytes) -> None: ...
79+
7580
# PyByteArray
7681

7782
def PyByteArray_AS_STRING(__0: object) -> typing.Optional[bytes]: ...

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
long_description_content_type="text/markdown",
1919
name="pycapi",
2020
url="https://github.com/brandtbucher/pycapi",
21-
version="0.74.0",
21+
version="0.75.0",
2222
)

0 commit comments

Comments
 (0)