Skip to content

gh-76535: Make PyUnicode_ToLowerFull and friends public #136176

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Disallow passing NULL
  • Loading branch information
lysnikolaou committed Jul 2, 2025
commit 6a974c44b5767aa95319e412de0091bae02fea18
20 changes: 8 additions & 12 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,8 @@ These APIs can be used for fast direct character conversions:
Convert *ch* to lower case, store result in *buffer*, which should be
able to hold as many characters needed for *ch* to be lower cased
(e.g. a maximum of two character for Unicode 16.0), and
return the number of characters stored. Passing a ``NULL`` buffer returns
the buffer size needed. If at some point a buffer overflow is detected,
an :exc:`ValueError` is raised and ``-1`` is returned.
return the number of characters stored. If at some point a buffer overflow
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.

.. versionadded:: next

Expand All @@ -324,9 +323,8 @@ These APIs can be used for fast direct character conversions:
Convert *ch* to upper case, store result in *buffer*, which should be
able to hold as many characters needed for *ch* to be upper cased
(e.g. a maximum of three character for Unicode 16.0), and
return the number of characters stored. Passing a ``NULL`` buffer returns
the buffer size needed. If at some point a buffer overflow is detected,
an :exc:`ValueError` is raised and ``-1`` is returned.
return the number of characters stored. If at some point a buffer overflow
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.

.. versionadded:: next

Expand All @@ -336,9 +334,8 @@ These APIs can be used for fast direct character conversions:
Convert *ch* to title case, store result in *buffer*, which should be
able to hold as many characters needed for *ch* to be title cased
(e.g. a maximum of three character for Unicode 16.0), and
return the number of characters stored. Passing a ``NULL`` buffer returns
the buffer size needed. If at some point a buffer overflow is detected,
an :exc:`ValueError` is raised and ``-1`` is returned.
return the number of characters stored. If at some point a buffer overflow
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.

.. versionadded:: next

Expand All @@ -348,9 +345,8 @@ These APIs can be used for fast direct character conversions:
Foldcase *ch*, store result in *buffer*, which should be
able to hold as many characters needed for *ch* to be foldcased
(e.g. a maximum of three character for Unicode 16.0), and
return the number of characters stored. Passing a ``NULL`` buffer returns
the buffer size needed. If at some point a buffer overflow is detected,
an :exc:`ValueError` is raised and ``-1`` is returned.
return the number of characters stored. If at some point a buffer overflow
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.

.. versionadded:: next

Expand Down
73 changes: 31 additions & 42 deletions Objects/unicodectype.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,20 @@ int PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *res, int size)
int n = ctype->lower >> 24;
int i;
for (i = 0; i < n; i++) {
if (res != NULL) {
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[i] = _PyUnicode_ExtendedCase[index + i];
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[i] = _PyUnicode_ExtendedCase[index + i];
}
return n;
}

if (res != NULL) {
if (0 >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[0] = ch + ctype->lower;
if (0 >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[0] = ch + ctype->lower;
return 1;
}

Expand All @@ -237,23 +233,20 @@ int PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *res, int size)
int n = ctype->title >> 24;
int i;
for (i = 0; i < n; i++) {
if (res != NULL) {
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[i] = _PyUnicode_ExtendedCase[index + i];
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[i] = _PyUnicode_ExtendedCase[index + i];
}
return n;
}
if (res != NULL) {
if (0 >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[0] = ch + ctype->title;

if (0 >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[0] = ch + ctype->title;
return 1;
}

Expand All @@ -266,23 +259,20 @@ int PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *res, int size)
int n = ctype->upper >> 24;
int i;
for (i = 0; i < n; i++) {
if (res != NULL) {
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[i] = _PyUnicode_ExtendedCase[index + i];
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[i] = _PyUnicode_ExtendedCase[index + i];
}
return n;
}
if (res != NULL) {
if (0 >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[0] = ch + ctype->upper;

if (0 >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[0] = ch + ctype->upper;
return 1;
}

Expand All @@ -295,16 +285,15 @@ int PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *res, int size)
int n = (ctype->lower >> 20) & 7;
int i;
for (i = 0; i < n; i++) {
if (res != NULL) {
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[i] = _PyUnicode_ExtendedCase[index + i];
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[i] = _PyUnicode_ExtendedCase[index + i];
}
return n;
}

return PyUnicode_ToLower(ch, res, size);
}

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy