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
Use Py_ssize_t and don't check overflow in loop
  • Loading branch information
lysnikolaou committed Jul 2, 2025
commit e7ef477c36fdd2892c68243d257ec8584cd4ad7a
8 changes: 4 additions & 4 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ These APIs can be used for fast direct character conversions:
possible. This function does not raise exceptions.


.. c:function:: Py_ssize_t PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *buffer, int size)
.. c:function:: Py_ssize_t PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)

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
Expand All @@ -318,7 +318,7 @@ These APIs can be used for fast direct character conversions:
.. versionadded:: next


.. c:function:: Py_ssize_t PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *buffer, int size)
.. c:function:: Py_ssize_t PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)

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
Expand All @@ -329,7 +329,7 @@ These APIs can be used for fast direct character conversions:
.. versionadded:: next


.. c:function:: Py_ssize_t PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *buffer, int size)
.. c:function:: Py_ssize_t PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)

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
Expand All @@ -340,7 +340,7 @@ These APIs can be used for fast direct character conversions:
.. versionadded:: next


.. c:function:: Py_ssize_t PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *buffer, int size)
.. c:function:: Py_ssize_t PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)

Foldcase *ch*, store result in *buffer*, which should be
able to hold as many characters needed for *ch* to be foldcased
Expand Down
16 changes: 8 additions & 8 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -733,28 +733,28 @@ PyAPI_FUNC(int) _PyUnicode_IsAlpha(
Py_UCS4 ch /* Unicode character */
);

PyAPI_FUNC(int) PyUnicode_ToLower(
PyAPI_FUNC(Py_ssize_t) PyUnicode_ToLower(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res, /* Output buffer */
int size /* Buffer size */
Py_ssize_t size /* Buffer size */
);

PyAPI_FUNC(int) PyUnicode_ToUpper(
PyAPI_FUNC(Py_ssize_t) PyUnicode_ToUpper(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res, /* Output buffer */
int size /* Buffer size */
Py_ssize_t size /* Buffer size */
);

PyAPI_FUNC(int) PyUnicode_ToTitle(
PyAPI_FUNC(Py_ssize_t) PyUnicode_ToTitle(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res, /* Output buffer */
int size /* Buffer size */
Py_ssize_t size /* Buffer size */
);

PyAPI_FUNC(int) PyUnicode_ToFolded(
PyAPI_FUNC(Py_ssize_t) PyUnicode_ToFolded(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res, /* Output buffer */
int size /* Buffer size */
Py_ssize_t size /* Buffer size */
);


Expand Down
4 changes: 2 additions & 2 deletions Modules/_testcapi/unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ unicode_copycharacters(PyObject *self, PyObject *args)
}

static PyObject *
unicode_case_operation(PyObject *str, int (*function)(Py_UCS4, Py_UCS4 *, int))
unicode_case_operation(PyObject *str, Py_ssize_t (*function)(Py_UCS4, Py_UCS4 *, Py_ssize_t))
{
if (!PyUnicode_Check(str)) {
PyErr_Format(PyExc_TypeError, "expect str type, got %T", str);
Expand All @@ -236,7 +236,7 @@ unicode_case_operation(PyObject *str, int (*function)(Py_UCS4, Py_UCS4 *, int))
Py_UCS4 c = PyUnicode_READ_CHAR(str, 0);

Py_UCS4 buf[3];
int chars = function(c, buf, Py_ARRAY_LENGTH(buf));
Py_ssize_t chars = function(c, buf, Py_ARRAY_LENGTH(buf));
if (chars < 0) {
return NULL;
}
Expand Down
62 changes: 31 additions & 31 deletions Objects/unicodectype.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,99 +198,99 @@ Py_UCS4 _PyUnicode_ToLowercase(Py_UCS4 ch)
return ch + ctype->lower;
}

int PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *res, int size)
Py_ssize_t PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *res, Py_ssize_t size)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

if (ctype->flags & EXTENDED_CASE_MASK) {
int index = ctype->lower & 0xFFFF;
int n = ctype->lower >> 24;
if (n > size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}

int i;
for (i = 0; i < n; i++) {
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
}
return n;
}

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

int PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *res, int size)
Py_ssize_t PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *res, Py_ssize_t size)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

if (ctype->flags & EXTENDED_CASE_MASK) {
int index = ctype->title & 0xFFFF;
int n = ctype->title >> 24;
if (n > size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}

int i;
for (i = 0; i < n; i++) {
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
}
return n;
}

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

int PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *res, int size)
Py_ssize_t PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *res, Py_ssize_t size)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

if (ctype->flags & EXTENDED_CASE_MASK) {
int index = ctype->upper & 0xFFFF;
int n = ctype->upper >> 24;
if (n > size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}

int i;
for (i = 0; i < n; i++) {
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
}
return n;
}

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

int PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *res, int size)
Py_ssize_t PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *res, Py_ssize_t size)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

if (ctype->flags & EXTENDED_CASE_MASK && (ctype->lower >> 20) & 7) {
int index = (ctype->lower & 0xFFFF) + (ctype->lower >> 24);
int n = (ctype->lower >> 20) & 7;
if (n > size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}

int i;
for (i = 0; i < n; i++) {
if (i >= size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
}
return n;
}

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