Skip to content

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

Merged
merged 20 commits into from
Aug 20, 2019

Conversation

nanjekyejoannah
Copy link
Contributor

@nanjekyejoannah nanjekyejoannah commented Jun 6, 2019

Added implementation for PyBuffer_SizeFromFormat().

https://bugs.python.org/issue15913

@nanjekyejoannah
Copy link
Contributor Author

nanjekyejoannah commented Jun 6, 2019

cc @skrah @pitrou @vstinner

@nanjekyejoannah nanjekyejoannah changed the title bpo-15913 : PyBuffer_SizeFromFormat is missing bpo-15913 : Add PyBuffer_SizeFromFormat() Jun 6, 2019
@pablogsal pablogsal requested a review from vstinner June 6, 2019 22:03
@pablogsal
Copy link
Member

pablogsal commented Jun 6, 2019

I think this needs tests. Implementation aside, calling the current function with 3si 3s 0i segfaults for me with a bus error.

@nanjekyejoannah
Copy link
Contributor Author

@pablogsal , thought about that also but am still wondering where to place this test?

@nanjekyejoannah nanjekyejoannah changed the title bpo-15913 : Add PyBuffer_SizeFromFormat() [WIP]bpo-15913 : Add PyBuffer_SizeFromFormat() Jun 6, 2019
@pablogsal
Copy link
Member

@pablogsal , thought about that also but am still wondering where to place this test?

I would say exposed in Modules/_testcapimodule.c and then tested with Lib/test/test_capi.py but Victor or Antoine would know better regarding this specific API.

@nanjekyejoannah nanjekyejoannah changed the title [WIP]bpo-15913 : Add PyBuffer_SizeFromFormat() bpo-15913 : Add PyBuffer_SizeFromFormat() Jun 6, 2019
@nanjekyejoannah nanjekyejoannah changed the title bpo-15913 : Add PyBuffer_SizeFromFormat() [WIP]bpo-15913 : Add PyBuffer_SizeFromFormat() Jun 7, 2019
@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@vstinner
Copy link
Member

vstinner commented Jun 7, 2019

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 @cpython_only, or skipped if _testcapi is None).

@nanjekyejoannah
Copy link
Contributor Author

I have made some changes locally that will address some of the comments @vstinner. I will update the PR when I finish the tests.

@vstinner
Copy link
Member

vstinner commented Jun 7, 2019

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 ;-)

@nanjekyejoannah nanjekyejoannah changed the title [WIP]bpo-15913 : Add PyBuffer_SizeFromFormat() bpo-15913 : Add PyBuffer_SizeFromFormat() Jun 11, 2019
return NULL;
}

return PyLong_FromSsize_t(PyBuffer_SizeFromFormat(format));
Copy link
Contributor

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.

Copy link
Member

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) {
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

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.

@vstinner
Copy link
Member

Without .encode, the test failed with:

-----------------------------------
Traceback (most recent call last):
  File "/home/travis/build/python/cpython/Lib/test/test_buffer.py", line 4425, in test_pybuffer_size_from_format
    struct.calcsize(format))
BytesWarning: Comparison between bytes and string

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?

@nanjekyejoannah
Copy link
Contributor Author

nanjekyejoannah commented Aug 16, 2019

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 fmt = PyBytes_FromString(format) to fmt = PyUnicode_FromString(format); in order to do away with the comparison between bytes and string error.

The test passes now :

gitpod /workspace/cpython $ ./python -bb -m test test_buffer
Run tests sequentially
0:00:00 load avg: 0.69 [1/1] test_buffer

== Tests result: SUCCESS ==

1 test OK.

@nanjekyejoannah
Copy link
Contributor Author

I have made the requested changes; please review again .

@bedevere-bot
Copy link

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.
Copy link
Member

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`.

@@ -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)
Copy link
Member

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'):

@@ -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},
Copy link
Member

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:

Suggested change
{"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.

@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

const char *format;
Py_ssize_t result;

if (!PyArg_ParseTuple(args, "s:pybuffer_size_from_format",
Copy link
Member

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.

@support.cpython_only
def test_pybuffer_size_from_format(self):
# basic tests
for format in ("i", "3s", "0i", " "):
Copy link
Member

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

@nanjekyejoannah
Copy link
Contributor Author

@vstinner I don't think this CI fail is now my business. Is it? i.e

Windows PR Tests win64: "The job running on agent Azure Pipelines 7 has exceeded the maximum execution time of 60. For more information, see https://go.microsoft.com/fwlink/?linkid=2077134 "

@vstinner vstinner merged commit 9e66aba into python:master Aug 20, 2019
@vstinner
Copy link
Member

I merged your PR @nanjekyejoannah, thanks!

@vstinner I don't think this CI fail is now my business.

I don't know why Azure CI failed, but this CI is not mandatory yet.

@nanjekyejoannah nanjekyejoannah deleted the issue15913 branch September 5, 2019 22:11
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
Implement PyBuffer_SizeFromFormat() function (previously
documented but not implemented): call struct.calcsize().
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
Implement PyBuffer_SizeFromFormat() function (previously
documented but not implemented): call struct.calcsize().
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
Implement PyBuffer_SizeFromFormat() function (previously
documented but not implemented): call struct.calcsize().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants