Skip to content

Commit 64af05c

Browse files
committed
avoid deprecated XML_GetError{Line,Column}Number
1 parent 7f91f2e commit 64af05c

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

Modules/pyexpat.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,33 +122,39 @@ CALL_XML_HANDLER_SETTER(const struct HandlerInfo *handler_info,
122122
setter(xml_parser, xml_handler);
123123
}
124124

125+
static int
126+
set_error_code(PyObject *err, enum XML_Error code)
127+
{
128+
PyObject *v = PyLong_FromLong((long)code);
129+
int ok = v != NULL && PyObject_SetAttr(err, &_Py_ID(code), v) != -1;
130+
Py_XDECREF(v);
131+
return ok;
132+
}
133+
125134
/* Set an integer attribute on the error object; return true on success,
126135
* false on an exception.
127136
*/
128137
static int
129-
set_error_attr(PyObject *err, const char *name, int value)
138+
set_error_location(PyObject *err, const char *name, XML_Size value)
130139
{
131-
PyObject *v = PyLong_FromLong(value);
132-
133-
if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) {
134-
Py_XDECREF(v);
135-
return 0;
136-
}
137-
Py_DECREF(v);
138-
return 1;
140+
PyObject *v = PyLong_FromSize_t((size_t)value);
141+
int ok = v != NULL && PyObject_SetAttrString(err, name, v) != -1;
142+
Py_XDECREF(v);
143+
return ok;
139144
}
140145

146+
141147
static PyObject *
142-
format_xml_error(enum XML_Error code, int lineno, int column)
148+
format_xml_error(enum XML_Error code, XML_Size lineno, XML_Size column)
143149
{
144150
const char *errmsg = XML_ErrorString(code);
145151
PyUnicodeWriter *writer = PyUnicodeWriter_Create(strlen(errmsg) + 1);
146152
if (writer == NULL) {
147153
return NULL;
148154
}
149155
if (PyUnicodeWriter_Format(writer,
150-
"%s: line %i, column %i",
151-
errmsg, lineno, column) < 0)
156+
"%s: line %zu, column %zu",
157+
errmsg, (size_t)lineno, (size_t)column) < 0)
152158
{
153159
PyUnicodeWriter_Discard(writer);
154160
return NULL;
@@ -158,7 +164,7 @@ format_xml_error(enum XML_Error code, int lineno, int column)
158164

159165
static PyObject *
160166
set_xml_error(pyexpat_state *state,
161-
enum XML_Error code, int lineno, int column,
167+
enum XML_Error code, XML_Size lineno, XML_Size column,
162168
const char *errmsg)
163169
{
164170
PyObject *arg = errmsg == NULL
@@ -171,9 +177,9 @@ set_xml_error(pyexpat_state *state,
171177
Py_DECREF(arg);
172178
if (
173179
res != NULL
174-
&& set_error_attr(res, "code", code)
175-
&& set_error_attr(res, "lineno", lineno)
176-
&& set_error_attr(res, "offset", column)
180+
&& set_error_code(res, code)
181+
&& set_error_location(res, "lineno", lineno)
182+
&& set_error_location(res, "offset", column)
177183
) {
178184
PyErr_SetObject(state->error, res);
179185
}
@@ -185,8 +191,8 @@ set_xml_error(pyexpat_state *state,
185191
do { \
186192
XML_Parser parser = SELF->itself; \
187193
assert(parser != NULL); \
188-
int lineno = XML_GetErrorLineNumber(parser); \
189-
int column = XML_GetErrorColumnNumber(parser); \
194+
XML_Size lineno = XML_GetCurrentLineNumber(parser); \
195+
XML_Size column = XML_GetCurrentColumnNumber(parser); \
190196
(void)set_xml_error(state, CODE, lineno, column, ERRMSG); \
191197
} while (0)
192198

0 commit comments

Comments
 (0)