diff options
Diffstat (limited to 'Modules/pyexpat.c')
-rw-r--r-- | Modules/pyexpat.c | 383 |
1 files changed, 207 insertions, 176 deletions
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index 6a901f7..9a6da73 100644 --- 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) static XML_Memory_Handling_Suite ExpatMemoryHandler = { @@ -61,6 +68,8 @@ typedef struct { PyObject **handlers; } xmlparseobject; +#include "clinic/pyexpat.c.h" + #define CHARACTER_DATA_BUFFER_SIZE 8192 static PyTypeObject Xmlparsetype; @@ -671,6 +680,11 @@ VOID_HANDLER(StartDoctypeDecl, 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) @@ -687,25 +701,30 @@ get_parse_result(xmlparseobject *self, int rv) 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(c_default="0") = False + / + +Parse XML data. + +`isfinal' should be true at end of input. +[clinic start generated code]*/ + static PyObject * -xmlparse_Parse(xmlparseobject *self, PyObject *args) +pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data, + int isfinal) +/*[clinic end generated code: output=f4db843dd1f4ed4b input=199d9e8e92ebbb4b]*/ { - PyObject *data; - int isFinal = 0; 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); @@ -729,7 +748,7 @@ xmlparse_Parse(xmlparseobject *self, PyObject *args) slen -= MAX_CHUNK_SIZE; } assert(MAX_CHUNK_SIZE < INT_MAX && slen < INT_MAX); - rc = XML_Parse(self->itself, s, (int)slen, isFinal); + rc = XML_Parse(self->itself, s, (int)slen, isfinal); done: if (view.buf != NULL) @@ -780,18 +799,24 @@ error: 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]*/ static PyObject * -xmlparse_ParseFile(xmlparseobject *self, PyObject *f) +pyexpat_xmlparser_ParseFile(xmlparseobject *self, PyObject *file) +/*[clinic end generated code: output=2adc6a13100cc42b 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"); @@ -823,42 +848,50 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f) 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]*/ static PyObject * -xmlparse_SetBase(xmlparseobject *self, PyObject *args) +pyexpat_xmlparser_SetBase_impl(xmlparseobject *self, const char *base) +/*[clinic end generated code: output=c212ddceb607b539 input=c684e5de895ee1a8]*/ { - char *base; - - if (!PyArg_ParseTuple(args, "s:SetBase", &base)) - return NULL; 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]*/ static PyObject * -xmlparse_GetBase(xmlparseobject *self, PyObject *unused) +pyexpat_xmlparser_GetBase_impl(xmlparseobject *self) +/*[clinic end generated code: output=2886cb21f9a8739a 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]*/ static PyObject * -xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused) +pyexpat_xmlparser_GetInputContext_impl(xmlparseobject *self) +/*[clinic end generated code: output=a88026d683fc22cc input=034df8712db68379]*/ { if (self->in_callback) { int offset, size; @@ -875,24 +908,25 @@ xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused) 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(accept={str, NoneType}) + encoding: str = NULL + / + +Create a parser for parsing an external entity based on the information passed to the ExternalEntityRefHandler. +[clinic start generated code]*/ static PyObject * -xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args) +pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self, + const char *context, + const char *encoding) +/*[clinic end generated code: output=535cda9d7a0fbcd6 input=b906714cc122c322]*/ { - char *context; - char *encoding = NULL; 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; @@ -948,41 +982,49 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args) 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]*/ + +static PyObject * +pyexpat_xmlparser_SetParamEntityParsing_impl(xmlparseobject *self, int flag) +/*[clinic end generated code: output=18668ee8e760d64c input=8aea19b4b15e9af1]*/ { - int flag; - if (!PyArg_ParseTuple(args, "i", &flag)) - return NULL; - flag = XML_SetParamEntityParsing(p->itself, flag); + 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]*/ static PyObject * -xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args) +pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, int flag) +/*[clinic end generated code: output=cfaa9aa50bb0f65c input=78144c519d116a6e]*/ { - int flag = 1; 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); @@ -992,29 +1034,70 @@ xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args) } #endif -static PyObject *xmlparse_dir(PyObject *self, PyObject* noargs); +/*[clinic input] +pyexpat.xmlparser.__dir__ +[clinic start generated code]*/ + +static PyObject * +pyexpat_xmlparser___dir___impl(xmlparseobject *self) +/*[clinic end generated code: output=bc22451efb9e4d17 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 */ }; /* ---------- */ @@ -1077,7 +1160,7 @@ PyUnknownEncodingHandler(void *encodingHandlerData, static PyObject * -newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern) +newxmlparseobject(const char *encoding, const char *namespace_separator, PyObject *intern) { int i; xmlparseobject *self; @@ -1255,52 +1338,6 @@ xmlparse_getattro(xmlparseobject *self, PyObject *nameobj) 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) { @@ -1512,24 +1549,24 @@ static PyTypeObject Xmlparsetype = { /* End of code for xmlparser objects */ /* -------------------------------------------------------- */ -PyDoc_STRVAR(pyexpat_ParserCreate__doc__, -"ParserCreate([encoding[, namespace_separator]]) -> parser\n\ -Return a new XML parser object."); +/*[clinic input] +pyexpat.ParserCreate + + encoding: str(accept={str, NoneType}) = NULL + namespace_separator: str(accept={str, NoneType}) = NULL + intern: object = NULL + +Return a new XML parser object. +[clinic start generated code]*/ static PyObject * -pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw) +pyexpat_ParserCreate_impl(PyModuleDef *module, const char *encoding, + const char *namespace_separator, PyObject *intern) +/*[clinic end generated code: output=81fccd233e1743a8 input=23d29704acad385d]*/ { - char *encoding = NULL; - char *namespace_separator = NULL; - PyObject *intern = NULL; 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, @@ -1559,29 +1596,28 @@ pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw) return result; } -PyDoc_STRVAR(pyexpat_ErrorString__doc__, -"ErrorString(errno) -> string\n\ -Returns string error for given number."); +/*[clinic input] +pyexpat.ErrorString + + code: long + / + +Returns string error for given number. +[clinic start generated code]*/ static PyObject * -pyexpat_ErrorString(PyObject *self, PyObject *args) +pyexpat_ErrorString_impl(PyModuleDef *module, long code) +/*[clinic end generated code: output=d87668108b6868e5 input=cc67de010d9e62b3]*/ { - long code = 0; - - if (!PyArg_ParseTuple(args, "l:ErrorString", &code)) - return NULL; 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 */ @@ -1599,16 +1635,6 @@ PyDoc_STRVAR(pyexpat_module_documentation, #define MODULE_INITFUNC PyInit_pyexpat #endif -#ifndef PyMODINIT_FUNC -# ifdef MS_WINDOWS -# define PyMODINIT_FUNC __declspec(dllexport) void -# else -# define PyMODINIT_FUNC void -# endif -#endif - -PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */ - static struct PyModuleDef pyexpatmodule = { PyModuleDef_HEAD_INIT, MODULE_NAME, @@ -1666,7 +1692,7 @@ MODULE_INITFUNC(void) PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype); PyModule_AddStringConstant(m, "EXPAT_VERSION", - (char *) XML_ExpatVersion()); + XML_ExpatVersion()); { XML_Expat_Version info = XML_ExpatVersionInfo(); PyModule_AddObject(m, "version_info", @@ -1746,7 +1772,7 @@ MODULE_INITFUNC(void) #define MYCONST(name) \ if (PyModule_AddStringConstant(errors_module, #name, \ - (char *)XML_ErrorString(name)) < 0) \ + XML_ErrorString(name)) < 0) \ return NULL; \ tmpnum = PyLong_FromLong(name); \ if (tmpnum == NULL) return NULL; \ @@ -1957,3 +1983,8 @@ static struct HandlerInfo handler_info[] = { {NULL, NULL, NULL} /* sentinel */ }; + +/*[clinic input] +dump buffer +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |