File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -320,6 +320,34 @@ Create the bytes string ``b"Hello World"``::
320320 }
321321
322322
323+ Update ``PyBytes_FromStringAndSize() `` code
324+ -------------------------------------------
325+
326+ Example of code using the soft deprecated
327+ ``PyBytes_FromStringAndSize(NULL, size) `` API::
328+
329+ PyObject *result = PyBytes_FromStringAndSize(NULL, num_bytes);
330+ if (result == NULL) {
331+ return NULL;
332+ }
333+ if (safe_memcpy(PyBytes_AS_STRING(result), start, num_bytes) < 0) {
334+ Py_CLEAR(result);
335+ }
336+ return result;
337+
338+ It can now be updated to::
339+
340+ PyBytesWriter *writer = PyBytesWriter_Create(num_bytes);
341+ if (writer == NULL) {
342+ return NULL;
343+ }
344+ if (safe_memcpy(PyBytesWriter_GetData(writer), start, num_bytes) < 0) {
345+ PyBytesWriter_Discard(writer);
346+ return NULL;
347+ }
348+ return PyBytesWriter_Finish(writer);
349+
350+
323351Reference Implementation
324352========================
325353
You can’t perform that action at this time.
0 commit comments