From f33e3377992fe36e973688920efcaea7f4123658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horv=C3=A1t?= Date: Sat, 28 Nov 2026 15:24:08 +0000 Subject: [PATCH] fix: compare attribute names as UTF-8 during attribute combination Attribute names are passed to the C core as UTF-8, but they were compared against the Python keys with PyUnicode_IsEqualToASCIIString(). For a non-ASCII name the comparison always failed, so Graph.simplify() and Graph.contract_vertices() raised an InternalError when combining attributes, and adding vertices or edges with attribute values from the C core could overwrite the existing values of such an attribute. Use igraphmodule_PyUnicode_IsEqualToUTF8String() instead, both in the combination loop and when matching attribute records. Since that helper allocates a temporary Python string, make it return -1 with a Python exception set on failure, and raise an igraph error in all callers instead of treating the failure as a mismatch. Assisted-by: Claude Opus 5.5 --- CHANGELOG.md | 6 ++++++ src/_igraph/attributes.c | 23 +++++++++++++++++++---- src/_igraph/pyhelpers.c | 13 ++++++++++++- tests/test_attributes.py | 15 +++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50a259f12..246467248 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ - Dropped support for Python 3.9 and 3.10 as they have reached their end of life. The minimum supported Python version is now 3.11. +### Fixed + +- Attribute combination (e.g. in `Graph.simplify()` or + `Graph.contract_vertices()`) no longer fails with an internal error when an + attribute name contains non-ASCII characters. + ## 1.0.1 - 2025-12-26 ### Changed diff --git a/src/_igraph/attributes.c b/src/_igraph/attributes.c index 570e53641..c2d1bd658 100644 --- a/src/_igraph/attributes.c +++ b/src/_igraph/attributes.c @@ -177,7 +177,7 @@ int igraphmodule_PyObject_matches_attribute_record(PyObject* object, igraph_attr } if (PyUnicode_Check(object)) { - return PyUnicode_IsEqualToASCIIString(object, record->name); + return igraphmodule_PyUnicode_IsEqualToUTF8String(object, record->name); } return 0; @@ -521,6 +521,7 @@ static igraph_error_t igraphmodule_i_attribute_add_vertices( /* Extend the end of every value in the vertex hash with nv pieces of None */ PyObject *key, *value, *dict; igraph_int_t i, j, k, num_attr_entries; + int match; igraph_attribute_record_t *attr_rec; igraph_vector_bool_t added_attrs; Py_ssize_t pos = 0; @@ -550,7 +551,11 @@ static igraph_error_t igraphmodule_i_attribute_add_vertices( attr_rec = NULL; for (i = 0; i < num_attr_entries; i++) { attr_rec = igraph_attribute_record_list_get_ptr(attr, i); - if (igraphmodule_PyObject_matches_attribute_record(key, attr_rec)) { + match = igraphmodule_PyObject_matches_attribute_record(key, attr_rec); + if (match < 0) { + IGRAPH_ERROR("Cannot compare vertex attribute names.", IGRAPH_FAILURE); + } + if (match) { VECTOR(added_attrs)[i] = 1; break; } @@ -746,6 +751,7 @@ static igraph_error_t igraphmodule_i_attribute_add_edges( PyObject *key, *value, *dict; Py_ssize_t pos = 0; igraph_int_t i, j, k, ne, num_attr_entries; + int match; igraph_vector_bool_t added_attrs; igraph_attribute_record_t *attr_rec; @@ -775,7 +781,11 @@ static igraph_error_t igraphmodule_i_attribute_add_edges( attr_rec = NULL; for (i = 0; i < num_attr_entries; i++) { attr_rec = igraph_attribute_record_list_get_ptr(attr, i); - if (igraphmodule_PyObject_matches_attribute_record(key, attr_rec)) { + match = igraphmodule_PyObject_matches_attribute_record(key, attr_rec); + if (match < 0) { + IGRAPH_ERROR("Cannot compare edge attribute names.", IGRAPH_FAILURE); + } + if (match) { VECTOR(added_attrs)[i] = 1; break; } @@ -1400,6 +1410,7 @@ static igraph_error_t igraphmodule_i_attribute_combine_dicts(PyObject *dict, Py_ssize_t pos; igraph_attribute_combination_record_t* todo; Py_ssize_t i, n; + int match; if (!PyDict_Check(dict) || !PyDict_Check(newdict)) { IGRAPH_ERROR("dict or newdict are corrupted", IGRAPH_FAILURE); @@ -1438,7 +1449,11 @@ static igraph_error_t igraphmodule_i_attribute_combine_dicts(PyObject *dict, PyObject *newvalue; /* Safety check */ - if (!PyUnicode_IsEqualToASCIIString(key, todo[i].name)) { + match = igraphmodule_PyUnicode_IsEqualToUTF8String(key, todo[i].name); + if (match < 0) { + IGRAPH_ERROR("Cannot compare attribute names.", IGRAPH_FAILURE); + } + if (!match) { IGRAPH_ERROR("PyDict_Next iteration order not consistent. " "This should never happen. Please report the bug to the igraph " "developers!", IGRAPH_FAILURE); diff --git a/src/_igraph/pyhelpers.c b/src/_igraph/pyhelpers.c index 8bb97b8a9..4b809ad6f 100644 --- a/src/_igraph/pyhelpers.c +++ b/src/_igraph/pyhelpers.c @@ -195,6 +195,17 @@ char* igraphmodule_PyUnicode_CopyAsString(PyObject* string) { return result; } +/** + * Checks whether a Python string is equal to a UTF-8 encoded C string. + * + * Returns 1 if they are equal, 0 if they are not (or if the Python object is + * not a string), and -1 with a Python exception set if the C string could not + * be converted to a Python string. + * + * Do not use the result directly as a boolean, as in + * if (igraphmodule_PyUnicode_IsEqualToUTF8String(...)) { ... }; the error + * value -1 would then count as a match. Check for a negative result first. + */ int igraphmodule_PyUnicode_IsEqualToUTF8String(PyObject* py_string, const char* c_string) { PyObject* c_string_conv; @@ -205,7 +216,7 @@ int igraphmodule_PyUnicode_IsEqualToUTF8String(PyObject* py_string, c_string_conv = PyUnicode_FromString(c_string); if (c_string_conv == 0) - return 0; + return -1; result = (PyUnicode_Compare(py_string, c_string_conv) == 0); Py_DECREF(c_string_conv); diff --git a/tests/test_attributes.py b/tests/test_attributes.py index 1299cada9..8471a729e 100644 --- a/tests/test_attributes.py +++ b/tests/test_attributes.py @@ -202,6 +202,21 @@ def testCombinationConcat(self): self.assertFalse("weight2" in g.edge_attributes()) self.assertTrue(g.es["name"] == ["AB", "C", "DEF"]) + def testCombinationNonASCIIName(self): + # Attribute names are passed to the C core as UTF-8, so the names + # must be compared as UTF-8 and not as ASCII. + g = self.g + name = "caf\u00e9" + g.es[name] = [1, 2, 3, 4, 5, 6, 7] + g.simplify(combine_edges={name: "sum", None: "ignore"}) + self.assertEqual(g.edge_attributes(), [name]) + self.assertEqual(g.es[name], [3, 3, 15]) + + g = Graph.Ring(4) + g.vs[name] = [1, 2, 3, 4] + g.contract_vertices([0, 0, 1, 1], combine_attrs="sum") + self.assertEqual(g.vs[name], [3, 7]) + def testCombinationMaxMinIgnore(self): g = self.g g.es["name"] = list("ABCDEFG")