off_t lo = 0;
off_t hi = db->country_objects.count - 1;
+ // Check if the country code is valid
+ if (!loc_country_code_is_valid(code)) {
+ errno = EINVAL;
+ return 1;
+ }
+
#ifdef ENABLE_DEBUG
// Save start time
clock_t start = clock();
// Nothing found
*country = NULL;
- return 1;
+ return 0;
}
// Enumerator
}
static PyObject* Database_get_country(DatabaseObject* self, PyObject* args) {
+ struct loc_country* country = NULL;
const char* country_code = NULL;
if (!PyArg_ParseTuple(args, "s", &country_code))
return NULL;
- struct loc_country* country;
+ // Fetch the country
int r = loc_database_get_country(self->db, &country, country_code);
if (r) {
- Py_RETURN_NONE;
+ switch (errno) {
+ case EINVAL:
+ PyErr_SetString(PyExc_ValueError, "Invalid country code");
+ break;
+
+ default:
+ PyErr_SetFromErrno(PyExc_OSError);
+ break;
+ }
+
+ return NULL;
}
+ // No result
+ if (!country)
+ Py_RETURN_NONE;
+
PyObject* obj = new_country(&CountryType, country);
loc_country_unref(country);