Skip to content

Commit e409c19

Browse files
committed
gh-158585: Add 3 examples to PyBytesWriter documentation
1 parent 224b4b0 commit e409c19

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

‎Doc/c-api/bytes.rst‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,112 @@ Low-level API
464464
return NULL;
465465
}
466466
return (char*)PyBytesWriter_GetData(writer) + pos;
467+
468+
469+
Example creating the string ``b'abc'``
470+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
471+
472+
Example creating the bytes string ``b"abc"``, with a fixed size of 3 bytes::
473+
474+
PyObject* create_abc(void)
475+
{
476+
PyBytesWriter *writer = PyBytesWriter_Create(3);
477+
if (writer == NULL) {
478+
return NULL;
479+
}
480+
481+
char *str = PyBytesWriter_GetData(writer);
482+
memcpy(str, "abc", 3);
483+
return PyBytesWriter_Finish(writer);
484+
}
485+
486+
Variant using a pointer to compute the final size::
487+
488+
PyObject* create_abc_ptr(void)
489+
{
490+
PyBytesWriter *writer = PyBytesWriter_Create(3);
491+
if (writer == NULL) {
492+
return NULL;
493+
}
494+
495+
char *str = PyBytesWriter_GetData(writer);
496+
memcpy(str, "abc", 3);
497+
str += 1;
498+
return PyBytesWriter_FinishWithPointer(writer, str);
499+
}
500+
501+
502+
Update ``PyBytes_FromStringAndSize()`` code
503+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
504+
505+
Example of code using the soft deprecated
506+
``PyBytes_FromStringAndSize(NULL, size)`` API::
507+
508+
int copy_bytes(char *dest, const char *src, Py_ssize_t num_bytes)
509+
{
510+
...
511+
}
512+
513+
PyObject* create_string(const char *start, Py_ssize_t num_bytes)
514+
{
515+
PyObject *result = PyBytes_FromStringAndSize(NULL, num_bytes);
516+
if (result == NULL) {
517+
return NULL;
518+
}
519+
if (copy_bytes(PyBytes_AS_STRING(result), start, num_bytes) < 0) {
520+
Py_CLEAR(result);
521+
}
522+
return result;
523+
}
524+
525+
It can now be updated to::
526+
527+
PyObject* create_string(const char *start, Py_ssize_t num_bytes)
528+
{
529+
PyBytesWriter *writer = PyBytesWriter_Create(num_bytes);
530+
if (writer == NULL) {
531+
return NULL;
532+
}
533+
if (copy_bytes(PyBytesWriter_GetData(writer), start, num_bytes) < 0) {
534+
PyBytesWriter_Discard(writer);
535+
return NULL;
536+
}
537+
return PyBytesWriter_Finish(writer);
538+
}
539+
540+
541+
Update ``_PyBytes_Resize()`` code
542+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
543+
544+
Example of code using the soft deprecated ``_PyBytes_Resize()`` API::
545+
546+
void create_string(Py_ssize_t size)
547+
{
548+
PyObject *bytes = PyBytes_FromStringAndSize(NULL, size);
549+
if (bytes == NULL) {
550+
return NULL;
551+
}
552+
char *str = PyBytes_AS_STRING(bytes);
553+
554+
// ... write data into str ...
555+
556+
if (_PyBytes_Resize(&bytes, (str - PyBytes_AS_STRING(bytes)))) {
557+
return NULL;
558+
}
559+
return bytes;
560+
}
561+
562+
It can now be updated to::
563+
564+
void create_string(Py_ssize_t size)
565+
{
566+
PyBytesWriter *writer = PyBytesWriter_Create(size);
567+
if (writer == NULL) {
568+
return NULL;
569+
}
570+
char *str = PyBytesWriter_GetData(writer);
571+
572+
// ... write data into str ...
573+
574+
return PyBytesWriter_FinishWithPointer(writer, str);
575+
}

0 commit comments

Comments
 (0)