cython-devel
changeset 1886:8db1388bb844
generate shorter code for empty except blocks (ticket #251)
| author | Stefan Behnel <scoder@users.berlios.de> |
|---|---|
| date | Sun Mar 22 19:49:00 2009 +0100 (2 years ago) |
| parents | 651a88137be7 |
| children | ae066635bb30496a6ca20618 |
| files | Cython/Compiler/Nodes.py |
line diff
1.1 --- a/Cython/Compiler/Nodes.py Fri Mar 20 17:51:46 2009 +0100
1.2 +++ b/Cython/Compiler/Nodes.py Sun Mar 22 19:49:00 2009 +0100
1.3 @@ -4218,6 +4218,7 @@
1.4
1.5 exc_value = None
1.6 excinfo_target = None
1.7 + empty_body = False
1.8
1.9 def analyse_declarations(self, env):
1.10 if self.target:
1.11 @@ -4236,7 +4237,18 @@
1.12 self.match_flag = env.allocate_temp(PyrexTypes.c_int_type)
1.13 self.pattern.release_temp(env)
1.14 env.release_temp(self.match_flag)
1.15 - self.exc_vars = [env.allocate_temp(py_object_type) for i in xrange(3)]
1.16 +
1.17 + # most simple case: empty body (pass)
1.18 + self.empty_body = not self.target and not self.excinfo_target and \
1.19 + not getattr(self.body, 'stats', True)
1.20 +
1.21 + if not self.empty_body:
1.22 + self.exc_vars = [env.allocate_temp(py_object_type) for i in xrange(3)]
1.23 + env.use_utility_code(get_exception_utility_code)
1.24 + env.use_utility_code(restore_exception_utility_code)
1.25 + else:
1.26 + self.exc_vars = []
1.27 +
1.28 if self.target:
1.29 self.exc_value = ExprNodes.ExcValueNode(self.pos, env, self.exc_vars[1])
1.30 self.exc_value.allocate_temps(env)
1.31 @@ -4255,8 +4267,6 @@
1.32 self.body.analyse_expressions(env)
1.33 for var in self.exc_vars:
1.34 env.release_temp(var)
1.35 - env.use_utility_code(get_exception_utility_code)
1.36 - env.use_utility_code(restore_exception_utility_code)
1.37
1.38 def generate_handling_code(self, code, end_label):
1.39 code.mark_pos(self.pos)
1.40 @@ -4273,6 +4283,14 @@
1.41 self.match_flag)
1.42 else:
1.43 code.putln("/*except:*/ {")
1.44 +
1.45 + if self.empty_body:
1.46 + # most simple case: reset the exception state, done
1.47 + code.putln("PyErr_Restore(0,0,0);")
1.48 + code.put_goto(end_label)
1.49 + code.putln("}")
1.50 + return
1.51 +
1.52 code.putln('__Pyx_AddTraceback("%s");' % self.function_name)
1.53 # We always have to fetch the exception value even if
1.54 # there is no target, because this also normalises the
