aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Modules/pyexpat.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.com>2024-02-12 07:53:52 +0300
committerDaniil Cherednik <dcherednik@ydb.tech>2024-02-14 14:26:16 +0000
commit31f2a419764a8ba77c2a970cfc80056c6cd06756 (patch)
treec1995d239eba8571cefc640f6648e1d5dd4ce9e2 /contrib/tools/python3/src/Modules/pyexpat.c
parentfe2ef02b38d9c85d80060963b265a1df9f38c3bb (diff)
downloadydb-31f2a419764a8ba77c2a970cfc80056c6cd06756.tar.gz
Update Python from 3.11.8 to 3.12.2
Diffstat (limited to 'contrib/tools/python3/src/Modules/pyexpat.c')
-rw-r--r--contrib/tools/python3/src/Modules/pyexpat.c128
1 files changed, 78 insertions, 50 deletions
diff --git a/contrib/tools/python3/src/Modules/pyexpat.c b/contrib/tools/python3/src/Modules/pyexpat.c
index 226887cfac..b21360419d 100644
--- a/contrib/tools/python3/src/Modules/pyexpat.c
+++ b/contrib/tools/python3/src/Modules/pyexpat.c
@@ -710,7 +710,7 @@ pyexpat.xmlparser.Parse
cls: defining_class
data: object
- isfinal: bool(accept={int}) = False
+ isfinal: bool = False
/
Parse XML data.
@@ -721,7 +721,7 @@ Parse XML data.
static PyObject *
pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyTypeObject *cls,
PyObject *data, int isfinal)
-/*[clinic end generated code: output=8faffe07fe1f862a input=fc97f833558ca715]*/
+/*[clinic end generated code: output=8faffe07fe1f862a input=d0eb2a69fab3b9f1]*/
{
const char *s;
Py_ssize_t slen;
@@ -959,8 +959,7 @@ pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self,
new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
encoding);
new_parser->handlers = 0;
- new_parser->intern = self->intern;
- Py_XINCREF(new_parser->intern);
+ new_parser->intern = Py_XNewRef(self->intern);
if (self->buffer != NULL) {
new_parser->buffer = PyMem_Malloc(new_parser->buffer_size);
@@ -991,8 +990,7 @@ pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self,
for (i = 0; handler_info[i].name != NULL; i++) {
PyObject *handler = self->handlers[i];
if (handler != NULL) {
- Py_INCREF(handler);
- new_parser->handlers[i] = handler;
+ new_parser->handlers[i] = Py_NewRef(handler);
handler_info[i].setter(new_parser->itself,
handler_info[i].handler);
}
@@ -1078,26 +1076,39 @@ static struct PyMethodDef xmlparse_methods[] = {
Make it as simple as possible.
*/
+static const unsigned char template_buffer[256] =
+ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
+ 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
+ 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
+ 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
+ 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
+ 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
+ 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137,
+ 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
+ 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
+ 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
+ 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
+ 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
+ 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
+ 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242,
+ 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255};
+
+
static int
PyUnknownEncodingHandler(void *encodingHandlerData,
const XML_Char *name,
XML_Encoding *info)
{
- static unsigned char template_buffer[256] = {0};
- PyObject* u;
+ PyObject *u;
int i;
const void *data;
- unsigned int kind;
+ int kind;
if (PyErr_Occurred())
return XML_STATUS_ERROR;
- if (template_buffer[1] == 0) {
- for (i = 0; i < 256; i++)
- template_buffer[i] = i;
- }
-
- u = PyUnicode_Decode((char*) template_buffer, 256, name, "replace");
+ u = PyUnicode_Decode((const char*) template_buffer, 256, name, "replace");
if (u == NULL || PyUnicode_READY(u)) {
Py_XDECREF(u);
return XML_STATUS_ERROR;
@@ -1148,8 +1159,7 @@ newxmlparseobject(pyexpat_state *state, const char *encoding,
self->in_callback = 0;
self->ns_prefixes = 0;
self->handlers = NULL;
- self->intern = intern;
- Py_XINCREF(self->intern);
+ self->intern = Py_XNewRef(intern);
/* namespace_separator is either NULL or contains one char + \0 */
self->itself = XML_ParserCreate_MM(encoding, &ExpatMemoryHandler,
@@ -1232,8 +1242,7 @@ xmlparse_handler_getter(xmlparseobject *self, struct HandlerInfo *hi)
PyObject *result = self->handlers[handlernum];
if (result == NULL)
result = Py_None;
- Py_INCREF(result);
- return result;
+ return Py_NewRef(result);
}
static int
@@ -1365,9 +1374,7 @@ xmlparse_buffer_size_setter(xmlparseobject *self, PyObject *v, void *closure)
/* check maximum */
if (new_buffer_size > INT_MAX) {
- char errmsg[100];
- sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
- PyErr_SetString(PyExc_ValueError, errmsg);
+ PyErr_Format(PyExc_ValueError, "buffer_size must not be greater than %i", INT_MAX);
return -1;
}
@@ -1725,7 +1732,7 @@ add_error(PyObject *errors_module, PyObject *codes_dict,
const int error_code = (int)error_index;
/* NOTE: This keeps the source of truth regarding error
- * messages with libexpat and (by definiton) in bulletproof sync
+ * messages with libexpat and (by definition) in bulletproof sync
* with the other uses of the XML_ErrorString function
* elsewhere within this file. pyexpat's copy of the messages
* only acts as a fallback in case of outdated runtime libexpat,
@@ -1888,6 +1895,18 @@ error:
}
#endif
+static void
+pyexpat_capsule_destructor(PyObject *capsule)
+{
+ void *p = PyCapsule_GetPointer(capsule, PyExpat_CAPSULE_NAME);
+ if (p == NULL) {
+ PyErr_WriteUnraisable(capsule);
+ return;
+ }
+ PyMem_Free(p);
+}
+
+
static int
pyexpat_exec(PyObject *mod)
{
@@ -1975,40 +1994,46 @@ pyexpat_exec(PyObject *mod)
MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
#undef MYCONST
- static struct PyExpat_CAPI capi;
+ struct PyExpat_CAPI *capi = PyMem_Malloc(sizeof(*capi));
+ if (capi == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
/* initialize pyexpat dispatch table */
- capi.size = sizeof(capi);
- capi.magic = PyExpat_CAPI_MAGIC;
- capi.MAJOR_VERSION = XML_MAJOR_VERSION;
- capi.MINOR_VERSION = XML_MINOR_VERSION;
- capi.MICRO_VERSION = XML_MICRO_VERSION;
- capi.ErrorString = XML_ErrorString;
- capi.GetErrorCode = XML_GetErrorCode;
- capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
- capi.GetErrorLineNumber = XML_GetErrorLineNumber;
- capi.Parse = XML_Parse;
- capi.ParserCreate_MM = XML_ParserCreate_MM;
- capi.ParserFree = XML_ParserFree;
- capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
- capi.SetCommentHandler = XML_SetCommentHandler;
- capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
- capi.SetElementHandler = XML_SetElementHandler;
- capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
- capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
- capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
- capi.SetUserData = XML_SetUserData;
- capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
- capi.SetEncoding = XML_SetEncoding;
- capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
+ capi->size = sizeof(*capi);
+ capi->magic = PyExpat_CAPI_MAGIC;
+ capi->MAJOR_VERSION = XML_MAJOR_VERSION;
+ capi->MINOR_VERSION = XML_MINOR_VERSION;
+ capi->MICRO_VERSION = XML_MICRO_VERSION;
+ capi->ErrorString = XML_ErrorString;
+ capi->GetErrorCode = XML_GetErrorCode;
+ capi->GetErrorColumnNumber = XML_GetErrorColumnNumber;
+ capi->GetErrorLineNumber = XML_GetErrorLineNumber;
+ capi->Parse = XML_Parse;
+ capi->ParserCreate_MM = XML_ParserCreate_MM;
+ capi->ParserFree = XML_ParserFree;
+ capi->SetCharacterDataHandler = XML_SetCharacterDataHandler;
+ capi->SetCommentHandler = XML_SetCommentHandler;
+ capi->SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
+ capi->SetElementHandler = XML_SetElementHandler;
+ capi->SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
+ capi->SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
+ capi->SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
+ capi->SetUserData = XML_SetUserData;
+ capi->SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
+ capi->SetEncoding = XML_SetEncoding;
+ capi->DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
#if XML_COMBINED_VERSION >= 20100
- capi.SetHashSalt = XML_SetHashSalt;
+ capi->SetHashSalt = XML_SetHashSalt;
#else
- capi.SetHashSalt = NULL;
+ capi->SetHashSalt = NULL;
#endif
/* export using capsule */
- PyObject *capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
+ PyObject *capi_object = PyCapsule_New(capi, PyExpat_CAPSULE_NAME,
+ pyexpat_capsule_destructor);
if (capi_object == NULL) {
+ PyMem_Free(capi);
return -1;
}
@@ -2048,6 +2073,9 @@ pyexpat_free(void *module)
static PyModuleDef_Slot pyexpat_slots[] = {
{Py_mod_exec, pyexpat_exec},
+ // XXX gh-103092: fix isolation.
+ {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
+ //{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{0, NULL}
};