igraph_write_graph_graphml() is locale dependent #930

Open
Ariki opened this Issue Mar 2, 2016 · 4 comments

Projects

None yet

2 participants

@Ariki
Ariki commented Mar 2, 2016

The function igraph_write_graph_graphml() writes floating point attribute values using locale dependent decimal separator. In locales that use comma as separator, this causes generation of locale dependent XML which can't be read back properly, even on the same system.The numbers containing a comma instead of a period are truncated when read back.

Here is a code example demonstrating the issue. This assumes that the locale using decimal comma ("ru_RU.UTF-8" in this example) presents in the system.

#include <locale.h>
#include <igraph.h>

int main(void) {
    igraph_t graph;
    igraph_i_set_attribute_table(&igraph_cattribute_table);
    igraph_full(&graph, 1, 0, 0);
    SETGAN(&graph, "myattr", 3.14);
    setlocale(LC_NUMERIC, "ru_RU.UTF-8"); // This locale uses decimal comma
    igraph_write_graph_graphml(&graph, stdout, 0); // The output contains "3,14"
    setlocale(LC_NUMERIC, "C");
    igraph_write_graph_graphml(&graph, stdout, 0); // The output contains "3.14"
    igraph_destroy(&graph);
    return 0;
}
@ntamas
Member
ntamas commented Mar 3, 2016

Note to self (for future testing and reproduction): ANSI C says that all programs start by default in the standard C locale, so this becomes a problem only if the user sets the locale using setlocale() first; it is not enough to set the LANG, LC_ALL or LC_NUMERIC environment variables.

Also, this probably affects other file formats as well, not only GraphML.

@Ariki
Ariki commented Mar 3, 2016

Yes, you are right. I've discovered the issue when using Python bindings from some exotic environment (ArcGIS ArcToolbox) which seems to set locale for me.

@ntamas
Member
ntamas commented Mar 3, 2016

Okay, you can then probably work around this from Python as follows:

import locale
locale.setlocale(LC_NUMERIC, "C")

I'll commit a fix soon(ish).

@Ariki
Ariki commented Mar 3, 2016

Yes, it works. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment