[Python-checkins] commit of r41664 - in python/trunk: Include/pyexpat.h Modules/pyexpat.c

fredrik.lundh python-checkins at python.org
Tue Dec 13 20:49:56 CET 2005


Author: fredrik.lundh
Date: Tue Dec 13 20:49:55 2005
New Revision: 41664

Added:
   python/trunk/Include/pyexpat.h
Modified:
   python/trunk/Modules/pyexpat.c
Log:
added cobject-based expat dispatch mechanism to pyexpat



Added: python/trunk/Include/pyexpat.h
==============================================================================
--- (empty file)
+++ python/trunk/Include/pyexpat.h	Tue Dec 13 20:49:55 2005
@@ -0,0 +1,45 @@
+/* Stuff to export relevant 'expat' entry points from pyexpat to other
+ * parser modules, such as cElementTree. */
+
+/* note: you must import expat.h before importing this module! */
+
+#define PyExpat_DISPATCH_MAGIC  "pyexpat.dispatch 1.0"
+
+struct PyExpat_Dispatch 
+{
+    int size; /* set to sizeof(struct PyExpat_Dispatch) */
+    int MAJOR_VERSION; /* XXX: use the ExpatVersionInfo instead? */
+    int MINOR_VERSION;
+    int MICRO_VERSION;
+    /* pointers to selected expat functions.  add new functions at
+       the end, if needed */
+    const XML_LChar * (*ErrorString)(enum XML_Error code);
+    int (*GetCurrentColumnNumber)(XML_Parser parser);
+    int (*GetCurrentLineNumber)(XML_Parser parser);
+    enum XML_Status (*Parse)(
+        XML_Parser parser, const char *s, int len, int isFinal);
+    XML_Parser (*ParserCreate_MM)(
+        const XML_Char *encoding, const XML_Memory_Handling_Suite *memsuite,
+        const XML_Char *namespaceSeparator);
+    void (*ParserFree)(XML_Parser parser);
+    void (*SetCharacterDataHandler)(
+        XML_Parser parser, XML_CharacterDataHandler handler);
+    void (*SetCommentHandler)(
+        XML_Parser parser, XML_CommentHandler handler);
+    void (*SetDefaultHandlerExpand)(
+        XML_Parser parser, XML_DefaultHandler handler);
+    void (*SetElementHandler)(
+        XML_Parser parser, XML_StartElementHandler start,
+        XML_EndElementHandler end);
+    void (*SetNamespaceDeclHandler)(
+        XML_Parser parser, XML_StartNamespaceDeclHandler start,
+        XML_EndNamespaceDeclHandler end);
+    void (*SetProcessingInstructionHandler)(
+        XML_Parser parser, XML_ProcessingInstructionHandler handler);
+    void (*SetUnknownEncodingHandler)(
+        XML_Parser parser, XML_UnknownEncodingHandler handler,
+        void *encodingHandlerData);
+    void (*SetUserData)(XML_Parser parser, void *userData);
+    /* always add new stuff to the end! */
+};
+

Modified: python/trunk/Modules/pyexpat.c
==============================================================================
--- python/trunk/Modules/pyexpat.c	(original)
+++ python/trunk/Modules/pyexpat.c	Tue Dec 13 20:49:55 2005
@@ -4,6 +4,8 @@
 #include "frameobject.h"
 #include "expat.h"
 
+#include "pyexpat.h"
+
 #define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
 
 #ifndef PyDoc_STRVAR
@@ -1838,6 +1840,8 @@
     PyObject *modelmod_name;
     PyObject *model_module;
     PyObject *sys_modules;
+    static struct PyExpat_Dispatch dispatch;
+    PyObject* dispatch_object;
 
     if (errmod_name == NULL)
         return;
@@ -2011,6 +2015,33 @@
     MYCONST(XML_CQUANT_REP);
     MYCONST(XML_CQUANT_PLUS);
 #undef MYCONST
+
+    /* initialize pyexpat dispatch table */
+    dispatch.size = sizeof(dispatch);
+    dispatch.MAJOR_VERSION = XML_MAJOR_VERSION;
+    dispatch.MINOR_VERSION = XML_MINOR_VERSION;
+    dispatch.MICRO_VERSION = XML_MICRO_VERSION;
+    dispatch.ErrorString = XML_ErrorString;
+    dispatch.GetCurrentColumnNumber = XML_GetCurrentColumnNumber;
+    dispatch.GetCurrentLineNumber = XML_GetCurrentLineNumber;
+    dispatch.Parse = XML_Parse;
+    dispatch.ParserCreate_MM = XML_ParserCreate_MM;
+    dispatch.ParserFree = XML_ParserFree;
+    dispatch.SetCharacterDataHandler = XML_SetCharacterDataHandler;
+    dispatch.SetCommentHandler = XML_SetCommentHandler;
+    dispatch.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
+    dispatch.SetElementHandler = XML_SetElementHandler;
+    dispatch.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
+    dispatch.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
+    dispatch.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
+    dispatch.SetUserData = XML_SetUserData;
+    
+    /* export as cobject */
+    dispatch_object = PyCObject_FromVoidPtrAndDesc(
+        &dispatch, PyExpat_DISPATCH_MAGIC, NULL
+        );
+    if (dispatch_object)
+        PyModule_AddObject(m, "dispatch", dispatch_object);
 }
 
 static void


More information about the Python-checkins mailing list