Changed naked except statements to catch only subclasses of Exception.
authorIan Lewis <IanMLewis@gmail.com>
Sun, 31 Oct 2010 11:29:28 +0000 (20:29 +0900)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 7 Nov 2010 12:35:08 +0000 (13:35 +0100)
Naked except statements catch subclasses of BaseException which
can occur anywhere (i.e. KeyboardInterrupt).

Unexpected issues can occur when the exception happens during the
loading of a module. The python interpreter doesn't know about a
module's failed load and does not remove it from sys.modules. This
is particularly a problem on AppEngine where python will think the
module is loaded but in fact the module load has failed.

See: http://code.google.com/p/googleappengine/issues/detail?id=1409
Signed-off-by: Armin Ronacher <armin.ronacher@active-4.com>
jinja2/__init__.py
jinja2/compiler.py
jinja2/environment.py
jinja2/nodes.py
jinja2/sandbox.py
jinja2/utils.py

index f944e11b6c332a7cfc4c782633ecdf471e9a3f6d..dec8c27bca04a87851bf01f38aded67c9b5c5027 100644 (file)
@@ -30,7 +30,7 @@ __docformat__ = 'restructuredtext en'
 try:
     __version__ = __import__('pkg_resources') \
         .get_distribution('Jinja2').version
-except:
+except Exception:
     __version__ = 'unknown'
 
 # high level interface
index 57641596a32aadc87b43d68528a2846ea749b80f..50618a842ae32bbc070b06d862454221677bf94d 100644 (file)
@@ -1249,7 +1249,7 @@ class CodeGenerator(NodeVisitor):
                     else:
                         const = escape(const)
                 const = finalize(const)
-            except:
+            except Exception:
                 # if something goes wrong here we evaluate the node
                 # at runtime for easier debugging
                 body.append(child)
index ac74a5c68fd0d91538c16e6faaee7690502cec25..886cb30b52e577f1f213497ed27a52119e11a6cd 100644 (file)
@@ -354,7 +354,7 @@ class Environment(object):
             if isinstance(argument, basestring):
                 try:
                     attr = str(argument)
-                except:
+                except Exception:
                     pass
                 else:
                     try:
@@ -886,7 +886,7 @@ class Template(object):
         vars = dict(*args, **kwargs)
         try:
             return concat(self.root_render_func(self.new_context(vars)))
-        except:
+        except Exception:
             exc_info = sys.exc_info()
         return self.environment.handle_exception(exc_info, True)
 
@@ -908,7 +908,7 @@ class Template(object):
         try:
             for event in self.root_render_func(self.new_context(vars)):
                 yield event
-        except:
+        except Exception:
             exc_info = sys.exc_info()
         else:
             return
index 6446c70eae8439c993de01359c03709a673246a7..7c6b358a550546d2fb56edd70fbe87aca26f4a57 100644 (file)
@@ -375,7 +375,7 @@ class BinExpr(Expr):
         f = _binop_to_func[self.operator]
         try:
             return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
-        except:
+        except Exception:
             raise Impossible()
 
 
@@ -390,7 +390,7 @@ class UnaryExpr(Expr):
         f = _uaop_to_func[self.operator]
         try:
             return f(self.node.as_const(eval_ctx))
-        except:
+        except Exception:
             raise Impossible()
 
 
@@ -555,16 +555,16 @@ class Filter(Expr):
         if self.dyn_args is not None:
             try:
                 args.extend(self.dyn_args.as_const(eval_ctx))
-            except:
+            except Exception:
                 raise Impossible()
         if self.dyn_kwargs is not None:
             try:
                 kwargs.update(self.dyn_kwargs.as_const(eval_ctx))
-            except:
+            except Exception:
                 raise Impossible()
         try:
             return filter_(obj, *args, **kwargs)
-        except:
+        except Exception:
             raise Impossible()
 
 
@@ -604,16 +604,16 @@ class Call(Expr):
         if self.dyn_args is not None:
             try:
                 args.extend(self.dyn_args.as_const(eval_ctx))
-            except:
+            except Exception:
                 raise Impossible()
         if self.dyn_kwargs is not None:
             try:
                 kwargs.update(self.dyn_kwargs.as_const(eval_ctx))
-            except:
+            except Exception:
                 raise Impossible()
         try:
             return obj(*args, **kwargs)
-        except:
+        except Exception:
             raise Impossible()
 
 
@@ -628,7 +628,7 @@ class Getitem(Expr):
         try:
             return self.environment.getitem(self.node.as_const(eval_ctx),
                                             self.arg.as_const(eval_ctx))
-        except:
+        except Exception:
             raise Impossible()
 
     def can_assign(self):
@@ -648,7 +648,7 @@ class Getattr(Expr):
             eval_ctx = get_eval_context(self, eval_ctx)
             return self.environment.getattr(self.node.as_const(eval_ctx),
                                             self.attr)
-        except:
+        except Exception:
             raise Impossible()
 
     def can_assign(self):
@@ -695,7 +695,7 @@ class Compare(Expr):
                 new_value = op.expr.as_const(eval_ctx)
                 result = _cmpop_to_func[op.op](value, new_value)
                 value = new_value
-        except:
+        except Exception:
             raise Impossible()
         return result
 
index 749719548af8c69f896cf43da807f354020411ee..55817be9d1a286986b761b45ab057c94cf92450c 100644 (file)
@@ -212,7 +212,7 @@ class SandboxedEnvironment(Environment):
             if isinstance(argument, basestring):
                 try:
                     attr = str(argument)
-                except:
+                except Exception:
                     pass
                 else:
                     try:
index 7b77b8eb7d139a28c6d08891d6acb845c319180e..49e9e9ae0737dd5876b1ca67d8f00185dcad0370 100644 (file)
@@ -53,7 +53,7 @@ except TypeError, _error:
         def concat(gen):
             try:
                 return _concat(list(gen))
-            except:
+            except Exception:
                 # this hack is needed so that the current frame
                 # does not show up in the traceback.
                 exc_type, exc_value, tb = sys.exc_info()