]> git.ipfire.org Git - location/libloc.git/commitdiff
country: Return NULL/None for unset attributes
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Feb 2024 16:03:09 +0000 (16:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Feb 2024 16:03:09 +0000 (16:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/country.c
src/python/country.c

index 309cee12301559beaa8d9b78bbe1748a21daf3b4..8152a89752d2ca9cb1eb61f59b762c31558fd494 100644 (file)
@@ -99,6 +99,9 @@ LOC_EXPORT const char* loc_country_get_code(struct loc_country* country) {
 }
 
 LOC_EXPORT const char* loc_country_get_continent_code(struct loc_country* country) {
+       if (!*country->continent_code)
+               return NULL;
+
        return country->continent_code;
 }
 
index 6d7ba111902ff60e96760edd2ee00bdcd6515dd6..711484658801b0dd7daa57ddc18016183e7bc12a 100644 (file)
@@ -81,6 +81,10 @@ static PyObject* Country_str(CountryObject* self) {
 static PyObject* Country_get_name(CountryObject* self) {
        const char* name = loc_country_get_name(self->country);
 
+       // Return None if no name has been set
+       if (!name)
+               Py_RETURN_NONE;
+
        return PyUnicode_FromString(name);
 }
 
@@ -99,6 +103,9 @@ static int Country_set_name(CountryObject* self, PyObject* value) {
 static PyObject* Country_get_continent_code(CountryObject* self) {
        const char* code = loc_country_get_continent_code(self->country);
 
+       if (!code)
+               Py_RETURN_NONE;
+
        return PyUnicode_FromString(code);
 }