From dd57b2deb22709c0de81aa1360fd3596c0f8d4b0 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Wed, 1 May 2013 13:46:19 +0200 Subject: [PATCH] handle some more cases where the error status is known to be used and clean up 'unraisable exception' code --- Cython/Compiler/Code.py | 23 +++++++++++++++++++++-- Cython/Compiler/ExprNodes.py | 2 +- Cython/Compiler/Nodes.py | 18 +++++------------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py index bd3f039..88df66a 100644 --- a/Cython/Compiler/Code.py +++ b/Cython/Compiler/Code.py @@ -1919,8 +1919,10 @@ class CCodeWriter(object): entry.name, self.error_goto(pos))) - def set_error_info(self, pos): + def set_error_info(self, pos, used=False): self.funcstate.should_declare_error_indicator = True + if used: + self.funcstate.uses_error_indicator = True if self.c_line_in_traceback: cinfo = " %s = %s;" % (Naming.clineno_cname, Naming.line_c_macro) else: @@ -1972,7 +1974,7 @@ class CCodeWriter(object): """ Build a Python traceback for propagating exceptions. - qualified_name should be the qualified name of the function + qualified_name should be the qualified name of the function. """ format_tuple = ( qualified_name, @@ -1983,6 +1985,23 @@ class CCodeWriter(object): self.funcstate.uses_error_indicator = True self.putln('__Pyx_AddTraceback("%s", %s, %s, %s);' % format_tuple) + def put_unraisable(self, qualified_name): + """ + Generate code to print a Python warning for an unraisable exception. + + qualified_name should be the qualified name of the function. + """ + format_tuple = ( + qualified_name, + Naming.clineno_cname, + Naming.lineno_cname, + Naming.filename_cname, + ) + self.funcstate.uses_error_indicator = True + self.putln('__Pyx_WriteUnraisable("%s", %s, %s, %s);' % format_tuple) + self.globalstate.use_utility_code( + UtilityCode.load_cached("WriteUnraisableException", "Exceptions.c")) + def put_trace_declarations(self): self.putln('__Pyx_TraceDeclarations') diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index f8a2bd8..f11c73a 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -9008,7 +9008,7 @@ class DivNode(NumBinopNode): self.operand1.result(), self.operand2.result())) code.put_ensure_gil() - code.putln(code.set_error_info(self.pos)) + code.putln(code.set_error_info(self.pos, used=True)) code.putln("if (__Pyx_cdivision_warning(%(FILENAME)s, " "%(LINENO)s)) {" % { 'FILENAME': Naming.filename_cname, diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index cb8bc58..b8d5b9c 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -1798,18 +1798,10 @@ class FuncDefNode(StatNode, BlockNode): code.put_release_ensured_gil() code.putln("}") else: - warning(self.entry.pos, "Unraisable exception in function '%s'." \ - % self.entry.qualified_name, 0) - format_tuple = ( - self.entry.qualified_name, - Naming.clineno_cname, - Naming.lineno_cname, - Naming.filename_cname, - ) - code.putln( - '__Pyx_WriteUnraisable("%s", %s, %s, %s);' % format_tuple) - env.use_utility_code(unraisable_exception_utility_code) - env.use_utility_code(restore_exception_utility_code) + warning(self.entry.pos, + "Unraisable exception in function '%s'." % + self.entry.qualified_name, 0) + code.put_unraisable(self.entry.qualified_name) default_retval = self.return_type.default_value if err_val is None and default_retval: err_val = default_retval @@ -7402,6 +7394,7 @@ class ParallelStatNode(StatNode, ParallelNode): code.putln("__Pyx_ErrFetch(&%s, &%s, &%s);" % self.parallel_exc) pos_info = chain(*zip(self.parallel_pos_info, self.pos_info)) + code.funcstate.uses_error_indicator = True code.putln("%s = %s; %s = %s; %s = %s;" % tuple(pos_info)) code.putln('__Pyx_GOTREF(%s);' % Naming.parallel_exc_type) @@ -8076,7 +8069,6 @@ restore_exception_utility_code = UtilityCode.load_cached("PyErrFetchRestore", "E raise_utility_code = UtilityCode.load_cached("RaiseException", "Exceptions.c") get_exception_utility_code = UtilityCode.load_cached("GetException", "Exceptions.c") swap_exception_utility_code = UtilityCode.load_cached("SwapException", "Exceptions.c") -unraisable_exception_utility_code = UtilityCode.load_cached("WriteUnraisableException", "Exceptions.c") reset_exception_utility_code = UtilityCode.load_cached("SaveResetException", "Exceptions.c") traceback_utility_code = UtilityCode.load_cached("AddTraceback", "Exceptions.c") -- 2.7.4