-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-119182: Add PyUnicodeWriter C API #119184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
3c4da2e
b12f085
175c239
99fa2cb
e3e15f0
1dbb5df
8f02e33
a1d0ab0
e6195b7
4865d43
79b7c09
db02dae
10343b0
957dc05
52efb94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1502,3 +1502,78 @@ They all return ``NULL`` or ``-1`` if an exception occurs. | |||||||||||||||||||||||||||
:c:func:`PyUnicode_InternInPlace`, returning either a new Unicode string | ||||||||||||||||||||||||||||
object that has been interned, or a new ("owned") reference to an earlier | ||||||||||||||||||||||||||||
interned string object with the same value. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
PyUnicodeWriter | ||||||||||||||||||||||||||||
^^^^^^^^^^^^^^^ | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
The :c:type:`PyUnicodeWriter` API can be used to create a Python :class:`str` | ||||||||||||||||||||||||||||
object. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. versionadded:: 3.14 | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:type:: PyUnicodeWriter | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
An Unicode writer instance. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:function:: PyUnicodeWriter* PyUnicodeWriter_Create(Py_ssize_t length) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Create an Unicode writer instance. | ||||||||||||||||||||||||||||
encukou marked this conversation as resolved.
Show resolved
Hide resolved
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Set an exception and return ``NULL`` on error. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:function:: void PyUnicodeWriter_Discard(PyUnicodeWriter *writer) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Discard an Unicode writer instance: free its memory. | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
10000
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:function:: PyObject* PyUnicodeWriter_Finish(PyUnicodeWriter *writer) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Get the final Python :class:`str` object and free the writer instance. | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Set an exception and return ``NULL`` on error. | ||||||||||||||||||||||||||||
erlend-aasland marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:function:: int PyUnicodeWriter_WriteChar(PyUnicodeWriter *writer, Py_UCS4 ch) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Write a single Unicode character. | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Return ``0`` on success, or set an exception and return ``-1`` on error. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:function:: int PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer, const char *str, Py_ssize_t size) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Decode a string from UTF-8 in strict mode and write the output into the | ||||||||||||||||||||||||||||
writer. | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
*size* is the string length in bytes. If *size* is equal to ``-1``, call | ||||||||||||||||||||||||||||
``strlen(str)`` to get the string length. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Return ``0`` on success, or set an exception and return ``-1`` on error. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:function:: int PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *str) | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Call :c:func:`PyObject_Str(obj) <PyObject_Str>` and write the output into | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
the writer. | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Return ``0`` on success, or set an exception and return ``-1`` on error. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:function:: int PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Call :c:func:`PyObject_Repr(obj) <PyObject_Repr>` and write the output into | ||||||||||||||||||||||||||||
the writer. | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Return ``0`` on success, or set an exception and return ``-1`` on error. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:function:: int PyUnicodeWriter_WriteSubstring(PyUnicodeWriter *writer, PyObject *str, Py_ssize_t start, Py_ssize_t end) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Write the substring ``str[start:end]`` into the writer. | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
*str* must be Python :class:`str` object. *start* must be greater than or | ||||||||||||||||||||||||||||
equal to 0, and less than or equal to *end*. *end* must be less than or | ||||||||||||||||||||||||||||
equal to *str* length. | ||||||||||||||||||||||||||||
Comment on lines
+1576
to
+1578
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit; I prefer to use SemBr for paragraphs like this.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL that this is called SemBr! Breaking on comma may be too much, but I prefer to break at the sentence boundary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. You don't need to break at comma, but I often do to minimise future diffs. Alternative suggestion:
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Return ``0`` on success, or set an exception and return ``-1`` on error. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. c:function:: int PyUnicodeWriter_Format(PyUnicodeWriter *writer, const char *format, ...) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Similar to :c:func:`PyUnicode_FromFormat`, but write directly the output | ||||||||||||||||||||||||||||
into the writer. | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Return ``0`` on success, or set an exception and return ``-1`` on error. | ||||||||||||||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Add a new :c:type:`PyUnicodeWriter` API to create a Python :class:`str` object: | ||
|
||
* :c:func:`PyUnicodeWriter_Create` | ||
* :c:func:`PyUnicodeWriter_Discard` | ||
* :c:func:`PyUnicodeWriter_Finish` | ||
* :c:func:`PyUnicodeWriter_WriteChar` | ||
* :c:func:`PyUnicodeWriter_WriteUTF8` | ||
* :c:func:`PyUnicodeWriter_WriteStr` | ||
* :c:func:`PyUnicodeWriter_WriteRepr` | ||
* :c:func:`PyUnicodeWriter_WriteSubstring` | ||
* :c:func:`PyUnicodeWriter_Format` | ||
|
||
Patch by Victor Stinner. |
Uh oh!
There was an error while loading. Please reload this page.