ソースを参照

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 年 前
コミット
6ab2a1d757
共有1 個のファイルを変更した11 個の追加21 個の削除を含む
  1. 11 21
      render_math/math.py

+ 11 - 21
render_math/math.py

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