gh-155742: Use PyBytesWriter in marshal - #155748
Conversation
Replace soft deprecated PyBytes_FromStringAndSize() and _PyBytes_Resize() with PyBytesWriter.
No PyBytesWriter is needed.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
I do not think the current code is broken.
I didn't say that the current code is broken. The PR only just avoids the soft deprecated PyBytes_FromStringAndSize() function. I reworked the error handling. @serhiy-storchaka: Please review the updated PR. |
|
I wrote a script to test manually this PR by injecting import marshal
import io
import _testcapi
obj = b'x' * (1024 * 1024)
file = io.BytesIO()
for i in range(10):
try:
try:
_testcapi.set_nomemory(i)
res = marshal.dump(obj, file)
finally:
_testcapi.remove_mem_hooks()
except Exception as exc:
print(f"marshal.dump failed: {exc!r}")
else:
print(f"{res=}")Before, the code failed with an assertion error. With my latest change, the code works is all cases (always raise MemoryError as expected). |
|
Ah, I noticed that the PyMarshal C API is not tested by test_capi currently. So I wrote PR gh-156890 to add tests. |
|
I extracted the TYPE_STRING change: it does in fact fix an issue, using |
|
I do not think we need this change. It looks to me like a code churn which makes the code more complicated. |
Replace soft deprecated PyBytes_FromStringAndSize() and _PyBytes_Resize() with PyBytesWriter.