8000 Add format parameter to PyUnicode_Export() · python/cpython@17ad7b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17ad7b9

Browse files
committed
Add format parameter to PyUnicode_Export()
1 parent 93d4470 commit 17ad7b9

File tree

4 files changed

+16
-60
lines changed

4 files changed

+16
-60
lines changed

Doc/c-api/unicode.rst

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,12 @@ APIs:
341341
.. versionadded:: 3.3
342342
343343
344-
.. c:function:: int PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
344+
.. c:function:: int PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view, uint32_t *format)
345345
346346
Export the contents of the *unicode* string in one of the requested format
347347
*requested_formats*.
348348
349-
* On success, fill *view*, and return ``0``.
349+
* On success, fill *view* and set *\*format*, and return ``0``.
350350
* On error, set an exception and return ``-1``.
351351
352352
The *view* buffer must be released by :c:func:`PyBuffer_Release`.
@@ -372,27 +372,13 @@ APIs:
372372
373373
*requested_formats* can be a single format or a bitwise combination of the
374374
formats in the table above.
375-
To determine the format that was selected for output, call
376-
:c:func:`PyUnicode_GetBufferFormat`.
375+
On success, *\*format* will be set to a single one of the requested flags.
377376
378377
Note that future versions of Python may introduce additional formats.
379378
380379
.. versionadded:: 3.14
381380
382381
383-
.. c:function:: int PyUnicode_GetBufferFormat(const Py_buffer *view, uint32_t *format)
384-
8000
385-
Get the format of the buffer *view*.
386-
387-
* On success, set *\*format* to the corresponding ``PyUnicode_FORMAT_*`` value
388-
and return ``0``.
389-
* On error, set an exception and return ``-1``.
390-
391-
*view* must be a buffer filled by :c:func:`PyUnicode_Export`.
392-
393-
.. versionadded:: 3.14
394-
395-
396382
.. c:function:: PyObject* PyUnicode_Import(const void *data, Py_ssize_t nbytes, uint32_t format)
397383
398384
Create a Unicode string object from a buffer in a supported format.

Include/unicodeobject.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,7 @@ PyAPI_FUNC(PyObject *) PyUnicode_InternFromString(
258258
PyAPI_FUNC(int) PyUnicode_Export(
259259
PyObject *unicode,
260260
uint32_t requested_formats,
261-
Py_buffer *view);
262-
PyAPI_FUNC(int) PyUnicode_GetBufferFormat(
263-
const Py_buffer *view,
261+
Py_buffer *view,
264262
uint32_t *format);
265263
PyAPI_FUNC(PyObject*) PyUnicode_Import(
266264
const void *data,

Modules/_testlimitedcapi/unicode.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,11 +1849,8 @@ unicode_export(PyObject *self, PyObject *args)
18491849
}
18501850

18511851
Py_buffer view;
1852-
if (PyUnicode_Export(obj, requested_formats, &view) < 0) {
1853-
return NULL;
1854-
}
18551852
uint32_t format;
1856-
if (PyUnicode_GetBufferFormat(&view, &format) < 0) {
1853+
if (PyUnicode_Export(obj, requested_formats, &view, &format) < 0) {
18571854
return NULL;
18581855
}
18591856

Objects/unicodeobject.c

Lines changed: 11 additions & 36 deletions
< 10000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,7 @@ PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *pub_writer,
23332333

23342334

23352335
static int
2336-
unicode_export(PyObject *unicode, Py_buffer *view,
2336+
unicode_export(PyObject *unicode, Py_buffer *view, uint32_t *pformat,
23372337
Py_ssize_t len, const void *buf,
23382338
int itemsize, const char *format, uint32_t internal_format)
23392339
{
@@ -2344,12 +2344,14 @@ unicode_export(PyObject *unicode, Py_buffer *view,
23442344
view->itemsize = itemsize;
23452345
view->format = (char*)format;
23462346
view->internal = (void*)(uintptr_t)internal_format;
2347+
*pformat = internal_format;
23472348
return 0;
23482349
}
23492350

23502351

23512352
int
2352-
PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
2353+
PyUnicode_Export(PyObject *unicode, uint32_t requested_formats,
2354+
Py_buffer *view, uint32_t *format)
23532355
{
23542356
#if SIZEOF_INT == 4
23552357
# define BUFFER_UCS4 "I"
@@ -2369,7 +2371,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
23692371
if (PyUnicode_IS_ASCII(unicode)
23702372
&& (requested_formats & PyUnicode_FORMAT_ASCII))
23712373
{
2372-
return unicode_export(unicode, view,
2374+
return unicode_export(unicode, view, format,
23732375
len, PyUnicode_1BYTE_DATA(unicode),
23742376
1, "B", PyUnicode_FORMAT_ASCII);
23752377
}
@@ -2379,7 +2381,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
23792381
if (kind == PyUnicode_1BYTE_KIND
23802382
&& (requested_formats & PyUnicode_FORMAT_UCS1))
23812383
{
2382-
return unicode_export(unicode, view,
2384+
return unicode_export(unicode, view, format,
23832385
len, PyUnicode_1BYTE_DATA(unicode),
23842386
1, "B", PyUnicode_FORMAT_UCS1);
23852387
}
@@ -2388,7 +2390,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
23882390
if (kind == PyUnicode_2BYTE_KIND
23892391
&& (requested_formats & PyUnicode_FORMAT_UCS2))
23902392
{
2391-
return unicode_export(unicode, view,
2393+
return unicode_export(unicode, view, format,
23922394
len, PyUnicode_2BYTE_DATA(unicode),
23932395
2, "H", PyUnicode_FORMAT_UCS2);
23942396
}
@@ -2409,7 +2411,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
24092411
ucs2);
24102412
ucs2[len] = 0;
24112413

2412-
return unicode_export(unicode, view,
2414+
return unicode_export(unicode, view, format,
24132415
len, ucs2,
24142416
2, "H", PyUnicode_FORMAT_UCS2);
24152417
}
@@ -2418,7 +2420,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
24182420
if (kind == PyUnicode_4BYTE_KIND
24192421
&& (requested_formats & PyUnicode_FORMAT_UCS4))
24202422
{
2421-
return unicode_export(unicode, view,
2423+
return unicode_export(unicode, view, format,
24222424
len, PyUnicode_4BYTE_DATA(unicode),
24232425
4, BUFFER_UCS4, PyUnicode_FORMAT_UCS4);
24242426
}
@@ -2429,7 +2431,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
24292431
if (ucs4 == NULL) {
24302432
return -1;
24312433
}
2432-
return unicode_export(unicode, view,
2434+
return unicode_export(unicode, view, format,
24332435
len, ucs4,
24342436
4, BUFFER_UCS4, PyUnicode_FORMAT_UCS4);
24352437
}
@@ -2441,7 +2443,7 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
24412443
if (utf8 == NULL) {
24422444
return -1;
24432445
}
2444-
return unicode_export(unicode, view,
2446+
return unicode_export(unicode, view, format,
24452447
nbytes, utf8,
24462448
1, "B", PyUnicode_FORMAT_UTF8);
24472449
}
@@ -2454,33 +2456,6 @@ PyUnicode_Export(PyObject *unicode, uint32_t requested_formats, Py_buffer *view)
24542456
}
24552457

24562458

2457-
int
2458-
PyUnicode_GetBufferFormat(const Py_buffer *view, uint32_t *format)
2459-
{
2460-
if (view->obj == NULL || !PyUnicode_Check(view->obj)) {
2461-
PyErr_SetString(PyExc_ValueError, "not a str export");
2462-
return -1;
2463-
}
2464-
2465-
uintptr_t internal_format = (uintptr_t)view->internal;
2466-
switch (internal_format)
2467-
{
2468-
case PyUnicode_FORMAT_ASCII:
2469-
case PyUnicode_FORMAT_UCS1:
2470-
case PyUnicode_FORMAT_UCS2:
2471-
case PyUnicode_FORMAT_UCS4:
2472-
case PyUnicode_FORMAT_UTF8:
2473-
break;
2474-
default:
2475-
PyErr_SetString(PyExc_ValueError, "invalid format");
2476-
return -1;
2477-
}
2478-
2479-
*format = (uint32_t)internal_format;
2480-
return 0;
2481-
}
2482-
2483-
24842459
static void
24852460
unicode_releasebuffer(PyObject *unicode, Py_buffer *view)
24862461
{

0 commit comments

Comments
 (0)
0