Support ujson.loads(bytearray(...)) and other bytes-like objects.#573
Support ujson.loads(bytearray(...)) and other bytes-like objects.#573bwoodsend merged 2 commits intoultrajson:mainfrom
Conversation
5c90668 to
0809940
Compare
|
Ughh, I hate supporting PyPy |
Codecov Report
@@ Coverage Diff @@
## main #573 +/- ##
==========================================
+ Coverage 91.49% 91.64% +0.14%
==========================================
Files 6 6
Lines 1905 1938 +33
==========================================
+ Hits 1743 1776 +33
Misses 162 162
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
|
Looks like this upstream issue might be related: https://foss.heptapod.net/pypy/pypy/-/issues/3520 |
|
Hmm, I'd have expected this line to fail too if it was the same cause since Line 1156 in 0809940 I'm just going to create a new ticket. |
|
Filed upstream: https://foss.heptapod.net/pypy/pypy/-/issues/3872 How do we want to handle this?
|
|
How about an extra test inside the bytes-like case for PyPy, testing specifically for bool is_bytes_like = !PyObject_GetBuffer(arg, &buffer, PyBUF_C_CONTIGUOUS);
if (is_bytes_like)
{
#ifdef PYPY_VERSION
// PyPy's buffer protocol implementation is buggy: https://foss.heptapod.net/pypy/pypy/-/issues/3872
if (!PyBytes_Check(arg) && !PyByteArray_Check(arg)) {
PyBuffer_Release(&buffer);
PyErr_Format(PyExc_TypeError, "Expected string, bytes, or bytearray object");
return NULL;
}
#endif
raw = buffer.buf;
sarg_length = buffer.len;
}Not sure what we'd want to do once the bug is fixed in PyPy though. Maybe we could change that to a version check, either in the code or somewhere in the build process, but not sure that's the best approach. |
15bfae0 to
efb514b
Compare
JustAnotherArchivist
left a comment
There was a problem hiding this comment.
Some minor things, else LGTM.
Due to a bug in PyPy [1], PyObject_GetBuffer() fails to detect non C contiguous inputs. Allowing ujson.loads() to run on non-contiguous buffers such as a strided NumPy array would lead to mayhem. Approximate the intention of PyObject_GetBuffer() by explicitly checking that the input is within the set of common bytes-like objects known to be unconditionally C contiguous. - [1]: https://foss.heptapod.net/pypy/pypy/-/issues/3872
efb514b to
f93f2f1
Compare
Fixes #572
Changes proposed in this pull request:
ujson.loads()on bytes to any C contiguous bytes like object such asbytearray(),memoryview()andarray.array().