[Python-checkins] cpython: Issue #20152: Port pyexpat to Argument Clinic.

brett.cannon python-checkins at python.org
Fri Aug 22 20:23:28 CEST 2014


http://hg.python.org/cpython/rev/0c2792a8f9db
changeset:   92194:0c2792a8f9db
user:        Brett Cannon <brett at python.org>
date:        Fri Aug 22 14:23:20 2014 -0400
summary:
  Issue #20152: Port pyexpat to Argument Clinic.

Could not emit an external file as pyexpat has a conditionally built
method which Clinic won't hide otherwise.

files:
  Modules/pyexpat.c |  625 +++++++++++++++++++++++++--------
  1 files changed, 471 insertions(+), 154 deletions(-)


diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -6,6 +6,13 @@
 
 #include "pyexpat.h"
 
+/* Do not emit Clinic output to a file as that wreaks havoc with conditionally
+   included methods. */
+/*[clinic input]
+module pyexpat
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b168d503a4490c15]*/
+
 #define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
 
 #define FIX_TRACE
@@ -777,6 +784,11 @@
 VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
 
 /* ---------------------------------------------------------------- */
+/*[clinic input]
+class pyexpat.xmlparser "xmlparseobject *" "&Xmlparsetype"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2393162385232e1c]*/
+
 
 static PyObject *
 get_parse_result(xmlparseobject *self, int rv)
@@ -793,25 +805,60 @@
     return PyLong_FromLong(rv);
 }
 
-PyDoc_STRVAR(xmlparse_Parse__doc__,
-"Parse(data[, isfinal])\n\
-Parse XML data.  `isfinal' should be true at end of input.");
-
 #define MAX_CHUNK_SIZE (1 << 20)
 
+/*[clinic input]
+pyexpat.xmlparser.Parse
+
+    data: object
+    isFinal: int = 0
+    /
+
+Parse XML data.
+
+`isfinal' should be true at end of input.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pyexpat_xmlparser_Parse__doc__,
+"Parse($self, data, isFinal=0, /)\n"
+"--\n"
+"\n"
+"Parse XML data.\n"
+"\n"
+"`isfinal\' should be true at end of input.");
+
+#define PYEXPAT_XMLPARSER_PARSE_METHODDEF    \
+    {"Parse", (PyCFunction)pyexpat_xmlparser_Parse, METH_VARARGS, pyexpat_xmlparser_Parse__doc__},
+
 static PyObject *
-xmlparse_Parse(xmlparseobject *self, PyObject *args)
+pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data, int isFinal);
+
+static PyObject *
+pyexpat_xmlparser_Parse(xmlparseobject *self, PyObject *args)
 {
+    PyObject *return_value = NULL;
     PyObject *data;
     int isFinal = 0;
+
+    if (!PyArg_ParseTuple(args,
+        "O|i:Parse",
+        &data, &isFinal))
+        goto exit;
+    return_value = pyexpat_xmlparser_Parse_impl(self, data, isFinal);
+
+exit:
+    return return_value;
+}
+
+static PyObject *
+pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data, int isFinal)
+/*[clinic end generated code: output=65b1652b01f20856 input=e37b81b8948ca7e0]*/
+{
     const char *s;
     Py_ssize_t slen;
     Py_buffer view;
     int rc;
 
-    if (!PyArg_ParseTuple(args, "O|i:Parse", &data, &isFinal))
-        return NULL;
-
     if (PyUnicode_Check(data)) {
         view.buf = NULL;
         s = PyUnicode_AsUTF8AndSize(data, &slen);
@@ -886,18 +933,33 @@
     return -1;
 }
 
-PyDoc_STRVAR(xmlparse_ParseFile__doc__,
-"ParseFile(file)\n\
-Parse XML data from file-like object.");
+/*[clinic input]
+pyexpat.xmlparser.ParseFile
+
+    file: object
+    /
+
+Parse XML data from file-like object.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pyexpat_xmlparser_ParseFile__doc__,
+"ParseFile($self, file, /)\n"
+"--\n"
+"\n"
+"Parse XML data from file-like object.");
+
+#define PYEXPAT_XMLPARSER_PARSEFILE_METHODDEF    \
+    {"ParseFile", (PyCFunction)pyexpat_xmlparser_ParseFile, METH_O, pyexpat_xmlparser_ParseFile__doc__},
 
 static PyObject *
-xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
+pyexpat_xmlparser_ParseFile(xmlparseobject *self, PyObject *file)
+/*[clinic end generated code: output=2e13803c3d8c22b2 input=fbb5a12b6038d735]*/
 {
     int rv = 1;
     PyObject *readmethod = NULL;
     _Py_IDENTIFIER(read);
 
-    readmethod = _PyObject_GetAttrId(f, &PyId_read);
+    readmethod = _PyObject_GetAttrId(file, &PyId_read);
     if (readmethod == NULL) {
         PyErr_SetString(PyExc_TypeError,
                         "argument must have 'read' attribute");
@@ -929,42 +991,117 @@
     return get_parse_result(self, rv);
 }
 
-PyDoc_STRVAR(xmlparse_SetBase__doc__,
-"SetBase(base_url)\n\
-Set the base URL for the parser.");
+/*[clinic input]
+pyexpat.xmlparser.SetBase
+
+    base: str
+    /
+
+Set the base URL for the parser.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pyexpat_xmlparser_SetBase__doc__,
+"SetBase($self, base, /)\n"
+"--\n"
+"\n"
+"Set the base URL for the parser.");
+
+#define PYEXPAT_XMLPARSER_SETBASE_METHODDEF    \
+    {"SetBase", (PyCFunction)pyexpat_xmlparser_SetBase, METH_VARARGS, pyexpat_xmlparser_SetBase__doc__},
 
 static PyObject *
-xmlparse_SetBase(xmlparseobject *self, PyObject *args)
+pyexpat_xmlparser_SetBase_impl(xmlparseobject *self, const char *base);
+
+static PyObject *
+pyexpat_xmlparser_SetBase(xmlparseobject *self, PyObject *args)
 {
-    char *base;
+    PyObject *return_value = NULL;
+    const char *base;
 
-    if (!PyArg_ParseTuple(args, "s:SetBase", &base))
-        return NULL;
+    if (!PyArg_ParseTuple(args,
+        "s:SetBase",
+        &base))
+        goto exit;
+    return_value = pyexpat_xmlparser_SetBase_impl(self, base);
+
+exit:
+    return return_value;
+}
+
+static PyObject *
+pyexpat_xmlparser_SetBase_impl(xmlparseobject *self, const char *base)
+/*[clinic end generated code: output=5bdb49f6689a5f93 input=c684e5de895ee1a8]*/
+{
     if (!XML_SetBase(self->itself, base)) {
         return PyErr_NoMemory();
     }
-    Py_INCREF(Py_None);
-    return Py_None;
+    Py_RETURN_NONE;
 }
 
-PyDoc_STRVAR(xmlparse_GetBase__doc__,
-"GetBase() -> url\n\
-Return base URL string for the parser.");
+/*[clinic input]
+pyexpat.xmlparser.GetBase
+
+Return base URL string for the parser.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pyexpat_xmlparser_GetBase__doc__,
+"GetBase($self, /)\n"
+"--\n"
+"\n"
+"Return base URL string for the parser.");
+
+#define PYEXPAT_XMLPARSER_GETBASE_METHODDEF    \
+    {"GetBase", (PyCFunction)pyexpat_xmlparser_GetBase, METH_NOARGS, pyexpat_xmlparser_GetBase__doc__},
 
 static PyObject *
-xmlparse_GetBase(xmlparseobject *self, PyObject *unused)
+pyexpat_xmlparser_GetBase_impl(xmlparseobject *self);
+
+static PyObject *
+pyexpat_xmlparser_GetBase(xmlparseobject *self, PyObject *Py_UNUSED(ignored))
+{
+    return pyexpat_xmlparser_GetBase_impl(self);
+}
+
+static PyObject *
+pyexpat_xmlparser_GetBase_impl(xmlparseobject *self)
+/*[clinic end generated code: output=ef6046ee28f2b8ee input=918d71c38009620e]*/
 {
     return Py_BuildValue("z", XML_GetBase(self->itself));
 }
 
-PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
-"GetInputContext() -> string\n\
-Return the untranslated text of the input that caused the current event.\n\
-If the event was generated by a large amount of text (such as a start tag\n\
-for an element with many attributes), not all of the text may be available.");
+/*[clinic input]
+pyexpat.xmlparser.GetInputContext
+
+Return the untranslated text of the input that caused the current event.
+
+If the event was generated by a large amount of text (such as a start tag
+for an element with many attributes), not all of the text may be available.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pyexpat_xmlparser_GetInputContext__doc__,
+"GetInputContext($self, /)\n"
+"--\n"
+"\n"
+"Return the untranslated text of the input that caused the current event.\n"
+"\n"
+"If the event was generated by a large amount of text (such as a start tag\n"
+"for an element with many attributes), not all of the text may be available.");
+
+#define PYEXPAT_XMLPARSER_GETINPUTCONTEXT_METHODDEF    \
+    {"GetInputContext", (PyCFunction)pyexpat_xmlparser_GetInputContext, METH_NOARGS, pyexpat_xmlparser_GetInputContext__doc__},
 
 static PyObject *
-xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused)
+pyexpat_xmlparser_GetInputContext_impl(xmlparseobject *self);
+
+static PyObject *
+pyexpat_xmlparser_GetInputContext(xmlparseobject *self, PyObject *Py_UNUSED(ignored))
+{
+    return pyexpat_xmlparser_GetInputContext_impl(self);
+}
+
+static PyObject *
+pyexpat_xmlparser_GetInputContext_impl(xmlparseobject *self)
+/*[clinic end generated code: output=62ff03390f074cd2 input=034df8712db68379]*/
 {
     if (self->in_callback) {
         int offset, size;
@@ -981,24 +1118,52 @@
         Py_RETURN_NONE;
 }
 
-PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
-"ExternalEntityParserCreate(context[, encoding])\n\
-Create a parser for parsing an external entity based on the\n\
-information passed to the ExternalEntityRefHandler.");
+/*[clinic input]
+pyexpat.xmlparser.ExternalEntityParserCreate
+
+    context: str(nullable=True)
+    encoding: str = NULL
+    /
+
+Create a parser for parsing an external entity based on the information passed to the ExternalEntityRefHandler.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pyexpat_xmlparser_ExternalEntityParserCreate__doc__,
+"ExternalEntityParserCreate($self, context, encoding=None, /)\n"
+"--\n"
+"\n"
+"Create a parser for parsing an external entity based on the information passed to the ExternalEntityRefHandler.");
+
+#define PYEXPAT_XMLPARSER_EXTERNALENTITYPARSERCREATE_METHODDEF    \
+    {"ExternalEntityParserCreate", (PyCFunction)pyexpat_xmlparser_ExternalEntityParserCreate, METH_VARARGS, pyexpat_xmlparser_ExternalEntityParserCreate__doc__},
 
 static PyObject *
-xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
+pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self, const char *context, const char *encoding);
+
+static PyObject *
+pyexpat_xmlparser_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
 {
-    char *context;
-    char *encoding = NULL;
+    PyObject *return_value = NULL;
+    const char *context;
+    const char *encoding = NULL;
+
+    if (!PyArg_ParseTuple(args,
+        "z|s:ExternalEntityParserCreate",
+        &context, &encoding))
+        goto exit;
+    return_value = pyexpat_xmlparser_ExternalEntityParserCreate_impl(self, context, encoding);
+
+exit:
+    return return_value;
+}
+
+static PyObject *
+pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self, const char *context, const char *encoding)
+/*[clinic end generated code: output=4948c35f3dd01133 input=283206575d960272]*/
+{
     xmlparseobject *new_parser;
     int i;
 
-    if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
-                          &context, &encoding)) {
-        return NULL;
-    }
-
     new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
     if (new_parser == NULL)
         return NULL;
@@ -1054,41 +1219,114 @@
     return (PyObject *)new_parser;
 }
 
-PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
-"SetParamEntityParsing(flag) -> success\n\
-Controls parsing of parameter entities (including the external DTD\n\
-subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
-XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
-XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
-was successful.");
+/*[clinic input]
+pyexpat.xmlparser.SetParamEntityParsing
 
-static PyObject*
-xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
+    flag: int
+    /
+
+Controls parsing of parameter entities (including the external DTD subset).
+
+Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,
+XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and
+XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag
+was successful.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pyexpat_xmlparser_SetParamEntityParsing__doc__,
+"SetParamEntityParsing($self, flag, /)\n"
+"--\n"
+"\n"
+"Controls parsing of parameter entities (including the external DTD subset).\n"
+"\n"
+"Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n"
+"XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n"
+"XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n"
+"was successful.");
+
+#define PYEXPAT_XMLPARSER_SETPARAMENTITYPARSING_METHODDEF    \
+    {"SetParamEntityParsing", (PyCFunction)pyexpat_xmlparser_SetParamEntityParsing, METH_VARARGS, pyexpat_xmlparser_SetParamEntityParsing__doc__},
+
+static PyObject *
+pyexpat_xmlparser_SetParamEntityParsing_impl(xmlparseobject *self, int flag);
+
+static PyObject *
+pyexpat_xmlparser_SetParamEntityParsing(xmlparseobject *self, PyObject *args)
 {
+    PyObject *return_value = NULL;
     int flag;
-    if (!PyArg_ParseTuple(args, "i", &flag))
-        return NULL;
-    flag = XML_SetParamEntityParsing(p->itself, flag);
+
+    if (!PyArg_ParseTuple(args,
+        "i:SetParamEntityParsing",
+        &flag))
+        goto exit;
+    return_value = pyexpat_xmlparser_SetParamEntityParsing_impl(self, flag);
+
+exit:
+    return return_value;
+}
+
+static PyObject *
+pyexpat_xmlparser_SetParamEntityParsing_impl(xmlparseobject *self, int flag)
+/*[clinic end generated code: output=0f820882bc7768cc input=8aea19b4b15e9af1]*/
+{
+    flag = XML_SetParamEntityParsing(self->itself, flag);
     return PyLong_FromLong(flag);
 }
 
 
 #if XML_COMBINED_VERSION >= 19505
-PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
-"UseForeignDTD([flag])\n\
-Allows the application to provide an artificial external subset if one is\n\
-not specified as part of the document instance.  This readily allows the\n\
-use of a 'default' document type controlled by the application, while still\n\
-getting the advantage of providing document type information to the parser.\n\
-'flag' defaults to True if not provided.");
+/*[clinic input]
+pyexpat.xmlparser.UseForeignDTD
+
+    flag: bool = True
+    /
+
+Allows the application to provide an artificial external subset if one is not specified as part of the document instance.
+
+This readily allows the use of a 'default' document type controlled by the
+application, while still getting the advantage of providing document type
+information to the parser. 'flag' defaults to True if not provided.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pyexpat_xmlparser_UseForeignDTD__doc__,
+"UseForeignDTD($self, flag=True, /)\n"
+"--\n"
+"\n"
+"Allows the application to provide an artificial external subset if one is not specified as part of the document instance.\n"
+"\n"
+"This readily allows the use of a \'default\' document type controlled by the\n"
+"application, while still getting the advantage of providing document type\n"
+"information to the parser. \'flag\' defaults to True if not provided.");
+
+#define PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF    \
+    {"UseForeignDTD", (PyCFunction)pyexpat_xmlparser_UseForeignDTD, METH_VARARGS, pyexpat_xmlparser_UseForeignDTD__doc__},
 
 static PyObject *
-xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
+pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, int flag);
+
+static PyObject *
+pyexpat_xmlparser_UseForeignDTD(xmlparseobject *self, PyObject *args)
 {
+    PyObject *return_value = NULL;
     int flag = 1;
+
+    if (!PyArg_ParseTuple(args,
+        "|p:UseForeignDTD",
+        &flag))
+        goto exit;
+    return_value = pyexpat_xmlparser_UseForeignDTD_impl(self, flag);
+
+exit:
+    return return_value;
+}
+
+static PyObject *
+pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, int flag)
+/*[clinic end generated code: output=22e924ae6cad67d6 input=78144c519d116a6e]*/
+{
     enum XML_Error rc;
-    if (!PyArg_ParseTuple(args, "|p:UseForeignDTD", &flag))
-        return NULL;
+
     rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE);
     if (rc != XML_ERROR_NONE) {
         return set_error(self, rc);
@@ -1098,29 +1336,86 @@
 }
 #endif
 
-static PyObject *xmlparse_dir(PyObject *self, PyObject* noargs);
+/*[clinic input]
+pyexpat.xmlparser.__dir__
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pyexpat_xmlparser___dir____doc__,
+"__dir__($self, /)\n"
+"--");
+
+#define PYEXPAT_XMLPARSER___DIR___METHODDEF    \
+    {"__dir__", (PyCFunction)pyexpat_xmlparser___dir__, METH_NOARGS, pyexpat_xmlparser___dir____doc__},
+
+static PyObject *
+pyexpat_xmlparser___dir___impl(xmlparseobject *self);
+
+static PyObject *
+pyexpat_xmlparser___dir__(xmlparseobject *self, PyObject *Py_UNUSED(ignored))
+{
+    return pyexpat_xmlparser___dir___impl(self);
+}
+
+static PyObject *
+pyexpat_xmlparser___dir___impl(xmlparseobject *self)
+/*[clinic end generated code: output=1ed6efe83bc304cc input=76aa455f2a661384]*/
+{
+#define APPEND(list, str)                               \
+        do {                                            \
+                PyObject *o = PyUnicode_FromString(str);        \
+                if (o != NULL)                          \
+                        PyList_Append(list, o);         \
+                Py_XDECREF(o);                          \
+        } while (0)
+
+    int i;
+    PyObject *rc = PyList_New(0);
+    if (!rc)
+        return NULL;
+    for (i = 0; handler_info[i].name != NULL; i++) {
+        PyObject *o = get_handler_name(&handler_info[i]);
+        if (o != NULL)
+            PyList_Append(rc, o);
+        Py_XDECREF(o);
+    }
+    APPEND(rc, "ErrorCode");
+    APPEND(rc, "ErrorLineNumber");
+    APPEND(rc, "ErrorColumnNumber");
+    APPEND(rc, "ErrorByteIndex");
+    APPEND(rc, "CurrentLineNumber");
+    APPEND(rc, "CurrentColumnNumber");
+    APPEND(rc, "CurrentByteIndex");
+    APPEND(rc, "buffer_size");
+    APPEND(rc, "buffer_text");
+    APPEND(rc, "buffer_used");
+    APPEND(rc, "namespace_prefixes");
+    APPEND(rc, "ordered_attributes");
+    APPEND(rc, "specified_attributes");
+    APPEND(rc, "intern");
+
+#undef APPEND
+
+    if (PyErr_Occurred()) {
+        Py_DECREF(rc);
+        rc = NULL;
+    }
+
+    return rc;
+}
 
 static struct PyMethodDef xmlparse_methods[] = {
-    {"Parse",     (PyCFunction)xmlparse_Parse,
-                  METH_VARARGS, xmlparse_Parse__doc__},
-    {"ParseFile", (PyCFunction)xmlparse_ParseFile,
-                  METH_O,       xmlparse_ParseFile__doc__},
-    {"SetBase",   (PyCFunction)xmlparse_SetBase,
-                  METH_VARARGS, xmlparse_SetBase__doc__},
-    {"GetBase",   (PyCFunction)xmlparse_GetBase,
-                  METH_NOARGS, xmlparse_GetBase__doc__},
-    {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
-                  METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
-    {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
-                  METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
-    {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
-                  METH_NOARGS, xmlparse_GetInputContext__doc__},
+    PYEXPAT_XMLPARSER_PARSE_METHODDEF
+    PYEXPAT_XMLPARSER_PARSEFILE_METHODDEF
+    PYEXPAT_XMLPARSER_SETBASE_METHODDEF
+    PYEXPAT_XMLPARSER_GETBASE_METHODDEF
+    PYEXPAT_XMLPARSER_GETINPUTCONTEXT_METHODDEF
+    PYEXPAT_XMLPARSER_EXTERNALENTITYPARSERCREATE_METHODDEF
+    PYEXPAT_XMLPARSER_SETPARAMENTITYPARSING_METHODDEF
 #if XML_COMBINED_VERSION >= 19505
-    {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
-                  METH_VARARGS, xmlparse_UseForeignDTD__doc__},
+    PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF
 #endif
-    {"__dir__", xmlparse_dir, METH_NOARGS},
-    {NULL,        NULL}         /* sentinel */
+    PYEXPAT_XMLPARSER___DIR___METHODDEF
+    {NULL, NULL}  /* sentinel */
 };
 
 /* ---------- */
@@ -1361,52 +1656,6 @@
     return PyObject_GenericGetAttr((PyObject*)self, nameobj);
 }
 
-static PyObject *
-xmlparse_dir(PyObject *self, PyObject* noargs)
-{
-#define APPEND(list, str)                               \
-        do {                                            \
-                PyObject *o = PyUnicode_FromString(str);        \
-                if (o != NULL)                          \
-                        PyList_Append(list, o);         \
-                Py_XDECREF(o);                          \
-        } while (0)
-
-    int i;
-    PyObject *rc = PyList_New(0);
-    if (!rc)
-        return NULL;
-    for (i = 0; handler_info[i].name != NULL; i++) {
-        PyObject *o = get_handler_name(&handler_info[i]);
-        if (o != NULL)
-            PyList_Append(rc, o);
-        Py_XDECREF(o);
-    }
-    APPEND(rc, "ErrorCode");
-    APPEND(rc, "ErrorLineNumber");
-    APPEND(rc, "ErrorColumnNumber");
-    APPEND(rc, "ErrorByteIndex");
-    APPEND(rc, "CurrentLineNumber");
-    APPEND(rc, "CurrentColumnNumber");
-    APPEND(rc, "CurrentByteIndex");
-    APPEND(rc, "buffer_size");
-    APPEND(rc, "buffer_text");
-    APPEND(rc, "buffer_used");
-    APPEND(rc, "namespace_prefixes");
-    APPEND(rc, "ordered_attributes");
-    APPEND(rc, "specified_attributes");
-    APPEND(rc, "intern");
-
-#undef APPEND
-
-    if (PyErr_Occurred()) {
-        Py_DECREF(rc);
-        rc = NULL;
-    }
-
-    return rc;
-}
-
 static int
 sethandler(xmlparseobject *self, PyObject *name, PyObject* v)
 {
@@ -1612,24 +1861,55 @@
 /* End of code for xmlparser objects */
 /* -------------------------------------------------------- */
 
+/*[clinic input]
+pyexpat.ParserCreate
+
+    encoding: str(nullable=True) = NULL
+    namespace_separator: str(nullable=True) = NULL
+    intern: object = NULL
+
+Return a new XML parser object.
+[clinic start generated code]*/
+
 PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
-"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
-Return a new XML parser object.");
+"ParserCreate($module, /, encoding=None, namespace_separator=None,\n"
+"             intern=None)\n"
+"--\n"
+"\n"
+"Return a new XML parser object.");
+
+#define PYEXPAT_PARSERCREATE_METHODDEF    \
+    {"ParserCreate", (PyCFunction)pyexpat_ParserCreate, METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
 
 static PyObject *
-pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
+pyexpat_ParserCreate_impl(PyModuleDef *module, const char *encoding, const char *namespace_separator, PyObject *intern);
+
+static PyObject *
+pyexpat_ParserCreate(PyModuleDef *module, PyObject *args, PyObject *kwargs)
 {
-    char *encoding = NULL;
-    char *namespace_separator = NULL;
+    PyObject *return_value = NULL;
+    static char *_keywords[] = {"encoding", "namespace_separator", "intern", NULL};
+    const char *encoding = NULL;
+    const char *namespace_separator = NULL;
     PyObject *intern = NULL;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+        "|zzO:ParserCreate", _keywords,
+        &encoding, &namespace_separator, &intern))
+        goto exit;
+    return_value = pyexpat_ParserCreate_impl(module, encoding, namespace_separator, intern);
+
+exit:
+    return return_value;
+}
+
+static PyObject *
+pyexpat_ParserCreate_impl(PyModuleDef *module, const char *encoding, const char *namespace_separator, PyObject *intern)
+/*[clinic end generated code: output=4fc027dd33b7a2ac input=71b9f471aa6f8f86]*/
+{
     PyObject *result;
     int intern_decref = 0;
-    static char *kwlist[] = {"encoding", "namespace_separator",
-                                   "intern", NULL};
 
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
-                                     &encoding, &namespace_separator, &intern))
-        return NULL;
     if (namespace_separator != NULL
         && strlen(namespace_separator) > 1) {
         PyErr_SetString(PyExc_ValueError,
@@ -1652,36 +1932,64 @@
         return NULL;
     }
 
-    result = newxmlparseobject(encoding, namespace_separator, intern);
+    result = newxmlparseobject((char *)encoding, (char *)namespace_separator,
+                               intern);
     if (intern_decref) {
         Py_DECREF(intern);
     }
     return result;
 }
 
+/*[clinic input]
+pyexpat.ErrorString
+
+    code: long
+    /
+
+Returns string error for given number.
+[clinic start generated code]*/
+
 PyDoc_STRVAR(pyexpat_ErrorString__doc__,
-"ErrorString(errno) -> string\n\
-Returns string error for given number.");
+"ErrorString($module, code, /)\n"
+"--\n"
+"\n"
+"Returns string error for given number.");
+
+#define PYEXPAT_ERRORSTRING_METHODDEF    \
+    {"ErrorString", (PyCFunction)pyexpat_ErrorString, METH_VARARGS, pyexpat_ErrorString__doc__},
 
 static PyObject *
-pyexpat_ErrorString(PyObject *self, PyObject *args)
+pyexpat_ErrorString_impl(PyModuleDef *module, long code);
+
+static PyObject *
+pyexpat_ErrorString(PyModuleDef *module, PyObject *args)
 {
-    long code = 0;
+    PyObject *return_value = NULL;
+    long code;
 
-    if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
-        return NULL;
+    if (!PyArg_ParseTuple(args,
+        "l:ErrorString",
+        &code))
+        goto exit;
+    return_value = pyexpat_ErrorString_impl(module, code);
+
+exit:
+    return return_value;
+}
+
+static PyObject *
+pyexpat_ErrorString_impl(PyModuleDef *module, long code)
+/*[clinic end generated code: output=c70f3cd82bfaf067 input=cc67de010d9e62b3]*/
+{
     return Py_BuildValue("z", XML_ErrorString((int)code));
 }
 
 /* List of methods defined in the module */
 
 static struct PyMethodDef pyexpat_methods[] = {
-    {"ParserCreate",    (PyCFunction)pyexpat_ParserCreate,
-     METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
-    {"ErrorString",     (PyCFunction)pyexpat_ErrorString,
-     METH_VARARGS,      pyexpat_ErrorString__doc__},
-
-    {NULL,       (PyCFunction)NULL, 0, NULL}            /* sentinel */
+    PYEXPAT_PARSERCREATE_METHODDEF
+    PYEXPAT_ERRORSTRING_METHODDEF
+    {NULL, NULL}  /* sentinel */
 };
 
 /* Module docstring */
@@ -2057,3 +2365,12 @@
 
     {NULL, NULL, NULL} /* sentinel */
 };
+
+/*[clinic input]
+dump buffer
+[clinic start generated code]*/
+
+#ifndef PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF
+    #define PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF
+#endif /* !defined(PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF) */
+/*[clinic end generated code: output=a7880cb78bbd58ce input=524ce2e021e4eba6]*/

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list