diff options
Diffstat (limited to 'Modules/pyexpat.c')
-rw-r--r-- | Modules/pyexpat.c | 61 |
1 files changed, 28 insertions, 33 deletions
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index 9d22d3a..022b0cb 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -100,16 +100,17 @@ static PyObject * set_error(xmlparseobject *self, enum XML_Error code) { PyObject *err; - char buffer[256]; + PyObject *buffer; XML_Parser parser = self->itself; int lineno = XML_GetErrorLineNumber(parser); int column = XML_GetErrorColumnNumber(parser); - /* There is no risk of overflowing this buffer, since - even for 64-bit integers, there is sufficient space. */ - sprintf(buffer, "%.200s: line %i, column %i", - XML_ErrorString(code), lineno, column); - err = PyObject_CallFunction(ErrorObject, "s", buffer); + buffer = PyUnicode_FromFormat("%s: line %i, column %i", + XML_ErrorString(code), lineno, column); + if (buffer == NULL) + return NULL; + err = PyObject_CallFunction(ErrorObject, "O", buffer); + Py_DECREF(buffer); if ( err != NULL && set_error_attr(err, "code", code) && set_error_attr(err, "offset", column) @@ -793,13 +794,10 @@ xmlparse_Parse(xmlparseobject *self, PyObject *args) return NULL; if (PyUnicode_Check(data)) { - PyObject *bytes; - bytes = PyUnicode_AsUTF8String(data); - if (bytes == NULL) - return NULL; view.buf = NULL; - s = PyBytes_AS_STRING(bytes); - slen = PyBytes_GET_SIZE(bytes); + s = PyUnicode_AsUTF8AndSize(data, &slen); + if (s == NULL) + return NULL; /* Explicitly set UTF-8 encoding. Return code ignored. */ (void)XML_SetEncoding(self->itself, "utf-8"); } @@ -877,9 +875,9 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f) { int rv = 1; PyObject *readmethod = NULL; + _Py_IDENTIFIER(read); - - readmethod = PyObject_GetAttrString(f, "read"); + readmethod = _PyObject_GetAttrId(f, &PyId_read); if (readmethod == NULL) { PyErr_SetString(PyExc_TypeError, "argument must have 'read' attribute"); @@ -1067,16 +1065,10 @@ getting the advantage of providing document type information to the parser.\n\ static PyObject * xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args) { - PyObject *flagobj = NULL; int flag = 1; enum XML_Error rc; - if (!PyArg_ParseTuple(args, "|O:UseForeignDTD", &flagobj)) + if (!PyArg_ParseTuple(args, "|p:UseForeignDTD", &flag)) return NULL; - if (flagobj != NULL) { - flag = PyObject_IsTrue(flagobj); - if (flag < 0) - return NULL; - } rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE); if (rc != XML_ERROR_NONE) { return set_error(self, rc); @@ -1139,17 +1131,22 @@ PyUnknownEncodingHandler(void *encodingHandlerData, PyUnicodeObject *_u_string = NULL; int result = 0; int i; + int kind; + void *data; /* Yes, supports only 8bit encodings */ _u_string = (PyUnicodeObject *) PyUnicode_Decode(template_buffer, 256, name, "replace"); - if (_u_string == NULL) + if (_u_string == NULL || PyUnicode_READY(_u_string) == -1) return result; + kind = PyUnicode_KIND(_u_string); + data = PyUnicode_DATA(_u_string); + for (i = 0; i < 256; i++) { /* Stupid to access directly, but fast */ - Py_UNICODE c = _u_string->str[i]; + Py_UCS4 c = PyUnicode_READ(kind, data, i); if (c == Py_UNICODE_REPLACEMENT_CHARACTER) info->map[i] = -1; else @@ -1273,11 +1270,13 @@ get_pybool(int istrue) static PyObject * xmlparse_getattro(xmlparseobject *self, PyObject *nameobj) { - Py_UNICODE *name; + Py_UCS4 first_char; int handlernum = -1; if (!PyUnicode_Check(nameobj)) goto generic; + if (PyUnicode_READY(nameobj)) + return NULL; handlernum = handlername2int(nameobj); @@ -1289,8 +1288,8 @@ xmlparse_getattro(xmlparseobject *self, PyObject *nameobj) return result; } - name = PyUnicode_AS_UNICODE(nameobj); - if (name[0] == 'E') { + first_char = PyUnicode_READ_CHAR(nameobj, 0); + if (first_char == 'E') { if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorCode") == 0) return PyLong_FromLong((long) XML_GetErrorCode(self->itself)); @@ -1304,7 +1303,7 @@ xmlparse_getattro(xmlparseobject *self, PyObject *nameobj) return PyLong_FromLong((long) XML_GetErrorByteIndex(self->itself)); } - if (name[0] == 'C') { + if (first_char == 'C') { if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentLineNumber") == 0) return PyLong_FromLong((long) XML_GetCurrentLineNumber(self->itself)); @@ -1315,7 +1314,7 @@ xmlparse_getattro(xmlparseobject *self, PyObject *nameobj) return PyLong_FromLong((long) XML_GetCurrentByteIndex(self->itself)); } - if (name[0] == 'b') { + if (first_char == 'b') { if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_size") == 0) return PyLong_FromLong((long) self->buffer_size); if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_text") == 0) @@ -1709,7 +1708,6 @@ MODULE_INITFUNC(void) PyObject *errors_module; PyObject *modelmod_name; PyObject *model_module; - PyObject *version; PyObject *sys_modules; PyObject *tmpnum, *tmpstr; PyObject *codes_dict; @@ -1746,10 +1744,6 @@ MODULE_INITFUNC(void) Py_INCREF(&Xmlparsetype); PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype); - version = PyUnicode_FromString(PY_VERSION); - if (!version) - return NULL; - PyModule_AddObject(m, "__version__", version); PyModule_AddStringConstant(m, "EXPAT_VERSION", (char *) XML_ExpatVersion()); { @@ -1942,6 +1936,7 @@ MODULE_INITFUNC(void) capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler; capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler; capi.SetUserData = XML_SetUserData; + capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler; /* export using capsule */ capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL); |