Skip to content

Commit d1da6d6

Browse files
committed
Migrate py_id_code into pyexpat_state
Idea by @picnixz
1 parent 98cb5cd commit d1da6d6

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Modules/pyexpat.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ enum HandlerTypes {
5454
typedef struct {
5555
PyTypeObject *xml_parse_type;
5656
PyObject *error;
57+
PyObject *str_code;
5758
} pyexpat_state;
5859

5960
static inline pyexpat_state*
@@ -107,13 +108,11 @@ struct HandlerInfo {
107108
static struct HandlerInfo handler_info[64];
108109

109110
static int
110-
set_xml_error_attr_code(PyObject *err, enum XML_Error code)
111+
set_xml_error_attr_code(pyexpat_state *state, PyObject *err, enum XML_Error code)
111112
{
112-
PyObject *py_id_code = PyUnicode_InternFromString("code");
113113
PyObject *v = PyLong_FromLong((long)code);
114-
int ok = v != NULL && PyObject_SetAttr(err, py_id_code, v) != -1;
114+
int ok = v != NULL && PyObject_SetAttr(err, state->str_code, v) != -1;
115115
Py_XDECREF(v);
116-
Py_XDECREF(py_id_code);
117116
return ok;
118117
}
119118

@@ -153,7 +152,7 @@ set_xml_error(pyexpat_state *state,
153152
Py_DECREF(arg);
154153
if (
155154
res != NULL
156-
&& set_xml_error_attr_code(res, code)
155+
&& set_xml_error_attr_code(state, res, code)
157156
&& set_xml_error_attr_location(res, "lineno", lineno)
158157
&& set_xml_error_attr_location(res, "offset", column)
159158
) {
@@ -2061,6 +2060,11 @@ pyexpat_exec(PyObject *mod)
20612060
return -1;
20622061
}
20632062

2063+
state->str_code = PyUnicode_InternFromString("code");
2064+
if (state->str_code == NULL) {
2065+
return -1;
2066+
}
2067+
20642068
/* Add some symbolic constants to the module */
20652069

20662070
if (PyModule_AddObjectRef(mod, "error", state->error) < 0) {
@@ -2196,6 +2200,7 @@ pyexpat_clear(PyObject *module)
21962200
pyexpat_state *state = pyexpat_get_state(module);
21972201
Py_CLEAR(state->xml_parse_type);
21982202
Py_CLEAR(state->error);
2203+
Py_CLEAR(state->str_code);
21992204
return 0;
22002205
}
22012206

0 commit comments

Comments
 (0)