Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-133968: Add PyUnicodeWriter_WriteASCII() function#133973
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
2aa2e87
fc08c32
14c22c3
33b3276
e7ca52f
25e9444
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1806,9 +1806,24 @@ object. | ||
See also :c:func:`PyUnicodeWriter_DecodeUTF8Stateful`. | ||
.. c:function:: int PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer, const char *str, Py_ssize_t size) | ||
ZeroIntensity marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
Write the ASCII string *str* into *writer*. | ||
*size* is the string length in bytes. If *size* is equal to ``-1``, call | ||
``strlen(str)`` to get the string length. | ||
*str* must only contain ASCII characters. The behavior is undefined if | ||
*str* contains non-ASCII characters. | ||
On success, return ``0``. | ||
On error, set an exception, leave the writer unchanged, and return ``-1``. | ||
.. versionadded:: next | ||
.. c:function:: int PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer, const wchar_t *str, Py_ssize_t size) | ||
Write the wide string *str* into *writer*. | ||
*size* is a number of wide characters. If *size* is equal to ``-1``, call | ||
``wcslen(str)`` to get the string length. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1776,6 +1776,13 @@ def test_utf8(self): | ||
self.assertEqual(writer.finish(), | ||
"ascii-latin1=\xE9-euro=\u20AC.") | ||
def test_ascii(self): | ||
writer = self.create_writer(0) | ||
writer.write_ascii(b"Hello ", -1) | ||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
writer.write_ascii(b"", 0) | ||
writer.write_ascii(b"Python! <truncated>", 6) | ||
self.assertEqual(writer.finish(), "Hello Python") | ||
def test_invalid_utf8(self): | ||
writer = self.create_writer(0) | ||
with self.assertRaises(UnicodeDecodeError): | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Add :c:func:`PyUnicodeWriter_WriteASCII` function to write an ASCII string | ||
into a :c:type:`PyUnicodeWriter`. The function is faster than | ||
:c:func:`PyUnicodeWriter_WriteUTF8`, but has an undefined behavior if the | ||
input string contains non-ASCII characters. Patch by Victor Stinner. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -14083,6 +14083,20 @@ _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, | ||
return 0; | ||
} | ||
int | ||
PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer, | ||
const char *str, | ||
Py_ssize_t size) | ||
{ | ||
assert(writer != NULL); | ||
_Py_AssertHoldsTstate(); | ||
_PyUnicodeWriter *priv_writer = (_PyUnicodeWriter*)writer; | ||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return _PyUnicodeWriter_WriteASCIIString(priv_writer, str, size); | ||
} | ||
int | ||
PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer, | ||
const char *str, | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -290,7 +290,7 @@ union_repr(PyObject *self) | ||
} | ||
for (Py_ssize_t i = 0; i < len; i++) { | ||
if (i > 0 &&PyUnicodeWriter_WriteASCII(writer, " | ", 3) < 0) { | ||
goto error; | ||
} | ||
PyObject *p = PyTuple_GET_ITEM(alias->args, i); | ||
@@ -300,12 +300,12 @@ union_repr(PyObject *self) | ||
} | ||
#if 0 | ||
PyUnicodeWriter_WriteASCII(writer, "|args=", 6); | ||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
PyUnicodeWriter_WriteRepr(writer, alias->args); | ||
PyUnicodeWriter_WriteASCII(writer, "|h=", 3); | ||
PyUnicodeWriter_WriteRepr(writer, alias->hashable_args); | ||
if (alias->unhashable_args) { | ||
PyUnicodeWriter_WriteASCII(writer, "|u=", 3); | ||
PyUnicodeWriter_WriteRepr(writer, alias->unhashable_args); | ||
} | ||
#endif | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1176,7 +1176,7 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node, | ||
} | ||
if (key_or_null == NULL) { | ||
if (PyUnicodeWriter_WriteASCII(writer, "NULL:\n",6) < 0) { | ||
goto error; | ||
} | ||
@@ -1194,7 +1194,7 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node, | ||
} | ||
} | ||
if (PyUnicodeWriter_WriteASCII(writer, "\n", 1) < 0) { | ||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
goto error; | ||
} | ||
} | ||
@@ -1915,7 +1915,7 @@ hamt_node_array_dump(PyHamtNode_Array *node, | ||
goto error; | ||
} | ||
if (PyUnicodeWriter_WriteASCII(writer, "\n", 1) < 0) { | ||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
goto error; | ||
} | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.