summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorJoannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com>2019-08-20 14:46:36 (GMT)
committerVictor Stinner <vstinner@redhat.com>2019-08-20 14:46:36 (GMT)
commit9e66aba99925eebacfe137d9deb0ef1fdbc2d5db (patch)
treee9353ac99e9911959aadb9092ae35237ec06b5e2 /Objects
parent18f8dcfa10d8a858b152d12a9ad8fa83b7e967f0 (diff)
downloadcpython-9e66aba99925eebacfe137d9deb0ef1fdbc2d5db.zip
cpython-9e66aba99925eebacfe137d9deb0ef1fdbc2d5db.tar.gz
cpython-9e66aba99925eebacfe137d9deb0ef1fdbc2d5db.tar.bz2
bpo-15913: Implement PyBuffer_SizeFromFormat() (GH-13873)
Implement PyBuffer_SizeFromFormat() function (previously documented but not implemented): call struct.calcsize().
Diffstat (limited to 'Objects')
-rwxr-xr-x[-rw-r--r--]Objects/abstract.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index f93d73f..3db56fa 100644..100755
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -495,6 +495,48 @@ _Py_add_one_to_index_C(int nd, Py_ssize_t *index, const Py_ssize_t *shape)
}
}
+Py_ssize_t
+PyBuffer_SizeFromFormat(const char *format)
+{
+ PyObject *structmodule = NULL;
+ PyObject *calcsize = NULL;
+ PyObject *res = NULL;
+ PyObject *fmt = NULL;
+ Py_ssize_t itemsize = -1;
+
+ structmodule = PyImport_ImportModule("struct");
+ if (structmodule == NULL) {
+ return itemsize;
+ }
+
+ calcsize = PyObject_GetAttrString(structmodule, "calcsize");
+ if (calcsize == NULL) {
+ goto done;
+ }
+
+ fmt = PyUnicode_FromString(format);
+ if (fmt == NULL) {
+ goto done;
+ }
+
+ res = PyObject_CallFunctionObjArgs(calcsize, fmt, NULL);
+ if (res == NULL) {
+ goto done;
+ }
+
+ itemsize = PyLong_AsSsize_t(res);
+ if (itemsize < 0) {
+ goto done;
+ }
+
+done:
+ Py_DECREF(structmodule);
+ Py_XDECREF(calcsize);
+ Py_XDECREF(fmt);
+ Py_XDECREF(res);
+ return itemsize;
+}
+
int
PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
{