@@ -1878,6 +1878,18 @@ add_features(PyObject *mod)
18781878}
18791879#endif
18801880
1881+ static void
1882+ pyexpat_capsule_destructor (PyObject * capsule )
1883+ {
1884+ void * p = PyCapsule_GetPointer (capsule , PyExpat_CAPSULE_NAME );
1885+ if (p == NULL ) {
1886+ PyErr_WriteUnraisable (capsule );
1887+ return ;
1888+ }
1889+ PyMem_Free (p );
1890+ }
1891+
1892+
18811893static int
18821894pyexpat_exec (PyObject * mod )
18831895{
@@ -1965,40 +1977,46 @@ pyexpat_exec(PyObject *mod)
19651977 MYCONST (XML_PARAM_ENTITY_PARSING_ALWAYS );
19661978#undef MYCONST
19671979
1968- static struct PyExpat_CAPI capi ;
1980+ struct PyExpat_CAPI * capi = PyMem_Malloc (sizeof (* capi ));
1981+ if (capi == NULL ) {
1982+ PyErr_NoMemory ();
1983+ return -1 ;
1984+ }
19691985 /* initialize pyexpat dispatch table */
1970- capi . size = sizeof (capi );
1971- capi . magic = PyExpat_CAPI_MAGIC ;
1972- capi . MAJOR_VERSION = XML_MAJOR_VERSION ;
1973- capi . MINOR_VERSION = XML_MINOR_VERSION ;
1974- capi . MICRO_VERSION = XML_MICRO_VERSION ;
1975- capi . ErrorString = XML_ErrorString ;
1976- capi . GetErrorCode = XML_GetErrorCode ;
1977- capi . GetErrorColumnNumber = XML_GetErrorColumnNumber ;
1978- capi . GetErrorLineNumber = XML_GetErrorLineNumber ;
1979- capi . Parse = XML_Parse ;
1980- capi . ParserCreate_MM = XML_ParserCreate_MM ;
1981- capi . ParserFree = XML_ParserFree ;
1982- capi . SetCharacterDataHandler = XML_SetCharacterDataHandler ;
1983- capi . SetCommentHandler = XML_SetCommentHandler ;
1984- capi . SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand ;
1985- capi . SetElementHandler = XML_SetElementHandler ;
1986- capi . SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler ;
1987- capi . SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler ;
1988- capi . SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler ;
1989- capi . SetUserData = XML_SetUserData ;
1990- capi . SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler ;
1991- capi . SetEncoding = XML_SetEncoding ;
1992- capi . DefaultUnknownEncodingHandler = PyUnknownEncodingHandler ;
1986+ capi -> size = sizeof (* capi );
1987+ capi -> magic = PyExpat_CAPI_MAGIC ;
1988+ capi -> MAJOR_VERSION = XML_MAJOR_VERSION ;
1989+ capi -> MINOR_VERSION = XML_MINOR_VERSION ;
1990+ capi -> MICRO_VERSION = XML_MICRO_VERSION ;
1991+ capi -> ErrorString = XML_ErrorString ;
1992+ capi -> GetErrorCode = XML_GetErrorCode ;
1993+ capi -> GetErrorColumnNumber = XML_GetErrorColumnNumber ;
1994+ capi -> GetErrorLineNumber = XML_GetErrorLineNumber ;
1995+ capi -> Parse = XML_Parse ;
1996+ capi -> ParserCreate_MM = XML_ParserCreate_MM ;
1997+ capi -> ParserFree = XML_ParserFree ;
1998+ capi -> SetCharacterDataHandler = XML_SetCharacterDataHandler ;
1999+ capi -> SetCommentHandler = XML_SetCommentHandler ;
2000+ capi -> SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand ;
2001+ capi -> SetElementHandler = XML_SetElementHandler ;
2002+ capi -> SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler ;
2003+ capi -> SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler ;
2004+ capi -> SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler ;
2005+ capi -> SetUserData = XML_SetUserData ;
2006+ capi -> SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler ;
2007+ capi -> SetEncoding = XML_SetEncoding ;
2008+ capi -> DefaultUnknownEncodingHandler = PyUnknownEncodingHandler ;
19932009#if XML_COMBINED_VERSION >= 20100
1994- capi . SetHashSalt = XML_SetHashSalt ;
2010+ capi -> SetHashSalt = XML_SetHashSalt ;
19952011#else
1996- capi . SetHashSalt = NULL ;
2012+ capi -> SetHashSalt = NULL ;
19972013#endif
19982014
19992015 /* export using capsule */
2000- PyObject * capi_object = PyCapsule_New (& capi , PyExpat_CAPSULE_NAME , NULL );
2016+ PyObject * capi_object = PyCapsule_New (capi , PyExpat_CAPSULE_NAME ,
2017+ pyexpat_capsule_destructor );
20012018 if (capi_object == NULL ) {
2019+ PyMem_Free (capi );
20022020 return -1 ;
20032021 }
20042022
0 commit comments