Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[py3] Made GeoIP tests pass with Python 3

  • Loading branch information...
commit 7b00d90208e998debc60e51cadb352645c91e3fd 1 parent 465a29a
Claude Paroz authored May 10, 2013
25  django/contrib/gis/geoip/base.py
@@ -137,9 +137,6 @@ def _check_query(self, query, country=False, city=False, city_or_country=False):
137 137
         if not isinstance(query, six.string_types):
138 138
             raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__)
139 139
 
140  
-        # GeoIP only takes ASCII-encoded strings.
141  
-        query = query.encode('ascii')
142  
-
143 140
         # Extra checks for the existence of country and city databases.
144 141
         if city_or_country and not (self._country or self._city):
145 142
             raise GeoIPException('Invalid GeoIP country and city data files.')
@@ -148,8 +145,8 @@ def _check_query(self, query, country=False, city=False, city_or_country=False):
148 145
         elif city and not self._city:
149 146
             raise GeoIPException('Invalid GeoIP city data file: %s' % self._city_file)
150 147
 
151  
-        # Return the query string back to the caller.
152  
-        return query
  148
+        # Return the query string back to the caller. GeoIP only takes bytestrings.
  149
+        return force_bytes(query)
153 150
 
154 151
     def city(self, query):
155 152
         """
@@ -157,33 +154,33 @@ def city(self, query):
157 154
         Fully Qualified Domain Name (FQDN).  Some information in the dictionary
158 155
         may be undefined (None).
159 156
         """
160  
-        query = self._check_query(query, city=True)
  157
+        enc_query = self._check_query(query, city=True)
161 158
         if ipv4_re.match(query):
162 159
             # If an IP address was passed in
163  
-            return GeoIP_record_by_addr(self._city, c_char_p(query))
  160
+            return GeoIP_record_by_addr(self._city, c_char_p(enc_query))
164 161
         else:
165 162
             # If a FQDN was passed in.
166  
-            return GeoIP_record_by_name(self._city, c_char_p(query))
  163
+            return GeoIP_record_by_name(self._city, c_char_p(enc_query))
167 164
 
168 165
     def country_code(self, query):
169 166
         "Returns the country code for the given IP Address or FQDN."
170  
-        query = self._check_query(query, city_or_country=True)
  167
+        enc_query = self._check_query(query, city_or_country=True)
171 168
         if self._country:
172 169
             if ipv4_re.match(query):
173  
-                return GeoIP_country_code_by_addr(self._country, query)
  170
+                return GeoIP_country_code_by_addr(self._country, enc_query)
174 171
             else:
175  
-                return GeoIP_country_code_by_name(self._country, query)
  172
+                return GeoIP_country_code_by_name(self._country, enc_query)
176 173
         else:
177 174
             return self.city(query)['country_code']
178 175
 
179 176
     def country_name(self, query):
180 177
         "Returns the country name for the given IP Address or FQDN."
181  
-        query = self._check_query(query, city_or_country=True)
  178
+        enc_query = self._check_query(query, city_or_country=True)
182 179
         if self._country:
183 180
             if ipv4_re.match(query):
184  
-                return GeoIP_country_name_by_addr(self._country, query)
  181
+                return GeoIP_country_name_by_addr(self._country, enc_query)
185 182
             else:
186  
-                return GeoIP_country_name_by_name(self._country, query)
  183
+                return GeoIP_country_name_by_name(self._country, enc_query)
187 184
         else:
188 185
             return self.city(query)['country_name']
189 186
 
7  django/contrib/gis/geoip/prototypes.py
@@ -92,7 +92,7 @@ def check_string(result, func, cargs):
92 92
         free(result)
93 93
     else:
94 94
         s = ''
95  
-    return s
  95
+    return s.decode()
96 96
 
97 97
 GeoIP_database_info = lgeoip.GeoIP_database_info
98 98
 GeoIP_database_info.restype = geoip_char_p
@@ -100,7 +100,12 @@ def check_string(result, func, cargs):
100 100
 
101 101
 # String output routines.
102 102
 def string_output(func):
  103
+    def _err_check(result, func, cargs):
  104
+        if result:
  105
+            return result.decode()
  106
+        return result
103 107
     func.restype = c_char_p
  108
+    func.errcheck = _err_check
104 109
     return func
105 110
 
106 111
 GeoIP_country_code_by_addr = string_output(lgeoip.GeoIP_country_code_by_addr)
6  django/contrib/gis/geoip/tests.py
@@ -106,12 +106,6 @@ def test05_unicode_response(self):
106 106
         d = g.city("www.osnabrueck.de")
107 107
         self.assertEqual('Osnabrück', d['city'])
108 108
 
109  
-    def test06_unicode_query(self):
110  
-        "Testing that GeoIP accepts unicode string queries, see #17059."
111  
-        g = GeoIP()
112  
-        d = g.country('whitehouse.gov')
113  
-        self.assertEqual('US', d['country_code'])
114  
-
115 109
 
116 110
 def suite():
117 111
     s = unittest.TestSuite()

0 notes on commit 7b00d90

Please sign in to comment.
Something went wrong with that request. Please try again.