Skip to content
Closed
Prev Previous commit
Next Next commit
Rename size parameter to nbytes
  • Loading branch information
vstinner committed Jun 21, 2024
commit 62f4598501a4e6918a58abff2c4f8d284a4afbf5
10 changes: 5 additions & 5 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,19 @@ APIs:
.. versionadded:: 3.3


.. c:function:: const void* PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_ssize_t *size, uint32_t *format)
.. c:function:: const void* PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_ssize_t *nbytes, uint32_t *format)

Export the contents of the *unicode* string in one of the requested format
*requested_formats*.

* On success, set *\*size* and *\*format*, and return the contents.
* On success, set *\*nbytes* and *\*format*, and return the contents.
* On error, set an exception and return ``NULL``.

The contents is valid as long as *unicode* is valid.

The export must be released by :c:func:`PyUnicode_ReleaseExport`.
Comment thread
vstinner marked this conversation as resolved.

*unicode*, *size* and *format* must not be NULL.
*unicode*, *nbytes* and *format* must not be NULL.

Available formats:

Expand Down Expand Up @@ -382,14 +382,14 @@ APIs:
.. versionadded:: 3.14


.. c:function:: PyObject* PyUnicode_Import(const void *data, Py_ssize_t size, uint32_t format)
.. c:function:: PyObject* PyUnicode_Import(const void *data, Py_ssize_t nbytes, uint32_t format)

Import a string from the *format* format.

* Return a reference to a new string object on success.
* Set an exception and return ``NULL`` on error.

*data* must not be NULL. *size* must be positive or zero.
*data* must not be NULL. *nbytes* must be positive or zero.

See :c:func:`PyUnicode_Export` for the available formats.

Expand Down
6 changes: 3 additions & 3 deletions Include/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ PyAPI_FUNC(PyObject *) PyUnicode_InternFromString(
#define PyUnicode_FORMAT_UTF8 0x10

// Get the content of a string in the requested format:
// - Return the content, set '*size' and '*format' on success.
// - Return the content, set '*nbytes' and '*format' on success.
// - Set an exception and return NULL on error.
//
// The export must be released by PyUnicode_ReleaseExport().
PyAPI_FUNC(const void*) PyUnicode_Export(
PyObject *unicode,
uint32_t requested_formats,
Py_ssize_t *size,
Py_ssize_t *nbytes,
uint32_t *format);

// Release an export created by PyUnicode_Export().
Expand All @@ -276,7 +276,7 @@ PyAPI_FUNC(void) PyUnicode_ReleaseExport(
// - Set an exception and return NULL on error.
PyAPI_FUNC(PyObject*) PyUnicode_Import(
const void *data,
Py_ssize_t size,
Py_ssize_t nbytes,
uint32_t format);

/* --- wchar_t support for platforms which support it --------------------- */
Expand Down
49 changes: 24 additions & 25 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size)

const void*
PyUnicode_Export(PyObject *unicode, uint32_t requested_formats,
Py_ssize_t *size, uint32_t *format)
Py_ssize_t *nbytes, uint32_t *format)
{
if (!PyUnicode_Check(unicode)) {
PyErr_Format(PyExc_TypeError, "must be str, not %T", unicode);
Expand All @@ -2112,7 +2112,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats,
&& (requested_formats & PyUnicode_FORMAT_ASCII))
{
*format = PyUnicode_FORMAT_ASCII;
*size = len;
*nbytes = len;
return PyUnicode_1BYTE_DATA(unicode);
}

Expand All @@ -2122,7 +2122,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats,
&& (requested_formats & PyUnicode_FORMAT_UCS1))
{
*format = PyUnicode_FORMAT_UCS1;
*size = len;
*nbytes = len;
return PyUnicode_1BYTE_DATA(unicode);
}

Expand All @@ -2131,7 +2131,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats,
&& (requested_formats & PyUnicode_FORMAT_UCS2))
{
*format = PyUnicode_FORMAT_UCS2;
*size = len * 2;
*nbytes = len * 2;
return PyUnicode_2BYTE_DATA(unicode);
}

Expand All @@ -2152,7 +2152,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats,
ucs2[len] = 0;

*format = PyUnicode_FORMAT_UCS2;
*size = len * 2;
*nbytes = len * 2;
return ucs2;
}

Expand All @@ -2161,7 +2161,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats,
&& (requested_formats & PyUnicode_FORMAT_UCS4))
{
*format = PyUnicode_FORMAT_UCS4;
*size = len * 4;
*nbytes = len * 4;
return PyUnicode_4BYTE_DATA(unicode);
}

Expand All @@ -2172,14 +2172,14 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats,
goto error;
}
*format = PyUnicode_FORMAT_UCS4;
*size = len * 4;
*nbytes = len * 4;
return ucs4;
}

// Convert to UTF-8
if (requested_formats & PyUnicode_FORMAT_UTF8) {
// Encode UCS1, UCS2 or UCS4 to UTF-8
const char *utf8 = PyUnicode_AsUTF8AndSize(unicode, size);
const char *utf8 = PyUnicode_AsUTF8AndSize(unicode, nbytes);
if (utf8 == NULL) {
goto error;
}
Expand All @@ -2190,7 +2190,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats,
PyErr_Format(PyExc_ValueError, "unable to find a matching export format");

error:
*size = 0;
*nbytes = 0;
*format = 0;
return NULL;
}
Expand Down Expand Up @@ -2221,44 +2221,43 @@ PyUnicode_ReleaseExport(PyObject *unicode, const void* data,
}

PyObject*
PyUnicode_Import(const void *data, Py_ssize_t size,
PyUnicode_Import(const void *data, Py_ssize_t nbytes,
uint32_t format)
{
if (size < 0) {
PyErr_SetString(PyExc_ValueError, "Negative size");
if (nbytes < 0) {
PyErr_SetString(PyExc_ValueError, "Negative nbytes");
return NULL;
}

switch (format)
{
case PyUnicode_FORMAT_ASCII:
return PyUnicode_DecodeASCII((const char*)data, size, NULL);
return PyUnicode_DecodeASCII((const char*)data, nbytes, NULL);

case PyUnicode_FORMAT_UCS1:
return _PyUnicode_FromUCS1(data, size);
return _PyUnicode_FromUCS1(data, nbytes);

case PyUnicode_FORMAT_UCS2:
if (size % 2) {
PyErr_Format(PyExc_ValueError, "size must be a multiple of 2: %zd",
size);
if (nbytes % 2) {
PyErr_Format(PyExc_ValueError, "nbytes must be a multiple of 2: %zd",
nbytes);
return NULL;
}
return _PyUnicode_FromUCS2(data, size / 2);
return _PyUnicode_FromUCS2(data, nbytes / 2);

case PyUnicode_FORMAT_UCS4:
if (size % 4) {
PyErr_Format(PyExc_ValueError, "size must be a multiple of 4: %zd",
size);
if (nbytes % 4) {
PyErr_Format(PyExc_ValueError, "nbytes must be a multiple of 4: %zd",
nbytes);
return NULL;
}
return _PyUnicode_FromUCS4(data, size / 4);
return _PyUnicode_FromUCS4(data, nbytes / 4);

case PyUnicode_FORMAT_UTF8:
return PyUnicode_DecodeUTF8((const char*)data, size, NULL);
return PyUnicode_DecodeUTF8((const char*)data, nbytes, NULL);

default:
PyErr_Format(PyExc_ValueError, "unknown format: %i",
format);
PyErr_Format(PyExc_ValueError, "unknown format: %i", format);
return NULL;
}
}
Expand Down