Skip to content

bpo-15913 : Add PyBuffer_SizeFromFormat()#13873

Merged
vstinner merged 20 commits into
python:masterfrom
nanjekyejoannah:issue15913
Aug 20, 2019
Merged

bpo-15913 : Add PyBuffer_SizeFromFormat()#13873
vstinner merged 20 commits into
python:masterfrom
nanjekyejoannah:issue15913

Conversation

@nanjekyejoannah

@nanjekyejoannah nanjekyejoannah commented Jun 6, 2019

Copy link
Copy Markdown
Contributor

Added implementation for PyBuffer_SizeFromFormat().

https://bugs.python.org/issue15913

@nanjekyejoannah

nanjekyejoannah commented Jun 6, 2019

Copy link
Copy Markdown
Contributor Author

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

pablogsal commented Jun 6, 2019

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Comment thread Doc/c-api/buffer.rst Outdated
Comment thread Objects/abstract.c
Comment thread Objects/abstract.c Outdated
Comment thread Doc/c-api/buffer.rst
@bedevere-bot

Copy link
Copy Markdown

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

vstinner commented Jun 7, 2019

Copy link
Copy Markdown
Member

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
Copy Markdown
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

vstinner commented Jun 7, 2019

Copy link
Copy Markdown
Member

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
Comment thread Lib/test/test_buffer.py Outdated
Comment thread Objects/abstract.c Outdated
Comment thread Objects/abstract.c Outdated
Comment thread Objects/abstract.c Outdated
Comment thread Objects/abstract.c Outdated
Comment thread Lib/test/test_buffer.py Outdated
Comment thread Objects/abstract.c Outdated
Comment thread Objects/abstract.c Outdated
Comment thread Doc/c-api/buffer.rst Outdated
Comment thread Modules/_testcapimodule.c Outdated
return NULL;
}

return PyLong_FromSsize_t(PyBuffer_SizeFromFormat(format));

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

Comment thread Objects/abstract.c
}

itemsize = PyLong_AsSsize_t(res);
if (itemsize < 0) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

nanjekyejoannah commented Aug 16, 2019

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor Author

I have made the requested changes; please review again .

@bedevere-bot

Copy link
Copy Markdown

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. No newline at end of file

Copy link
Copy Markdown
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`.

Comment thread Lib/test/test_buffer.py Outdated

@support.cpython_only
def test_pybuffer_size_from_format(self):
nitems = randrange(1, 30)

Copy link
Copy Markdown
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'):

Comment thread Modules/_testcapimodule.c Outdated
{"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
Copy Markdown
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
Copy Markdown

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.

Comment thread Modules/_testcapimodule.c Outdated
const char *format;
Py_ssize_t result;

if (!PyArg_ParseTuple(args, "s:pybuffer_size_from_format",

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

Comment thread Lib/test/test_buffer.py Outdated
@support.cpython_only
def test_pybuffer_size_from_format(self):
# basic tests
for format in ("i", "3s", "0i", " "):

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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