David Chambers avatar David Chambers committed f2569b0

Fixed "local variable referenced before assignment" error. Python does not have lexical scoping!

Comments (0)

Files changed (1)

settings/__init__.py

 import os
-import urllib
-import urllib2
 
 from django.conf import settings
 
     AKISMET_API_KEY
 except NameError:
     AKISMET_API_KEY = None
+else:
+    def _api_key():
+        from contextlib import closing
+        from urllib import urlencode
+        from urllib2 import Request, urlopen
+        req = Request('http://rest.akismet.com/1.1/verify-key',
+                data=urlencode({'key': AKISMET_API_KEY, 'blog': BASE_URL}),
+                headers={'User-Agent': 'Mango/%s' % mango.VERSION})
 
-if AKISMET_API_KEY:
-    def _verify_akismet_api_key():
-        req = urllib2.Request('http://rest.akismet.com/1.1/verify-key',
-                data=urllib.urlencode({'key': AKISMET_API_KEY, 'blog': BASE_URL}),
-                headers={'User-Agent': 'Mango/%s' % mango.VERSION})
-        f = urllib2.urlopen(req)
-        if f.read().strip() == 'invalid':
-            AKISMET_API_KEY = None
-            if settings.DEBUG:
-                raise InvalidSettingError('Akismet rejected the supplied API key',
-                        'Double-check AKISMET_API_KEY in `mango/settings/custom.py`.')
-        f.close()
-    _verify_akismet_api_key()
+        with closing(urlopen(req)) as page:
+            if page.read().strip() == 'invalid':
+                if settings.DEBUG:
+                    raise InvalidSettingError('Akismet rejected the supplied API key',
+                            'Double-check AKISMET_API_KEY in `mango/settings/custom.py`.')
+                return
+
+        return AKISMET_API_KEY
+
+    AKISMET_API_KEY = _api_key()
 
 BASE_URL = utf8(BASE_URL.rstrip('/'))
 
 
 UNIX_DOCUMENTS_PATH = _unix_documents_path()
 
-del dirname, mango, os, settings, urllib, urllib2 # tidy the namespace
+del dirname, mango, os, settings # tidy the namespace
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.