diff options
author | Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> | 2019-08-20 14:46:36 (GMT) |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2019-08-20 14:46:36 (GMT) |
commit | 9e66aba99925eebacfe137d9deb0ef1fdbc2d5db (patch) | |
tree | e9353ac99e9911959aadb9092ae35237ec06b5e2 /Objects | |
parent | 18f8dcfa10d8a858b152d12a9ad8fa83b7e967f0 (diff) | |
download | cpython-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.c | 42 |
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) { |