-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-15913 : Add PyBuffer_SizeFromFormat() #13873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3bf3ff7
74dd813
caa5b68
aef0745
30ac72a
31442ab
98a5576
79d1e3f
618fb40
83e63d2
e12e737
bb8cdf8
77f546b
2cbcec3
1dd3c2a
5cac74f
eddee93
244fdce
dda73c3
f3e8c51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Implement :c:func:`PyBuffer_SizeFromFormat()` function (previously | ||
documented but not implemented): call :func:`struct.calcsize`. | ||
Patch by Joannah Nanjekye. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
nanjekyejoannah marked this conversation as resolved.
Show resolved
Hide resolved
nanjekyejoannah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is not needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. calcsize() might return a integer which doesn't fit into a C Py_ssize_t. It's unlikely, but I prefer to keep the test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know that. I meant that the explicit check is not needed because the control flow will fall through to the |
||
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) | ||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.