Skip to content

Commit f802c8b

Browse files
authored
gh-128013: Convert unicodeobject.c macros to functions (#128061)
Convert unicodeobject.c macros to static inline functions. * Add _PyUnicode_SET_UTF8() and _PyUnicode_SET_UTF8_LENGTH() macros. * Add PyUnicode_HASH() and PyUnicode_SET_HASH() macros. * Remove unused _PyUnicode_KIND() and _PyUnicode_GET_LENGTH() macros.
1 parent 91c5508 commit f802c8b

File tree

1 file changed

+78
-45
lines changed

1 file changed

+78
-45
lines changed

Objects/unicodeobject.c

+78-45
Original file line numberDiff line numberDiff line change
@@ -112,47 +112,80 @@ NOTE: In the interpreter's initialization phase, some globals are currently
112112
# define _PyUnicode_CHECK(op) PyUnicode_Check(op)
113113
#endif
114114

115-
#define _PyUnicode_UTF8(op) \
116-
(_PyCompactUnicodeObject_CAST(op)->utf8)
117-
#define PyUnicode_UTF8(op) \
118-
(assert(_PyUnicode_CHECK(op)), \
119-
PyUnicode_IS_COMPACT_ASCII(op) ? \
120-
((char*)(_PyASCIIObject_CAST(op) + 1)) : \
121-
_PyUnicode_UTF8(op))
122-
#define _PyUnicode_UTF8_LENGTH(op) \
123-
(_PyCompactUnicodeObject_CAST(op)->utf8_length)
124-
#define PyUnicode_UTF8_LENGTH(op) \
125-
(assert(_PyUnicode_CHECK(op)), \
126-
PyUnicode_IS_COMPACT_ASCII(op) ? \
127-
_PyASCIIObject_CAST(op)->length : \
128-
_PyUnicode_UTF8_LENGTH(op))
115+
static inline char* _PyUnicode_UTF8(PyObject *op)
116+
{
117+
return (_PyCompactUnicodeObject_CAST(op)->utf8);
118+
}
119+
120+
static inline char* PyUnicode_UTF8(PyObject *op)
121+
{
122+
assert(_PyUnicode_CHECK(op));
123+
if (PyUnicode_IS_COMPACT_ASCII(op)) {
124+
return ((char*)(_PyASCIIObject_CAST(op) + 1));
125+
}
126+
else {
127+
return _PyUnicode_UTF8(op);
128+
}
129+
}
130+
131+
static inline void PyUnicode_SET_UTF8(PyObject *op, char *utf8)
132+
{
133+
_PyCompactUnicodeObject_CAST(op)->utf8 = utf8;
134+
}
135+
136+
static inline Py_ssize_t PyUnicode_UTF8_LENGTH(PyObject *op)
137+
{
138+
assert(_PyUnicode_CHECK(op));
139+
if (PyUnicode_IS_COMPACT_ASCII(op)) {
140+
return _PyASCIIObject_CAST(op)->length;
141+
}
142+
else {
143+
return _PyCompactUnicodeObject_CAST(op)->utf8_length;
144+
}
145+
}
146+
147+
static inline void PyUnicode_SET_UTF8_LENGTH(PyObject *op, Py_ssize_t length)
148+
{
149+
_PyCompactUnicodeObject_CAST(op)->utf8_length = length;
150+
}
129151

130152
#define _PyUnicode_LENGTH(op) \
131153
(_PyASCIIObject_CAST(op)->length)
132154
#define _PyUnicode_STATE(op) \
133155
(_PyASCIIObject_CAST(op)->state)
134156
#define _PyUnicode_HASH(op) \
135157
(_PyASCIIObject_CAST(op)->hash)
136-
#define _PyUnicode_KIND(op) \
137-
(assert(_PyUnicode_CHECK(op)), \
138-
_PyASCIIObject_CAST(op)->state.kind)
139-
#define _PyUnicode_GET_LENGTH(op) \
140-
(assert(_PyUnicode_CHECK(op)), \
141-
_PyASCIIObject_CAST(op)->length)
158+
159+
static inline Py_hash_t PyUnicode_HASH(PyObject *op)
160+
{
161+
assert(_PyUnicode_CHECK(op));
162+
return FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyASCIIObject_CAST(op)->hash);
163+
}
164+
165+
static inline void PyUnicode_SET_HASH(PyObject *op, Py_hash_t hash)
166+
{
167+
FT_ATOMIC_STORE_SSIZE_RELAXED(_PyASCIIObject_CAST(op)->hash, hash);
168+
}
169+
142170
#define _PyUnicode_DATA_ANY(op) \
143171
(_PyUnicodeObject_CAST(op)->data.any)
144172

145-
#define _PyUnicode_SHARE_UTF8(op) \
146-
(assert(_PyUnicode_CHECK(op)), \
147-
assert(!PyUnicode_IS_COMPACT_ASCII(op)), \
148-
(_PyUnicode_UTF8(op) == PyUnicode_DATA(op)))
173+
static inline int _PyUnicode_SHARE_UTF8(PyObject *op)
174+
{
175+
assert(_PyUnicode_CHECK(op));
176+
assert(!PyUnicode_IS_COMPACT_ASCII(op));
177+
return (_PyUnicode_UTF8(op) == PyUnicode_DATA(op));
178+
}
149179

150180
/* true if the Unicode object has an allocated UTF-8 memory block
151181
(not shared with other data) */
152-
#define _PyUnicode_HAS_UTF8_MEMORY(op) \
153-
((!PyUnicode_IS_COMPACT_ASCII(op) \
154-
&& _PyUnicode_UTF8(op) \
155-
&& _PyUnicode_UTF8(op) != PyUnicode_DATA(op)))
182+
static inline int _PyUnicode_HAS_UTF8_MEMORY(PyObject *op)
183+
{
184+
return (!PyUnicode_IS_COMPACT_ASCII(op)
185+
&& _PyUnicode_UTF8(op) != NULL
186+
&& _PyUnicode_UTF8(op) != PyUnicode_DATA(op));
187+
}
188+
156189

157190
/* Generic helper macro to convert characters of different types.
158191
from_type and to_type have to be valid type names, begin and end
@@ -1123,8 +1156,8 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
11231156

11241157
if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) {
11251158
PyMem_Free(_PyUnicode_UTF8(unicode));
1126-
_PyUnicode_UTF8(unicode) = NULL;
1127-
_PyUnicode_UTF8_LENGTH(unicode) = 0;
1159+
PyUnicode_SET_UTF8(unicode, NULL);
1160+
PyUnicode_SET_UTF8_LENGTH(unicode, 0);
11281161
}
11291162
#ifdef Py_TRACE_REFS
11301163
_Py_ForgetReference(unicode);
@@ -1177,8 +1210,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
11771210
if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode))
11781211
{
11791212
PyMem_Free(_PyUnicode_UTF8(unicode));
1180-
_PyUnicode_UTF8(unicode) = NULL;
1181-
_PyUnicode_UTF8_LENGTH(unicode) = 0;
1213+
PyUnicode_SET_UTF8(unicode, NULL);
1214+
PyUnicode_SET_UTF8_LENGTH(unicode, 0);
11821215
}
11831216

11841217
data = (PyObject *)PyObject_Realloc(data, new_size);
@@ -1188,8 +1221,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
11881221
}
11891222
_PyUnicode_DATA_ANY(unicode) = data;
11901223
if (share_utf8) {
1191-
_PyUnicode_UTF8(unicode) = data;
1192-
_PyUnicode_UTF8_LENGTH(unicode) = length;
1224+
PyUnicode_SET_UTF8(unicode, data);
1225+
PyUnicode_SET_UTF8_LENGTH(unicode, length);
11931226
}
11941227
_PyUnicode_LENGTH(unicode) = length;
11951228
PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0);
@@ -1769,7 +1802,7 @@ unicode_modifiable(PyObject *unicode)
17691802
assert(_PyUnicode_CHECK(unicode));
17701803
if (Py_REFCNT(unicode) != 1)
17711804
return 0;
1772-
if (FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(unicode)) != -1)
1805+
if (PyUnicode_HASH(unicode) != -1)
17731806
return 0;
17741807
if (PyUnicode_CHECK_INTERNED(unicode))
17751808
return 0;
@@ -5862,8 +5895,8 @@ unicode_fill_utf8(PyObject *unicode)
58625895
PyErr_NoMemory();
58635896
return -1;
58645897
}
5865-
_PyUnicode_UTF8(unicode) = cache;
5866-
_PyUnicode_UTF8_LENGTH(unicode) = len;
5898+
PyUnicode_SET_UTF8(unicode, cache);
5899+
PyUnicode_SET_UTF8_LENGTH(unicode, len);
58675900
memcpy(cache, start, len);
58685901
cache[len] = '\0';
58695902
_PyBytesWriter_Dealloc(&writer);
@@ -11434,9 +11467,9 @@ _PyUnicode_EqualToASCIIId(PyObject *left, _Py_Identifier *right)
1143411467
return 0;
1143511468
}
1143611469

11437-
Py_hash_t right_hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(right_uni));
11470+
Py_hash_t right_hash = PyUnicode_HASH(right_uni);
1143811471
assert(right_hash != -1);
11439-
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(left));
11472+
Py_hash_t hash = PyUnicode_HASH(left);
1144011473
if (hash != -1 && hash != right_hash) {
1144111474
return 0;
1144211475
}
@@ -11916,14 +11949,14 @@ unicode_hash(PyObject *self)
1191611949
#ifdef Py_DEBUG
1191711950
assert(_Py_HashSecret_Initialized);
1191811951
#endif
11919-
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(self));
11952+
Py_hash_t hash = PyUnicode_HASH(self);
1192011953
if (hash != -1) {
1192111954
return hash;
1192211955
}
1192311956
x = Py_HashBuffer(PyUnicode_DATA(self),
1192411957
PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self));
1192511958

11926-
FT_ATOMIC_STORE_SSIZE_RELAXED(_PyUnicode_HASH(self), x);
11959+
PyUnicode_SET_HASH(self, x);
1192711960
return x;
1192811961
}
1192911962

@@ -15427,8 +15460,8 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
1542715460
_PyUnicode_STATE(self).compact = 0;
1542815461
_PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii;
1542915462
_PyUnicode_STATE(self).statically_allocated = 0;
15430-
_PyUnicode_UTF8_LENGTH(self) = 0;
15431-
_PyUnicode_UTF8(self) = NULL;
15463+
PyUnicode_SET_UTF8_LENGTH(self, 0);
15464+
PyUnicode_SET_UTF8(self, NULL);
1543215465
_PyUnicode_DATA_ANY(self) = NULL;
1543315466

1543415467
share_utf8 = 0;
@@ -15458,8 +15491,8 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
1545815491

1545915492
_PyUnicode_DATA_ANY(self) = data;
1546015493
if (share_utf8) {
15461-
_PyUnicode_UTF8_LENGTH(self) = length;
15462-
_PyUnicode_UTF8(self) = data;
15494+
PyUnicode_SET_UTF8_LENGTH(self, length);
15495+
PyUnicode_SET_UTF8(self, data);
1546315496
}
1546415497

1546515498
memcpy(data, PyUnicode_DATA(unicode), kind * (length + 1));

0 commit comments

Comments
 (0)