[issue30195] writing non-ascii characters in xml file using python code embedded in C
New submission from mahboubi: my python code embedded in C program, uses etree from lxml to write a plain string as element attribute in xml file. the problem is when my string contains non english characters(non ascii), the program fails to write even with unicode conversion such as unicode(mystring, "utf-8"), but when I use python code only, it works. -- components: XML messages: 292521 nosy: aimad, benjamin.peterson, ezio.melotti, haypo, lemburg priority: normal severity: normal status: open title: writing non-ascii characters in xml file using python code embedded in C type: crash versions: Python 2.7 ___ Python tracker <http://bugs.python.org/issue30195> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30195] writing non-ascii characters in xml file using python code embedded in C
mahboubi added the comment: I have just tried to do this task using xml.etree.ElementTree and still have the same problem. In the file 'write_to_xml1.py' I'm trying to develop some function that creates an xml file and then add data containing non ascii characters.The python program works perfectly, but when I tried to call these functions from C, the program crashes.Note that this problem dosn't happen when adding ascii characters only.Here is the C program : void create_report() { PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs; // Initialize the Python Interpreter Py_Initialize(); // Build the name object pName = PyString_FromString("write_to_xml1"); // Load the module object pModule = PyImport_Import(pName); // pDict is a borrowed reference pDict = PyModule_GetDict(pModule); // pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict,"create_report"); if (PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc,NULL); } else { PyErr_Print(); } } void modif_report() { PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs; // Initialize the Python Interpreter Py_Initialize(); // Build the name object pName = PyString_FromString("write_to_xml1"); // Load the module object pModule = PyImport_Import(pName); // pDict is a borrowed reference pDict = PyModule_GetDict(pModule); // pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict,"traite"); if (PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc,NULL); } else { PyErr_Print(); } } int main(int argc, char *argv[]) { create_report(); modif_report(); return(0); } // end main() -- status: pending -> open Added file: http://bugs.python.org/file46841/write_to_xml1.py ___ Python tracker <http://bugs.python.org/issue30195> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30195] writing non-ascii characters in xml file using python code embedded in C
mahboubi added the comment: Thank you xiang zhang for your reply. I think that the problem is not in xml_file.write(doc.toprettyxml()) because it works using python only, and the C problem dosen't work since you didn't get any 'alerte' balise. -- status: pending -> open ___ Python tracker <http://bugs.python.org/issue30195> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30195] writing non-ascii characters in xml file using python code embedded in C
mahboubi added the comment: Also, using codecs.open(encoding='utf8') gives unhadled exception in C? but no problem using python code only. -- ___ Python tracker <http://bugs.python.org/issue30195> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30195] writing non-ascii characters in xml file using python code embedded in C
mahboubi added the comment: Probably it's my fault to use the word 'crash', what I mean is that generated report by python program contains the right result(the 'alerte' balise is added) but the other report generated by the same python program embedde in C didn't give the right result (report is created but no 'alerte' balise is added) -- ___ Python tracker <http://bugs.python.org/issue30195> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30195] writing non-ascii characters in xml file using python code embedded in C
Changes by mahboubi : -- type: crash -> behavior ___ Python tracker <http://bugs.python.org/issue30195> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30195] writing non-ascii characters in xml file using python code embedded in C
mahboubi added the comment: problem solved, It's not related to embedding python in C, Ijust had to use mystring.decode('utf-8') instead of unicode(mystring,"utf-8") -- stage: test needed -> resolved status: open -> closed ___ Python tracker <http://bugs.python.org/issue30195> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com