mirror of
https://github.com/python/cpython.git
synced 2025-07-16 07:45:20 +00:00
Add API for static strings, primarily good for identifiers.
Thanks to Konrad Schöbel and Jasper Schulz for helping with the mass-editing.
This commit is contained in:
parent
67df285a33
commit
afe55bba33
50 changed files with 578 additions and 240 deletions
|
@ -201,6 +201,9 @@ static PyObject *interned;
|
|||
/* The empty Unicode object is shared to improve performance. */
|
||||
static PyObject *unicode_empty;
|
||||
|
||||
/* List of static strings. */
|
||||
static _Py_Identifier *static_strings;
|
||||
|
||||
/* Single character Unicode strings in the Latin-1 range are being
|
||||
shared as well. */
|
||||
static PyObject *unicode_latin1[256];
|
||||
|
@ -1609,6 +1612,33 @@ PyUnicode_FromString(const char *u)
|
|||
return PyUnicode_FromStringAndSize(u, size);
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyUnicode_FromId(_Py_Identifier *id)
|
||||
{
|
||||
if (!id->object) {
|
||||
id->object = PyUnicode_FromString(id->string);
|
||||
if (!id->object)
|
||||
return NULL;
|
||||
PyUnicode_InternInPlace(&id->object);
|
||||
assert(!id->next);
|
||||
id->next = static_strings;
|
||||
static_strings = id;
|
||||
}
|
||||
Py_INCREF(id->object);
|
||||
return id->object;
|
||||
}
|
||||
|
||||
void
|
||||
_PyUnicode_ClearStaticStrings()
|
||||
{
|
||||
_Py_Identifier *i;
|
||||
for (i = static_strings; i; i = i->next) {
|
||||
Py_DECREF(i->object);
|
||||
i->object = NULL;
|
||||
i->next = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
unicode_fromascii(const unsigned char* s, Py_ssize_t size)
|
||||
{
|
||||
|
@ -13523,6 +13553,7 @@ _PyUnicode_Fini(void)
|
|||
unicode_latin1[i] = NULL;
|
||||
}
|
||||
}
|
||||
_PyUnicode_ClearStaticStrings();
|
||||
(void)PyUnicode_ClearFreeList();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue