2021-12-09 12:59:26 -07:00
|
|
|
#ifndef Py_INTERNAL_UNICODEOBJECT_H
|
|
|
|
|
#define Py_INTERNAL_UNICODEOBJECT_H
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef Py_BUILD_CORE
|
|
|
|
|
# error "this header requires Py_BUILD_CORE define"
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-01-11 11:56:16 +01:00
|
|
|
#include "pycore_fileutils.h" // _Py_error_handler
|
2022-12-07 15:56:31 -07:00
|
|
|
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
|
2022-01-11 11:56:16 +01:00
|
|
|
|
2022-04-19 14:02:19 -04:00
|
|
|
void _PyUnicode_ExactDealloc(PyObject *op);
|
2023-04-22 15:39:37 -04:00
|
|
|
Py_ssize_t _PyUnicode_InternedSize(void);
|
[3.12] gh-113993: Make interned strings mortal (GH-120520, GH-121364, GH-121903, GH-122303) (#123065)
This backports several PRs for gh-113993, making interned strings mortal so they can be garbage-collected when no longer needed.
* Allow interned strings to be mortal, and fix related issues (GH-120520)
* Add an InternalDocs file describing how interning should work and how to use it.
* Add internal functions to *explicitly* request what kind of interning is done:
- `_PyUnicode_InternMortal`
- `_PyUnicode_InternImmortal`
- `_PyUnicode_InternStatic`
* Switch uses of `PyUnicode_InternInPlace` to those.
* Disallow using `_Py_SetImmortal` on strings directly.
You should use `_PyUnicode_InternImmortal` instead:
- Strings should be interned before immortalization, otherwise you're possibly
interning a immortalizing copy.
- `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to
`SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in
backports, as they are now part of public API and version-specific ABI.
* Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery.
Make sure the statically allocated string singletons are unique. This means these sets are now disjoint:
- `_Py_ID`
- `_Py_STR` (including the empty string)
- one-character latin-1 singletons
Now, when you intern a singleton, that exact singleton will be interned.
* Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic).
* Intern `_Py_STR` singletons at startup.
* Beef up the tests. Cover internal details (marked with `@cpython_only`).
* Add lots of assertions
* Don't immortalize in PyUnicode_InternInPlace; keep immortalizing in other API (GH-121364)
* Switch PyUnicode_InternInPlace to _PyUnicode_InternMortal, clarify docs
* Document immortality in some functions that take `const char *`
This is PyUnicode_InternFromString;
PyDict_SetItemString, PyObject_SetAttrString;
PyObject_DelAttrString; PyUnicode_InternFromString;
and the PyModule_Add convenience functions.
Always point out a non-immortalizing alternative.
* Don't immortalize user-provided attr names in _ctypes
* Immortalize names in code objects to avoid crash (GH-121903)
* Intern latin-1 one-byte strings at startup (GH-122303)
There are some 3.12-specific changes, mainly to allow statically allocated strings in deepfreeze. (In 3.13, deepfreeze switched to the general `_Py_ID`/`_Py_STR`.)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
2024-09-27 22:28:48 +02:00
|
|
|
Py_ssize_t _PyUnicode_InternedSize_Immortal(void);
|
2021-12-09 12:59:26 -07:00
|
|
|
|
|
|
|
|
/* runtime lifecycle */
|
|
|
|
|
|
|
|
|
|
extern void _PyUnicode_InitState(PyInterpreterState *);
|
|
|
|
|
extern PyStatus _PyUnicode_InitGlobalObjects(PyInterpreterState *);
|
[3.12] gh-113993: Make interned strings mortal (GH-120520, GH-121364, GH-121903, GH-122303) (#123065)
This backports several PRs for gh-113993, making interned strings mortal so they can be garbage-collected when no longer needed.
* Allow interned strings to be mortal, and fix related issues (GH-120520)
* Add an InternalDocs file describing how interning should work and how to use it.
* Add internal functions to *explicitly* request what kind of interning is done:
- `_PyUnicode_InternMortal`
- `_PyUnicode_InternImmortal`
- `_PyUnicode_InternStatic`
* Switch uses of `PyUnicode_InternInPlace` to those.
* Disallow using `_Py_SetImmortal` on strings directly.
You should use `_PyUnicode_InternImmortal` instead:
- Strings should be interned before immortalization, otherwise you're possibly
interning a immortalizing copy.
- `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to
`SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in
backports, as they are now part of public API and version-specific ABI.
* Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery.
Make sure the statically allocated string singletons are unique. This means these sets are now disjoint:
- `_Py_ID`
- `_Py_STR` (including the empty string)
- one-character latin-1 singletons
Now, when you intern a singleton, that exact singleton will be interned.
* Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic).
* Intern `_Py_STR` singletons at startup.
* Beef up the tests. Cover internal details (marked with `@cpython_only`).
* Add lots of assertions
* Don't immortalize in PyUnicode_InternInPlace; keep immortalizing in other API (GH-121364)
* Switch PyUnicode_InternInPlace to _PyUnicode_InternMortal, clarify docs
* Document immortality in some functions that take `const char *`
This is PyUnicode_InternFromString;
PyDict_SetItemString, PyObject_SetAttrString;
PyObject_DelAttrString; PyUnicode_InternFromString;
and the PyModule_Add convenience functions.
Always point out a non-immortalizing alternative.
* Don't immortalize user-provided attr names in _ctypes
* Immortalize names in code objects to avoid crash (GH-121903)
* Intern latin-1 one-byte strings at startup (GH-122303)
There are some 3.12-specific changes, mainly to allow statically allocated strings in deepfreeze. (In 3.13, deepfreeze switched to the general `_Py_ID`/`_Py_STR`.)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
2024-09-27 22:28:48 +02:00
|
|
|
extern PyStatus _PyUnicode_InitInternDict(PyInterpreterState *);
|
2021-12-09 12:59:26 -07:00
|
|
|
extern PyStatus _PyUnicode_InitTypes(PyInterpreterState *);
|
|
|
|
|
extern void _PyUnicode_Fini(PyInterpreterState *);
|
2022-01-22 22:55:39 +01:00
|
|
|
extern void _PyUnicode_FiniTypes(PyInterpreterState *);
|
2021-12-09 12:59:26 -07:00
|
|
|
|
2022-04-18 19:48:27 +05:30
|
|
|
extern PyTypeObject _PyUnicodeASCIIIter_Type;
|
2021-12-09 12:59:26 -07:00
|
|
|
|
[3.12] gh-113993: Make interned strings mortal (GH-120520, GH-121364, GH-121903, GH-122303) (#123065)
This backports several PRs for gh-113993, making interned strings mortal so they can be garbage-collected when no longer needed.
* Allow interned strings to be mortal, and fix related issues (GH-120520)
* Add an InternalDocs file describing how interning should work and how to use it.
* Add internal functions to *explicitly* request what kind of interning is done:
- `_PyUnicode_InternMortal`
- `_PyUnicode_InternImmortal`
- `_PyUnicode_InternStatic`
* Switch uses of `PyUnicode_InternInPlace` to those.
* Disallow using `_Py_SetImmortal` on strings directly.
You should use `_PyUnicode_InternImmortal` instead:
- Strings should be interned before immortalization, otherwise you're possibly
interning a immortalizing copy.
- `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to
`SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in
backports, as they are now part of public API and version-specific ABI.
* Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery.
Make sure the statically allocated string singletons are unique. This means these sets are now disjoint:
- `_Py_ID`
- `_Py_STR` (including the empty string)
- one-character latin-1 singletons
Now, when you intern a singleton, that exact singleton will be interned.
* Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic).
* Intern `_Py_STR` singletons at startup.
* Beef up the tests. Cover internal details (marked with `@cpython_only`).
* Add lots of assertions
* Don't immortalize in PyUnicode_InternInPlace; keep immortalizing in other API (GH-121364)
* Switch PyUnicode_InternInPlace to _PyUnicode_InternMortal, clarify docs
* Document immortality in some functions that take `const char *`
This is PyUnicode_InternFromString;
PyDict_SetItemString, PyObject_SetAttrString;
PyObject_DelAttrString; PyUnicode_InternFromString;
and the PyModule_Add convenience functions.
Always point out a non-immortalizing alternative.
* Don't immortalize user-provided attr names in _ctypes
* Immortalize names in code objects to avoid crash (GH-121903)
* Intern latin-1 one-byte strings at startup (GH-122303)
There are some 3.12-specific changes, mainly to allow statically allocated strings in deepfreeze. (In 3.13, deepfreeze switched to the general `_Py_ID`/`_Py_STR`.)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
2024-09-27 22:28:48 +02:00
|
|
|
/* Interning */
|
|
|
|
|
|
|
|
|
|
// All these are "ref-neutral", like the public PyUnicode_InternInPlace.
|
|
|
|
|
|
|
|
|
|
// Explicit interning routines:
|
|
|
|
|
PyAPI_FUNC(void) _PyUnicode_InternMortal(PyInterpreterState *interp, PyObject **);
|
|
|
|
|
PyAPI_FUNC(void) _PyUnicode_InternImmortal(PyInterpreterState *interp, PyObject **);
|
|
|
|
|
// Left here to help backporting:
|
|
|
|
|
PyAPI_FUNC(void) _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p);
|
|
|
|
|
// Only for statically allocated strings:
|
|
|
|
|
extern void _PyUnicode_InternStatic(PyInterpreterState *interp, PyObject **);
|
|
|
|
|
|
2021-12-09 12:59:26 -07:00
|
|
|
/* other API */
|
|
|
|
|
|
|
|
|
|
struct _Py_unicode_runtime_ids {
|
|
|
|
|
PyThread_type_lock lock;
|
|
|
|
|
// next_index value must be preserved when Py_Initialize()/Py_Finalize()
|
|
|
|
|
// is called multiple times: see _PyUnicode_FromId() implementation.
|
|
|
|
|
Py_ssize_t next_index;
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-16 09:37:14 -07:00
|
|
|
struct _Py_unicode_runtime_state {
|
|
|
|
|
struct _Py_unicode_runtime_ids ids;
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-09 12:59:26 -07:00
|
|
|
/* fs_codec.encoding is initialized to NULL.
|
|
|
|
|
Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */
|
|
|
|
|
struct _Py_unicode_fs_codec {
|
|
|
|
|
char *encoding; // Filesystem encoding (encoded to UTF-8)
|
|
|
|
|
int utf8; // encoding=="utf-8"?
|
|
|
|
|
char *errors; // Filesystem errors (encoded to UTF-8)
|
|
|
|
|
_Py_error_handler error_handler;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _Py_unicode_ids {
|
|
|
|
|
Py_ssize_t size;
|
|
|
|
|
PyObject **array;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _Py_unicode_state {
|
|
|
|
|
struct _Py_unicode_fs_codec fs_codec;
|
|
|
|
|
|
2022-12-07 15:56:31 -07:00
|
|
|
_PyUnicode_Name_CAPI *ucnhash_capi;
|
|
|
|
|
|
2021-12-09 12:59:26 -07:00
|
|
|
// Unicode identifiers (_Py_Identifier): see _PyUnicode_FromId()
|
|
|
|
|
struct _Py_unicode_ids ids;
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-06 08:53:44 +01:00
|
|
|
extern void _PyUnicode_ClearInterned(PyInterpreterState *interp);
|
2021-12-09 12:59:26 -07:00
|
|
|
|
2024-11-29 17:03:24 +01:00
|
|
|
// Like PyUnicode_AsUTF8(), but check for embedded null characters.
|
|
|
|
|
extern const char* _PyUnicode_AsUTF8NoNUL(PyObject *);
|
|
|
|
|
|
2021-12-09 12:59:26 -07:00
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif /* !Py_INTERNAL_UNICODEOBJECT_H */
|