Skip to content
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

Improve PyBytes_FromStringAndSize test coverage #121842

Closed
sobolevn opened this issue Jul 16, 2024 · 0 comments
Closed

Improve PyBytes_FromStringAndSize test coverage #121842

sobolevn opened this issue Jul 16, 2024 · 0 comments
Assignees
Labels
tests Tests in the Lib/test dir topic-C-API

Comments

@sobolevn
Copy link
Member

sobolevn commented Jul 16, 2024

There's a fast-path in PyBytes_FromStringAndSize that is not covered:

if (size == 1 && str != NULL) {
op = CHARACTER(*str & 255);
assert(_Py_IsImmortal(op));
return (PyObject *)op;
}

Here:

PyObject *
PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
{
PyBytesObject *op;
if (size < 0) {
PyErr_SetString(PyExc_SystemError,
"Negative size passed to PyBytes_FromStringAndSize");
return NULL;
}
if (size == 1 && str != NULL) {
op = CHARACTER(*str & 255);
assert(_Py_IsImmortal(op));
return (PyObject *)op;
}

We don't have test cases for it:

def test_fromstringandsize(self):
# Test PyBytes_FromStringAndSize()
fromstringandsize = _testlimitedcapi.bytes_fromstringandsize
self.assertEqual(fromstringandsize(b'abc'), b'abc')
self.assertEqual(fromstringandsize(b'abc', 2), b'ab')
self.assertEqual(fromstringandsize(b'abc\0def'), b'abc\0def')
self.assertEqual(fromstringandsize(b'', 0), b'')
self.assertEqual(fromstringandsize(NULL, 0), b'')
self.assertEqual(len(fromstringandsize(NULL, 3)), 3)
self.assertRaises((MemoryError, OverflowError),
fromstringandsize, NULL, PY_SSIZE_T_MAX)
self.assertRaises(SystemError, fromstringandsize, b'abc', -1)
self.assertRaises(SystemError, fromstringandsize, b'abc', PY_SSIZE_T_MIN)
self.assertRaises(SystemError, fromstringandsize, NULL, -1)
self.assertRaises(SystemError, fromstringandsize, NULL, PY_SSIZE_T_MIN)

I propose adding:

self.assertEqual(fromstringandsize(b'a'), b'a')
self.assertEqual(fromstringandsize(b'a', 1), b'a')

So we would have better C coverage stats. It won't hurt in any case, even if this path is removed some day.

Linked PRs

@sobolevn sobolevn added tests Tests in the Lib/test dir topic-C-API labels Jul 16, 2024
@sobolevn sobolevn self-assigned this Jul 16, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Jul 16, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 17, 2024
…honGH-121843)

(cherry picked from commit f6c7d8d)

Co-authored-by: sobolevn <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 17, 2024
…honGH-121843)

(cherry picked from commit f6c7d8d)

Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit that referenced this issue Jul 17, 2024
…-121843) (#121894)

gh-121842: Improve coverage of `PyBytes_FromStringAndSize` (GH-121843)
(cherry picked from commit f6c7d8d)

Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit that referenced this issue Jul 17, 2024
…-121843) (#121893)

gh-121842: Improve coverage of `PyBytes_FromStringAndSize` (GH-121843)
(cherry picked from commit f6c7d8d)

Co-authored-by: sobolevn <mail@sobolevn.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir topic-C-API
Projects
None yet
Development

No branches or pull requests

1 participant