-
-
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
Conversation
I think this needs tests. Implementation aside, calling the current function with |
@pablogsal , thought about that also but am still wondering where to place this test? |
I would say exposed in |
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Would it be possible to add an unit test? Expose the function in _testcapi and add a test in test_buffer using _testcapi (such test should be marked with |
I have made some changes locally that will address some of the comments @vstinner. I will update the PR when I finish the tests. |
Ok. Ping me when I should review again ;-) |
Modules/_testcapimodule.c
Outdated
return NULL; | ||
} | ||
|
||
return PyLong_FromSsize_t(PyBuffer_SizeFromFormat(format)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe PyBuffer_SizeFromFormat()
should be inlined like this. Please check it for failure before the PyLong_FromSsize_t()
call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right, NULL should be returned if the result is -1.
} | ||
|
||
itemsize = PyLong_AsSsize_t(res); | ||
if (itemsize < 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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 comment
The 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 done
label.
Without .encode, the test failed with:
You should be able to reproduce rhe issue using: ./python -bb -m tets test_buffer The test should pass without .encode. You should investigate why it fails. struct.calcsize() accepts format as a Unicode string, no? |
I needed to change The test passes now :
|
I have made the requested changes; please review again . |
Thanks for making the requested changes! @vstinner: please review the changes made to this pull request. |
@@ -0,0 +1 @@ | |||
:c:func:`PyBuffer_SizeFromFormat()` now added. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest:
Implement :c:func:`PyBuffer_SizeFromFormat()` function (previously documented but not implemented): call :func:`struct.calcsize`.
Lib/test/test_buffer.py
Outdated
@@ -4412,6 +4417,12 @@ def test_issue_7385(self): | |||
x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL) | |||
self.assertRaises(BufferError, memoryview, x) | |||
|
|||
@support.cpython_only | |||
def test_pybuffer_size_from_format(self): | |||
nitems = randrange(1, 30) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to avoid using random in such test. iter_format() returns 91 items: it sounds overkill to me. IMHO tTesting 3 formats should but enough, like:
for fmt in ('', 'ii', '3s'):
Modules/_testcapimodule.c
Outdated
@@ -5087,6 +5107,7 @@ static PyMethodDef TestMethods[] = { | |||
{"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS}, | |||
#endif | |||
{"getbuffer_with_null_view", getbuffer_with_null_view, METH_O}, | |||
{"pybuffer_size_from_format", (PyCFunction)pybuffer_size_from_format, METH_VARARGS}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe reuse the same name:
{"pybuffer_size_from_format", (PyCFunction)pybuffer_size_from_format, METH_VARARGS}, | |
{"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS}, |
Note: I don't think that (PyCFunction) cast is needed here.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Modules/_testcapimodule.c
Outdated
const char *format; | ||
Py_ssize_t result; | ||
|
||
if (!PyArg_ParseTuple(args, "s:pybuffer_size_from_format", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You forget to update the function name here.
Lib/test/test_buffer.py
Outdated
@support.cpython_only | ||
def test_pybuffer_size_from_format(self): | ||
# basic tests | ||
for format in ("i", "3s", "0i", " "): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
" " <= my proposal was to test an empty format string
@vstinner I don't think this CI fail is now my business. Is it? i.e
|
I merged your PR @nanjekyejoannah, thanks!
I don't know why Azure CI failed, but this CI is not mandatory yet. |
Implement PyBuffer_SizeFromFormat() function (previously documented but not implemented): call struct.calcsize().
Implement PyBuffer_SizeFromFormat() function (previously documented but not implemented): call struct.calcsize().
Implement PyBuffer_SizeFromFormat() function (previously documented but not implemented): call struct.calcsize().
Added implementation for PyBuffer_SizeFromFormat().
https://bugs.python.org/issue15913