summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorEli Bendersky <eliben@gmail.com>2012-03-16 06:20:05 (GMT)
committerEli Bendersky <eliben@gmail.com>2012-03-16 06:20:05 (GMT)
commit5b77d81314bb974f212d5849b4c5ceae9e2ba142 (patch)
tree5cb5d2796e6848d6169bcf21569360c1cdd33cc1 /Modules
parentf996e775eaf22e6a6465e640a6de46ea74011bc0 (diff)
downloadcpython-5b77d81314bb974f212d5849b4c5ceae9e2ba142.zip
cpython-5b77d81314bb974f212d5849b4c5ceae9e2ba142.tar.gz
cpython-5b77d81314bb974f212d5849b4c5ceae9e2ba142.tar.bz2
Issue #14207: the ParseError exception raised by _elementtree was made
consistent to the one raised by the Python module (the 'code' attribute was added). In addition, the exception is now documented. Added a test to check that ParseError has the required attributes, and threw away the equivalent doctest which is no longer required.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_elementtree.c35
1 files changed, 27 insertions, 8 deletions
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 99935b9..a50a3e7 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -2177,13 +2177,18 @@ makeuniversal(XMLParserObject* self, const char* string)
return value;
}
+/* Set the ParseError exception with the given parameters.
+ * If message is not NULL, it's used as the error string. Otherwise, the
+ * message string is the default for the given error_code.
+*/
static void
-expat_set_error(const char* message, int line, int column)
+expat_set_error(enum XML_Error error_code, int line, int column, char *message)
{
- PyObject *errmsg, *error, *position;
+ PyObject *errmsg, *error, *position, *code;
errmsg = PyUnicode_FromFormat("%s: line %d, column %d",
- message, line, column);
+ message ? message : EXPAT(ErrorString)(error_code),
+ line, column);
if (errmsg == NULL)
return;
@@ -2192,7 +2197,19 @@ expat_set_error(const char* message, int line, int column)
if (!error)
return;
- /* add position attribute */
+ /* Add code and position attributes */
+ code = PyLong_FromLong((long)error_code);
+ if (!code) {
+ Py_DECREF(error);
+ return;
+ }
+ if (PyObject_SetAttrString(error, "code", code) == -1) {
+ Py_DECREF(error);
+ Py_DECREF(code);
+ return;
+ }
+ Py_DECREF(code);
+
position = Py_BuildValue("(ii)", line, column);
if (!position) {
Py_DECREF(error);
@@ -2244,9 +2261,10 @@ expat_default_handler(XMLParserObject* self, const XML_Char* data_in,
char message[128] = "undefined entity ";
strncat(message, data_in, data_len < 100?data_len:100);
expat_set_error(
- message,
+ XML_ERROR_UNDEFINED_ENTITY,
EXPAT(GetErrorLineNumber)(self->parser),
- EXPAT(GetErrorColumnNumber)(self->parser)
+ EXPAT(GetErrorColumnNumber)(self->parser),
+ message
);
}
@@ -2629,9 +2647,10 @@ expat_parse(XMLParserObject* self, char* data, int data_len, int final)
if (!ok) {
expat_set_error(
- EXPAT(ErrorString)(EXPAT(GetErrorCode)(self->parser)),
+ EXPAT(GetErrorCode)(self->parser),
EXPAT(GetErrorLineNumber)(self->parser),
- EXPAT(GetErrorColumnNumber)(self->parser)
+ EXPAT(GetErrorColumnNumber)(self->parser),
+ NULL
);
return NULL;
}