-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
base: main
Are you sure you want to change the base?
Changes from all commits
431abba
d604fc8
fbbf841
f17aa0c
4a70489
61afd9a
7885b17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Make :c:func:`PyUnicode_ToLower`, :c:func:`PyUnicode_ToUpper`, :c:func:`PyUnicode_ToTitle` and :c:func:`PyUnicode_ToFolded` public. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -220,6 +220,64 @@ unicode_copycharacters(PyObject *self, PyObject *args) | |||||
return Py_BuildValue("(Nn)", to_copy, copied); | ||||||
} | ||||||
|
||||||
static PyObject * | ||||||
unicode_case_operation(PyObject *str, int (*function)(Py_UCS4, Py_UCS4 *, int), const char *name) | ||||||
{ | ||||||
lysnikolaou marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if (PyUnicode_GET_LENGTH(str) != 1) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check str type? Something like: if (!PyUnicode_Check(str)) {
PyErr_Format(PyExc_TypeError, "expect str type, got %T", str);
return NULL;
} |
||||||
PyErr_Format(PyExc_ValueError, "%s only accepts 1-character strings", name); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may remove the function name to simplify the code, it's just testing code. |
||||||
return NULL; | ||||||
} | ||||||
|
||||||
Py_UCS4 c = PyUnicode_READ_CHAR(str, 0); | ||||||
|
||||||
Py_UCS4 buf[3]; | ||||||
int chars = function(c, buf, Py_ARRAY_LENGTH(buf)); | ||||||
if (chars <= 0) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0 is also an unexpected result here that shouldn't be happening. |
||||||
PyErr_BadInternalCall(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function is supposed to raise an exception in this case. You can remove this call. |
||||||
return NULL; | ||||||
} | ||||||
|
||||||
PyUnicodeWriter *writer = PyUnicodeWriter_Create(1); | ||||||
if (writer == NULL) { | ||||||
return NULL; | ||||||
} | ||||||
if (PyUnicodeWriter_WriteUCS4(writer, buf, chars) < 0) { | ||||||
PyUnicodeWriter_Discard(writer); | ||||||
return NULL; | ||||||
} | ||||||
return PyUnicodeWriter_Finish(writer); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use |
||||||
} | ||||||
|
||||||
/* Test PyUnicode_ToLower() */ | ||||||
static PyObject * | ||||||
unicode_tolower(PyObject *self, PyObject *arg) | ||||||
{ | ||||||
return unicode_case_operation(arg, PyUnicode_ToLower, "unicode_tolower"); | ||||||
} | ||||||
|
||||||
/* Test PyUnicode_ToUpper() */ | ||||||
static PyObject * | ||||||
unicode_toupper(PyObject *self, PyObject *arg) | ||||||
{ | ||||||
return unicode_case_operation(arg, PyUnicode_ToUpper, "unicode_toupper"); | ||||||
} | ||||||
|
||||||
|
||||||
/* Test PyUnicode_ToLower() */ | ||||||
static PyObject * | ||||||
unicode_totitle(PyObject *self, PyObject *arg) | ||||||
{ | ||||||
return unicode_case_operation(arg, PyUnicode_ToTitle, "unicode_totitle"); | ||||||
} | ||||||
|
||||||
/* Test PyUnicode_ToLower() */ | ||||||
static PyObject * | ||||||
unicode_tofolded(PyObject *self, PyObject *arg) | ||||||
{ | ||||||
return unicode_case_operation(arg, PyUnicode_ToFolded, "unicode_tofolded"); | ||||||
} | ||||||
|
||||||
|
||||||
static PyObject* | ||||||
unicode_GET_CACHED_HASH(PyObject *self, PyObject *arg) | ||||||
{ | ||||||
|
@@ -577,6 +635,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}, | ||||||
}; | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it an important usecase for you to be able to pass NULL buffer to get the buffer size? Removing this feature would make the implementation simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's not an important usecase. I implemented because it was suggested on the issue and it also seems like standard practice for some C functions that accept buffers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think I should remove it?