From: Michael Tremer Date: Wed, 21 Feb 2024 15:54:30 +0000 (+0000) Subject: python: Make Country hashable X-Git-Tag: 0.9.18~186 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=44f35ed1fb57a59355a638b8959f0776bd3f4302;p=people%2Fms%2Flibloc.git python: Make Country hashable Signed-off-by: Michael Tremer --- diff --git a/src/python/country.c b/src/python/country.c index 4bb6a31..5129b01 100644 --- a/src/python/country.c +++ b/src/python/country.c @@ -137,6 +137,22 @@ static PyObject* Country_richcompare(CountryObject* self, CountryObject* other, Py_RETURN_NOTIMPLEMENTED; } +static Py_hash_t Country_hash(CountryObject* self) { + PyObject* code = NULL; + Py_hash_t hash = 0; + + // Fetch the code as Python string + code = Country_get_code(self); + if (!code) + return -1; + + // Fetch the hash of that string + hash = PyObject_Hash(code); + Py_DECREF(code); + + return hash; +} + static struct PyGetSetDef Country_getsetters[] = { { "code", @@ -175,4 +191,5 @@ PyTypeObject CountryType = { .tp_repr = (reprfunc)Country_repr, .tp_str = (reprfunc)Country_str, .tp_richcompare = (richcmpfunc)Country_richcompare, + .tp_hash = (hashfunc)Country_hash, };