diff options
author | Georg Brandl <georg@python.org> | 2010-10-15 14:46:48 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-10-15 14:46:48 (GMT) |
commit | b4dac71a8768888dbe3de007780cd5467e48f0f4 (patch) | |
tree | c44b7b9544d5953a767a74a8bad038a5f99b4284 /Modules | |
parent | f668df5fa7e2048f4b17e98882f2a21405b6bdb4 (diff) | |
download | cpython-b4dac71a8768888dbe3de007780cd5467e48f0f4.zip cpython-b4dac71a8768888dbe3de007780cd5467e48f0f4.tar.gz cpython-b4dac71a8768888dbe3de007780cd5467e48f0f4.tar.bz2 |
#5355: Provide mappings from Expat error numbers to string descriptions and backwards, in order to actually make it possible to analyze error codes provided by ExpatError.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/pyexpat.c | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index e603984..a8b0f8f 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -1685,8 +1685,12 @@ MODULE_INITFUNC(void) PyObject *modelmod_name; PyObject *model_module; PyObject *sys_modules; + PyObject *tmpnum, *tmpstr; + PyObject *codes_dict; + PyObject *rev_codes_dict; + int res; static struct PyExpat_CAPI capi; - PyObject* capi_object; + PyObject *capi_object; if (errmod_name == NULL) return NULL; @@ -1789,9 +1793,29 @@ MODULE_INITFUNC(void) } #endif + codes_dict = PyDict_New(); + rev_codes_dict = PyDict_New(); + if (codes_dict == NULL || rev_codes_dict == NULL) { + Py_XDECREF(codes_dict); + Py_XDECREF(rev_codes_dict); + return NULL; + } + #define MYCONST(name) \ - PyModule_AddStringConstant(errors_module, #name, \ - (char*)XML_ErrorString(name)) + if (PyModule_AddStringConstant(errors_module, #name, \ + (char *)XML_ErrorString(name)) < 0) \ + return NULL; \ + tmpnum = PyLong_FromLong(name); \ + if (tmpnum == NULL) return NULL; \ + res = PyDict_SetItemString(codes_dict, \ + XML_ErrorString(name), tmpnum); \ + if (res < 0) return NULL; \ + tmpstr = PyUnicode_FromString(XML_ErrorString(name)); \ + if (tmpstr == NULL) return NULL; \ + res = PyDict_SetItem(rev_codes_dict, tmpnum, tmpstr); \ + Py_DECREF(tmpstr); \ + Py_DECREF(tmpnum); \ + if (res < 0) return NULL; \ MYCONST(XML_ERROR_NO_MEMORY); MYCONST(XML_ERROR_SYNTAX); @@ -1833,9 +1857,16 @@ MODULE_INITFUNC(void) MYCONST(XML_ERROR_FINISHED); MYCONST(XML_ERROR_SUSPEND_PE); - PyModule_AddStringConstant(errors_module, "__doc__", - "Constants used to describe error conditions."); + if (PyModule_AddStringConstant(errors_module, "__doc__", + "Constants used to describe " + "error conditions.") < 0) + return NULL; + if (PyModule_AddObject(errors_module, "codes", codes_dict) < 0) + return NULL; + if (PyModule_AddObject(errors_module, "messages", rev_codes_dict) < 0) + return NULL; + #undef MYCONST #define MYCONST(c) PyModule_AddIntConstant(m, #c, c) |