Skip to content

Commit 16c89f8

Browse files
vstinnerkumaraditya303
authored andcommitted
pythongh-128013: Convert unicodeobject.c macros to functions (python#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 261a604 commit 16c89f8

File tree

1 file changed

+78
-45
lines changed

1 file changed

+78
-45
lines changed

Objects/unicodeobject.c

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

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

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

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

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

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

11161149
if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) {
11171150
PyMem_Free(_PyUnicode_UTF8(unicode));
1118-
_PyUnicode_UTF8(unicode) = NULL;
1119-
_PyUnicode_UTF8_LENGTH(unicode) = 0;
1151+
PyUnicode_SET_UTF8(unicode, NULL);
1152+
PyUnicode_SET_UTF8_LENGTH(unicode, 0);
11201153
}
11211154
#ifdef Py_TRACE_REFS
11221155
_Py_ForgetReference(unicode);
@@ -1169,8 +1202,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
11691202
if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode))
11701203
{
11711204
PyMem_Free(_PyUnicode_UTF8(unicode));
1172-
_PyUnicode_UTF8(unicode) = NULL;
1173-
_PyUnicode_UTF8_LENGTH(unicode) = 0;
1205+
PyUnicode_SET_UTF8(unicode, NULL);
1206+
PyUnicode_SET_UTF8_LENGTH(unicode, 0);
11741207
}
11751208

11761209
data = (PyObject *)PyObject_Realloc(data, new_size);
@@ -1180,8 +1213,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
11801213
}
11811214
_PyUnicode_DATA_ANY(unicode) = data;
11821215
if (share_utf8) {
1183-
_PyUnicode_UTF8(unicode) = data;
1184-
_PyUnicode_UTF8_LENGTH(unicode) = length;
1216+
PyUnicode_SET_UTF8(unicode, data);
1217+
PyUnicode_SET_UTF8_LENGTH(unicode, length);
11851218
}
11861219
_PyUnicode_LENGTH(unicode) = length;
11871220
PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0);
@@ -1801,7 +1834,7 @@ unicode_modifiable(PyObject *unicode)
18011834
assert(_PyUnicode_CHECK(unicode));
18021835
if (Py_REFCNT(unicode) != 1)
18031836
return 0;
1804-
if (FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(unicode)) != -1)
1837+
if (PyUnicode_HASH(unicode) != -1)
18051838
return 0;
18061839
if (PyUnicode_CHECK_INTERNED(unicode))
18071840
return 0;
@@ -5442,8 +5475,8 @@ unicode_fill_utf8(PyObject *unicode)
54425475
PyErr_NoMemory();
54435476
return -1;
54445477
}
5445-
_PyUnicode_UTF8(unicode) = cache;
5446-
_PyUnicode_UTF8_LENGTH(unicode) = len;
5478+
PyUnicode_SET_UTF8(unicode, cache);
5479+
PyUnicode_SET_UTF8_LENGTH(unicode, len);
54475480
memcpy(cache, start, len);
54485481
cache[len] = '\0';
54495482
_PyBytesWriter_Dealloc(&writer);
@@ -10996,9 +11029,9 @@ _PyUnicode_EqualToASCIIId(PyObject *left, _Py_Identifier *right)
1099611029
return 0;
1099711030
}
1099811031

10999-
Py_hash_t right_hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(right_uni));
11032+
Py_hash_t right_hash = PyUnicode_HASH(right_uni);
1100011033
assert(right_hash != -1);
11001-
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(left));
11034+
Py_hash_t hash = PyUnicode_HASH(left);
1100211035
if (hash != -1 && hash != right_hash) {
1100311036
return 0;
1100411037
}
@@ -11484,14 +11517,14 @@ unicode_hash(PyObject *self)
1148411517
#ifdef Py_DEBUG
1148511518
assert(_Py_HashSecret_Initialized);
1148611519
#endif
11487-
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(self));
11520+
Py_hash_t hash = PyUnicode_HASH(self);
1148811521
if (hash != -1) {
1148911522
return hash;
1149011523
}
1149111524
x = _Py_HashBytes(PyUnicode_DATA(self),
1149211525
PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self));
1149311526

11494-
FT_ATOMIC_STORE_SSIZE_RELAXED(_PyUnicode_HASH(self), x);
11527+
PyUnicode_SET_HASH(self, x);
1149511528
return x;
1149611529
}
1149711530

@@ -14888,8 +14921,8 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
1488814921
_PyUnicode_STATE(self).compact = 0;
1488914922
_PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii;
1489014923
_PyUnicode_STATE(self).statically_allocated = 0;
14891-
_PyUnicode_UTF8_LENGTH(self) = 0;
14892-
_PyUnicode_UTF8(self) = NULL;
14924+
PyUnicode_SET_UTF8_LENGTH(self, 0);
14925+
PyUnicode_SET_UTF8(self, NULL);
1489314926
_PyUnicode_DATA_ANY(self) = NULL;
1489414927

1489514928
share_utf8 = 0;
@@ -14919,8 +14952,8 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
1491914952

1492014953
_PyUnicode_DATA_ANY(self) = data;
1492114954
if (share_utf8) {
14922-
_PyUnicode_UTF8_LENGTH(self) = length;
14923-
_PyUnicode_UTF8(self) = data;
14955+
PyUnicode_SET_UTF8_LENGTH(self, length);
14956+
PyUnicode_SET_UTF8(self, data);
1492414957
}
1492514958

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

0 commit comments

Comments
 (0)