10000 Add PyUnicodeWriter_WriteASCII() (#145) · python/pythoncapi-compat@ffae0ff · GitHub
[go: up one dir, main page]

Skip to content

Commit ffae0ff

Browse files
authored
Add PyUnicodeWriter_WriteASCII() (#145)
1 parent fde4d34 commit ffae0ff

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

docs/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ Python 3.14
136136

137137
See `PyUnicodeWriter_WriteUTF8() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicodeWriter_WriteUTF8>`__.
138138

139+
.. c:function:: int PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer, const char *str, Py_ssize_t size)
140+
141+
See `PyUnicodeWriter_WriteASCII() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicodeWriter_WriteASCII>`__.
142+
139143
.. c:function:: int PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer, const wchar_t *str, Py_ssize_t size)
140144

141145
See `PyUnicodeWriter_WriteWideChar() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicodeWriter_WriteWideChar>`__.

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Changelog
22
=========
33

4+
* 2025-06-09: Add ``PyUnicodeWriter_WriteASCII()`` function.
45
* 2025-06-03: Add functions:
56

67
* ``PySys_GetAttr()``

pythoncapi_compat.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,18 @@ PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer,
14561456
return res;
14571457
}
14581458

1459+
static inline int
1460+
PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer,
1461+
const char *str, Py_ssize_t size)
1462+
{
1463+
if (size < 0) {
1464+
size = (Py_ssize_t)strlen(str);
1465+
}
1466+
1467+
return _PyUnicodeWriter_WriteASCIIString((_PyUnicodeWriter*)writer,
1468+
str, size);
1469+
}
1470+
14591471
static inline int
14601472
PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer,
14611473
const wchar_t *str, Py_ssize_t size)

tests/test_pythoncapi_compat_cext.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,11 @@ test_unicodewriter(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
18471847
goto error;
18481848
}
18491849

1850+
// test PyUnicodeWriter_WriteASCII()
1851+
if (PyUnicodeWriter_WriteASCII(writer, " non-ASCII", -1) < 0) {
1852+
goto error;
1853+
}
1854+
18501855
// test PyUnicodeWriter_WriteUTF8()
18511856
if (PyUnicodeWriter_WriteUTF8(writer, " valu\xC3\xA9", -1) < 0) {
18521857
goto error;
@@ -1870,7 +1875,7 @@ test_unicodewriter(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
18701875
if (result == NULL) {
18711876
return NULL;
18721877
}
1873-
assert(PyUnicode_EqualToUTF8(result, "var=long valu\xC3\xA9 'repr'"));
1878+
assert(PyUnicode_EqualToUTF8(result, "var=long non-ASCII valu\xC3\xA9 'repr'"));
18741879
Py_DECREF(result);
18751880
}
18761881

0 commit comments

Comments
 (0)
0