@@ -1878,6 +1878,18 @@ add_features(PyObject *mod)
1878
1878
}
1879
1879
#endif
1880
1880
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
+
1881
1893
static int
1882
1894
pyexpat_exec (PyObject * mod )
1883
1895
{
@@ -1965,40 +1977,46 @@ pyexpat_exec(PyObject *mod)
1965
1977
MYCONST (XML_PARAM_ENTITY_PARSING_ALWAYS );
1966
1978
#undef MYCONST
1967
1979
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
+ }
1969
1985
/* 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 ;
1993
2009
#if XML_COMBINED_VERSION >= 20100
1994
- capi . SetHashSalt = XML_SetHashSalt ;
2010
+ capi -> SetHashSalt = XML_SetHashSalt ;
1995
2011
#else
1996
- capi . SetHashSalt = NULL ;
2012
+ capi -> SetHashSalt = NULL ;
1997
2013
#endif
1998
2014
1999
2015
/* 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 );
2001
2018
if (capi_object == NULL ) {
2019
+ PyMem_Free (capi );
2002
2020
return -1 ;
2003
2021
}
2004
2022
0 commit comments