Skip to content

Commit

Permalink
[py3] Made GeoIP tests pass with Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep committed May 10, 2013
1 parent 465a29a commit 7b00d90
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
25 changes: 11 additions & 14 deletions django/contrib/gis/geoip/base.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ def _check_query(self, query, country=False, city=False, city_or_country=False):
if not isinstance(query, six.string_types): if not isinstance(query, six.string_types):
raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__) raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__)


# GeoIP only takes ASCII-encoded strings.
query = query.encode('ascii')

# Extra checks for the existence of country and city databases. # Extra checks for the existence of country and city databases.
if city_or_country and not (self._country or self._city): if city_or_country and not (self._country or self._city):
raise GeoIPException('Invalid GeoIP country and city data files.') raise GeoIPException('Invalid GeoIP country and city data files.')
Expand All @@ -148,42 +145,42 @@ def _check_query(self, query, country=False, city=False, city_or_country=False):
elif city and not self._city: elif city and not self._city:
raise GeoIPException('Invalid GeoIP city data file: %s' % self._city_file) raise GeoIPException('Invalid GeoIP city data file: %s' % self._city_file)


# Return the query string back to the caller. # Return the query string back to the caller. GeoIP only takes bytestrings.
return query return force_bytes(query)


def city(self, query): def city(self, query):
""" """
Returns a dictionary of city information for the given IP address or Returns a dictionary of city information for the given IP address or
Fully Qualified Domain Name (FQDN). Some information in the dictionary Fully Qualified Domain Name (FQDN). Some information in the dictionary
may be undefined (None). may be undefined (None).
""" """
query = self._check_query(query, city=True) enc_query = self._check_query(query, city=True)
if ipv4_re.match(query): if ipv4_re.match(query):
# If an IP address was passed in # If an IP address was passed in
return GeoIP_record_by_addr(self._city, c_char_p(query)) return GeoIP_record_by_addr(self._city, c_char_p(enc_query))
else: else:
# If a FQDN was passed in. # If a FQDN was passed in.
return GeoIP_record_by_name(self._city, c_char_p(query)) return GeoIP_record_by_name(self._city, c_char_p(enc_query))


def country_code(self, query): def country_code(self, query):
"Returns the country code for the given IP Address or FQDN." "Returns the country code for the given IP Address or FQDN."
query = self._check_query(query, city_or_country=True) enc_query = self._check_query(query, city_or_country=True)
if self._country: if self._country:
if ipv4_re.match(query): if ipv4_re.match(query):
return GeoIP_country_code_by_addr(self._country, query) return GeoIP_country_code_by_addr(self._country, enc_query)
else: else:
return GeoIP_country_code_by_name(self._country, query) return GeoIP_country_code_by_name(self._country, enc_query)
else: else:
return self.city(query)['country_code'] return self.city(query)['country_code']


def country_name(self, query): def country_name(self, query):
"Returns the country name for the given IP Address or FQDN." "Returns the country name for the given IP Address or FQDN."
query = self._check_query(query, city_or_country=True) enc_query = self._check_query(query, city_or_country=True)
if self._country: if self._country:
if ipv4_re.match(query): if ipv4_re.match(query):
return GeoIP_country_name_by_addr(self._country, query) return GeoIP_country_name_by_addr(self._country, enc_query)
else: else:
return GeoIP_country_name_by_name(self._country, query) return GeoIP_country_name_by_name(self._country, enc_query)
else: else:
return self.city(query)['country_name'] return self.city(query)['country_name']


Expand Down
7 changes: 6 additions & 1 deletion django/contrib/gis/geoip/prototypes.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -92,15 +92,20 @@ def check_string(result, func, cargs):
free(result) free(result)
else: else:
s = '' s = ''
return s return s.decode()


GeoIP_database_info = lgeoip.GeoIP_database_info GeoIP_database_info = lgeoip.GeoIP_database_info
GeoIP_database_info.restype = geoip_char_p GeoIP_database_info.restype = geoip_char_p
GeoIP_database_info.errcheck = check_string GeoIP_database_info.errcheck = check_string


# String output routines. # String output routines.
def string_output(func): def string_output(func):
def _err_check(result, func, cargs):
if result:
return result.decode()
return result
func.restype = c_char_p func.restype = c_char_p
func.errcheck = _err_check
return func return func


GeoIP_country_code_by_addr = string_output(lgeoip.GeoIP_country_code_by_addr) GeoIP_country_code_by_addr = string_output(lgeoip.GeoIP_country_code_by_addr)
Expand Down
6 changes: 0 additions & 6 deletions django/contrib/gis/geoip/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ def test05_unicode_response(self):
d = g.city("www.osnabrueck.de") d = g.city("www.osnabrueck.de")
self.assertEqual('Osnabrück', d['city']) self.assertEqual('Osnabrück', d['city'])


def test06_unicode_query(self):
"Testing that GeoIP accepts unicode string queries, see #17059."
g = GeoIP()
d = g.country('whitehouse.gov')
self.assertEqual('US', d['country_code'])



def suite(): def suite():
s = unittest.TestSuite() s = unittest.TestSuite()
Expand Down

0 comments on commit 7b00d90

Please sign in to comment.