diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2025-01-14 22:48:58 +0100 |
|---|---|---|
| committer | nessita <124304+nessita@users.noreply.github.com> | 2025-01-15 13:46:06 -0300 |
| commit | 0cabed9efa2c7abd1693860069f20ec5db41fcd8 (patch) | |
| tree | 5f207223708c999071f289802171ffd035bba475 | |
| parent | b3c5830769d8a5dbf2f974da7116fe503c9454d9 (diff) | |
Simplified GeoIP2._query() when passing IPv4Address()/IPv6Address() instances.
There is no need to call validate_ipv46_address() for
ipaddress.IPv4Address()/ipaddress.IPv6Address() instances since this
relies on trying to create these kind objects from strings, so they will
always be valid.
| -rw-r--r-- | django/contrib/gis/geoip2.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/django/contrib/gis/geoip2.py b/django/contrib/gis/geoip2.py index a5fe429b89..5b510dee7f 100644 --- a/django/contrib/gis/geoip2.py +++ b/django/contrib/gis/geoip2.py @@ -153,11 +153,12 @@ class GeoIP2: if require_city and not self.is_city: raise GeoIP2Exception(f"Invalid GeoIP city data file: {self._path}") - try: - validate_ipv46_address(query) - except ValidationError: - # GeoIP2 only takes IP addresses, so try to resolve a hostname. - query = socket.gethostbyname(query) + if isinstance(query, str): + try: + validate_ipv46_address(query) + except ValidationError: + # GeoIP2 only takes IP addresses, so try to resolve a hostname. + query = socket.gethostbyname(query) function = self._reader.city if self.is_city else self._reader.country return function(query) |
