Skip to content

Commit 1c29ab2

Browse files
felixxmnessita
authored andcommitted
[5.1.x] 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. Backport of 0cabed9 from main.
1 parent c81669c commit 1c29ab2

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

django/contrib/gis/geoip2.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ def _query(self, query, *, require_city=False):
153153
if require_city and not self.is_city:
154154
raise GeoIP2Exception(f"Invalid GeoIP city data file: {self._path}")
155155

156-
try:
157-
validate_ipv46_address(query)
158-
except ValidationError:
159-
# GeoIP2 only takes IP addresses, so try to resolve a hostname.
160-
query = socket.gethostbyname(query)
156+
if isinstance(query, str):
157+
try:
158+
validate_ipv46_address(query)
159+
except ValidationError:
160+
# GeoIP2 only takes IP addresses, so try to resolve a hostname.
161+
query = socket.gethostbyname(query)
161162

162163
function = self._reader.city if self.is_city else self._reader.country
163164
return function(query)

0 commit comments

Comments
 (0)