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 12 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
49 changes: 49 additions & 0 deletions Lib/test/test_capi/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,55 @@ def test_GET_CACHED_HASH(self):
# impl detail: ASCII string hashes are equal to bytes ones
self.assertEqual(unicode_GET_CACHED_HASH(obj), hash(content_bytes))

@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_tolower(self):
import string
from _testcapi import unicode_tolower

for i, c in enumerate(string.ascii_uppercase):
with self.subTest(c):
self.assertEqual(unicode_tolower(c), string.ascii_lowercase[i])

# Test unicode character
self.assertEqual(unicode_tolower("Č"), "č")

@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_toupper(self):
import string
from _testcapi import unicode_toupper

for i, c in enumerate(string.ascii_lowercase):
with self.subTest(c):
self.assertEqual(unicode_toupper(c), string.ascii_uppercase[i])

# Test unicode character
self.assertEqual(unicode_toupper("č"), "Č")

@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_totitle(self):
from _testcapi import unicode_totitle

self.assertEqual(unicode_totitle("t"), "T")

# Test unicode character
self.assertEqual(unicode_totitle("ł"), "Ł")

@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_tofolded(self):
from _testcapi import unicode_tofolded

self.assertEqual(unicode_tofolded("T"), "t")

# Test unicode character
self.assertEqual(unicode_tofolded("Ł"), "ł")

# Test case-ignorable character
self.assertEqual(unicode_tofolded("👍"), "👍")


class PyUnicodeWriterTest(unittest.TestCase):
def create_writer(self, size):
Expand Down
110 changes: 110 additions & 0 deletions Modules/_testcapi/unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,112 @@ unicode_copycharacters(PyObject *self, PyObject *args)
return Py_BuildValue("(Nn)", to_copy, copied);
}

/* Test PyUnicode_ToLower() */
static PyObject *
unicode_tolower(PyObject *self, PyObject *arg)
{
if (PyUnicode_GET_LENGTH(arg) != 1) {
PyErr_SetString(PyExc_ValueError, "unicode_tolower only accepts 1-character strings");
return NULL;
}

Py_UCS4 c = PyUnicode_READ_CHAR(arg, 0);

Py_UCS4 lower[3];
int chars = PyUnicode_ToLower(c, lower, Py_ARRAY_LENGTH(lower));
assert(chars >= 1);

PyUnicodeWriter *writer = PyUnicodeWriter_Create(1);
if (writer == NULL) {
return NULL;
}
if (PyUnicodeWriter_WriteUCS4(writer, lower, chars) < 0) {
PyUnicodeWriter_Discard(writer);
return NULL;
}
return PyUnicodeWriter_Finish(writer);
}

/* Test PyUnicode_ToUpper() */
static PyObject *
unicode_toupper(PyObject *self, PyObject *arg)
{
if (PyUnicode_GET_LENGTH(arg) != 1) {
PyErr_SetString(PyExc_ValueError, "unicode_toupper only accepts 1-character strings");
return NULL;
}

Py_UCS4 c = PyUnicode_READ_CHAR(arg, 0);

Py_UCS4 upper[3];
int chars = PyUnicode_ToUpper(c, upper, Py_ARRAY_LENGTH(upper));
assert(chars >= 1);

PyUnicodeWriter *writer = PyUnicodeWriter_Create(1);
if (writer == NULL) {
return NULL;
}
if (PyUnicodeWriter_WriteUCS4(writer, upper, chars) < 0) {
PyUnicodeWriter_Discard(writer);
return NULL;
}
return PyUnicodeWriter_Finish(writer);
}


/* Test PyUnicode_ToLower() */
static PyObject *
unicode_totitle(PyObject *self, PyObject *arg)
{
if (PyUnicode_GET_LENGTH(arg) != 1) {
PyErr_SetString(PyExc_ValueError, "unicode_totitle only accepts 1-character strings");
return NULL;
}

Py_UCS4 c = PyUnicode_READ_CHAR(arg, 0);

Py_UCS4 title[3];
int chars = PyUnicode_ToTitle(c, title, Py_ARRAY_LENGTH(title));
assert(chars >= 1);

PyUnicodeWriter *writer = PyUnicodeWriter_Create(1);
if (writer == NULL) {
return NULL;
}
if (PyUnicodeWriter_WriteUCS4(writer, title, chars) < 0) {
PyUnicodeWriter_Discard(writer);
return NULL;
}
return PyUnicodeWriter_Finish(writer);
}

/* Test PyUnicode_ToLower() */
static PyObject *
unicode_tofolded(PyObject *self, PyObject *arg)
{
if (PyUnicode_GET_LENGTH(arg) != 1) {
PyErr_SetString(PyExc_ValueError, "unicode_tofolded only accepts 1-character strings");
return NULL;
}

Py_UCS4 c = PyUnicode_READ_CHAR(arg, 0);

Py_UCS4 folded[3];
int chars = PyUnicode_ToFolded(c, folded, Py_ARRAY_LENGTH(folded));
assert(chars >= 1);

PyUnicodeWriter *writer = PyUnicodeWriter_Create(1);
if (writer == NULL) {
return NULL;
}
if (PyUnicodeWriter_WriteUCS4(writer, folded, chars) < 0) {
PyUnicodeWriter_Discard(writer);
return NULL;
}
return PyUnicodeWriter_Finish(writer);
}


static PyObject*
unicode_GET_CACHED_HASH(PyObject *self, PyObject *arg)
{
Expand Down Expand Up @@ -577,6 +683,10 @@ static PyMethodDef TestMethods[] = {
{"unicode_asutf8", unicode_asutf8, METH_VARARGS},
{"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},
{"unicode_GET_CACHED_HASH", unicode_GET_CACHED_HASH, METH_O},
{"unicode_tolower", unicode_tolower, METH_O},
{"unicode_toupper", unicode_toupper, METH_O},
{"unicode_totitle", unicode_totitle, METH_O},
{"unicode_tofolded", unicode_tofolded, METH_O},
{NULL},
};

Expand Down
Loading