Skip to content

bpo-45490: Convert unicodeobject.h macros to static inline functions #31221

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
419 changes: 256 additions & 163 deletions Include/cpython/unicodeobject.h

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Include/internal/pycore_strhex.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ PyAPI_FUNC(PyObject*) _Py_strhex_bytes(
PyAPI_FUNC(PyObject*) _Py_strhex_with_sep(
const char* argbuf,
const Py_ssize_t arglen,
const PyObject* sep,
PyObject* sep,
const int bytes_per_group);
PyAPI_FUNC(PyObject*) _Py_strhex_bytes_with_sep(
const char* argbuf,
const Py_ssize_t arglen,
const PyObject* sep,
PyObject* sep,
const int bytes_per_group);

#ifdef __cplusplus
Expand Down
6 changes: 3 additions & 3 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ is_space(enum PyUnicode_Kind kind, const void *data, Py_ssize_t pos)
Return NULL if malloc fails and an empty string if invalid characters
are found. */
static char *
numeric_as_ascii(const PyObject *u, int strip_ws, int ignore_underscores)
Copy link
Contributor

@erlend-aasland erlend-aasland Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to remove const? Ditto for the rest of the PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the "u" string is not ready, PyUnicode_READY() will modify it. It's not a read-only operation.

In Python 3.12, PyUnicode_WCHAR_KIND will be removed: https://www.python.org/dev/peps/pep-0623/

In the meanwhile, I prefer to not stop lying: we do modify the object :-)

numeric_as_ascii(PyObject *u, int strip_ws, int ignore_underscores)
{
enum PyUnicode_Kind kind;
const void *data;
Expand Down Expand Up @@ -2009,7 +2009,7 @@ PyDecType_FromCStringExact(PyTypeObject *type, const char *s,

/* Return a new PyDecObject or a subtype from a PyUnicodeObject. */
static PyObject *
PyDecType_FromUnicode(PyTypeObject *type, const PyObject *u,
PyDecType_FromUnicode(PyTypeObject *type, PyObject *u,
PyObject *context)
{
PyObject *dec;
Expand All @@ -2029,7 +2029,7 @@ PyDecType_FromUnicode(PyTypeObject *type, const PyObject *u,
* conversion. If the conversion is not exact, fail with InvalidOperation.
* Allow leading and trailing whitespace in the input operand. */
static PyObject *
PyDecType_FromUnicodeExactWS(PyTypeObject *type, const PyObject *u,
PyDecType_FromUnicodeExactWS(PyTypeObject *type, PyObject *u,
PyObject *context)
{
PyObject *dec;
Expand Down
2 changes: 1 addition & 1 deletion Modules/unicodedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ nfd_nfkd(PyObject *self, PyObject *input, int k)
Py_UCS4 *output;
Py_ssize_t i, o, osize;
int kind;
const void *data;
void *data;
/* Longest decomposition in Unicode 3.2: U+FDFA */
Py_UCS4 stack[20];
Py_ssize_t space, isize;
Expand Down
13 changes: 5 additions & 8 deletions Objects/stringlib/eq.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
* unicode_eq() is called when the hash of two unicode objects is equal.
*/
Py_LOCAL_INLINE(int)
unicode_eq(PyObject *aa, PyObject *bb)
unicode_eq(PyObject *a, PyObject *b)
{
assert(PyUnicode_Check(aa));
assert(PyUnicode_Check(bb));
assert(PyUnicode_IS_READY(aa));
assert(PyUnicode_IS_READY(bb));

PyUnicodeObject *a = (PyUnicodeObject *)aa;
PyUnicodeObject *b = (PyUnicodeObject *)bb;
assert(PyUnicode_Check(a));
assert(PyUnicode_Check(b));
assert(PyUnicode_IS_READY(a));
assert(PyUnicode_IS_READY(b));

if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
return 0;
Expand Down
4 changes: 2 additions & 2 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ _PyUnicode_CheckConsistency(PyObject *op, int check_content)
const void *data;
Py_UCS4 ch;

data = PyUnicode_DATA(ascii);
data = PyUnicode_DATA(op);
for (i=0; i < ascii->length; i++)
{
ch = PyUnicode_READ(kind, data, i);
Expand Down Expand Up @@ -13649,7 +13649,7 @@ unicode_zfill_impl(PyObject *self, Py_ssize_t width)
Py_ssize_t fill;
PyObject *u;
int kind;
const void *data;
void *data;
Py_UCS4 chr;

if (PyUnicode_READY(self) == -1)
Expand Down
2 changes: 1 addition & 1 deletion Python/formatter_unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec,
/* Used to keep track of digits, decimal, and remainder. */
Py_ssize_t d_pos = d_start;
const unsigned int kind = writer->kind;
const void *data = writer->data;
void *data = writer->data;
Py_ssize_t r;

if (spec->n_lpadding) {
Expand Down
6 changes: 3 additions & 3 deletions Python/pystrhex.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdlib.h> // abs()

static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
const PyObject* sep, int bytes_per_sep_group,
PyObject* sep, int bytes_per_sep_group,
const int return_bytes)
{
assert(arglen >= 0);
Expand Down Expand Up @@ -159,14 +159,14 @@ PyObject * _Py_strhex_bytes(const char* argbuf, const Py_ssize_t arglen)

/* These variants include support for a separator between every N bytes: */

PyObject * _Py_strhex_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group)
PyObject* _Py_strhex_with_sep(const char* argbuf, const Py_ssize_t arglen, PyObject* sep, const int bytes_per_group)
{
return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 0);
}

/* Same as above but returns a bytes() instead of str() to avoid the
* need to decode the str() when bytes are needed. */
PyObject * _Py_strhex_bytes_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group)
PyObject* _Py_strhex_bytes_with_sep(const char* argbuf, const Py_ssize_t arglen, PyObject* sep, const int bytes_per_group)
{
return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 1);
}