Browse Source

Fix NameError in Python 3

This commit fixes the NameError produced by calling
 filter(lambda string: isinstance(string, basestring))
in Python 3. Because filter() is a generator, the lambda function doesn't
actually called at the point of the filter() statement (which is within
a try/except block), it gets called later and raises the error outside
the try block. With this commit, the code now identifies the relevant
string type (basestring or str) before ever using it, forcing the
NameError to get raised early in a controlled context.
David Zaslavsky 7 years ago
parent
commit
6ab2a1d757
1 changed files with 11 additions and 21 deletions
  1. 11 21
      render_math/math.py

+ 11 - 21
render_math/math.py

@@ -45,6 +45,12 @@ try:
 except ImportError as e:
     PelicanMathJaxExtension = None
 
+try:
+    string_type = basestring
+except NameError:
+    string_type = str
+
+
 def process_settings(pelicanobj):
     """Sets user specified MathJax settings (see README for more details)"""
 
@@ -90,10 +96,7 @@ def process_settings(pelicanobj):
         # and 3 of python
 
         if key == 'align':
-            try:
-                typeVal = isinstance(value, basestring)
-            except NameError:
-                typeVal = isinstance(value, str)
+            typeVal = isinstance(value, string_type)
 
             if not typeVal:
                 continue
@@ -122,10 +125,7 @@ def process_settings(pelicanobj):
             mathjax_settings[key] = 'true' if value else 'false'
 
         if key == 'latex_preview':
-            try:
-                typeVal = isinstance(value, basestring)
-            except NameError:
-                typeVal = isinstance(value, str)
+            typeVal = isinstance(value, string_type)
 
             if not typeVal:
                 continue
@@ -133,10 +133,7 @@ def process_settings(pelicanobj):
             mathjax_settings[key] = value
 
         if key == 'color':
-            try:
-                typeVal = isinstance(value, basestring)
-            except NameError:
-                typeVal = isinstance(value, str)
+            typeVal = isinstance(value, string_type)
 
             if not typeVal:
                 continue
@@ -161,19 +158,12 @@ def process_settings(pelicanobj):
 
         if key == 'tex_extensions' and isinstance(value, list):
             # filter string values, then add '' to them
-            try:
-                value = filter(lambda string: isinstance(string, basestring), value)
-            except NameError:
-                value = filter(lambda string: isinstance(string, str), value)
-
+            value = filter(lambda string: isinstance(string, string_type), value)
             value = map(lambda string: "'%s'" % string, value)
             mathjax_settings[key] = ',' + ','.join(value)
 
         if key == 'mathjax_font':
-            try:
-                typeVal = isinstance(value, basestring)
-            except NameError:
-                typeVal = isinstance(value, str)
+            typeVal = isinstance(value, string_type)
 
             if not typeVal:
                 continue