[pypy-svn] r13428 - pypy/dist/pypy/translator/c

pedronis at codespeak.net pedronis at codespeak.net
Wed Jun 15 04:09:28 CEST 2005


Author: pedronis
Date: Wed Jun 15 04:09:26 2005
New Revision: 13428

Modified:
   pypy/dist/pypy/translator/c/database.py
   pypy/dist/pypy/translator/c/funcgen.py
   pypy/dist/pypy/translator/c/g_exception.h
Log:
avoid leaking exc values when converting to CPython exceptions



Modified: pypy/dist/pypy/translator/c/database.py
==============================================================================
--- pypy/dist/pypy/translator/c/database.py	(original)
+++ pypy/dist/pypy/translator/c/database.py	Wed Jun 15 04:09:26 2005
@@ -147,6 +147,13 @@
             if node.globalcontainer:
                 yield node
 
+    def get_lltype_of_exception_value(self):
+        if self.translator is not None and self.translator.rtyper is not None:
+            exceptiondata = self.translator.rtyper.getexceptiondata()
+            return exceptiondata.lltype_of_exception_value
+        else:
+            return Ptr(PyObject)
+
     def pre_include_code_lines(self):
         # generate some #defines that go before the #include to control
         # what g_exception.h does

Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py	(original)
+++ pypy/dist/pypy/translator/c/funcgen.py	Wed Jun 15 04:09:26 2005
@@ -91,12 +91,20 @@
             raise TypeError, "expr(%r)" % (v,)
 
     def error_return_value(self):
+        returnlltype = self.lltypemap(self.graph.getreturnvar())
+        return self.db.get(ErrorValue(returnlltype))
+
+    def return_with_error(self):
         if self.cpython_exc:
+            lltype_of_exception_value = self.db.get_lltype_of_exception_value();
+            exc_value_typename = self.db.gettype(lltype_of_exception_value)
             assert self.lltypemap(self.graph.getreturnvar()) == PyObjPtr
-            return 'ConvertExceptionToCPython()'
-        else:
-            returnlltype = self.lltypemap(self.graph.getreturnvar())
-            return self.db.get(ErrorValue(returnlltype))
+            yield '{'
+            yield '\t%s;' % cdecl(exc_value_typename, 'vanishing_exc_value')
+            yield '\tConvertExceptionToCPython(vanishing_exc_value);'
+            yield '\t%s' % self.db.cdecrefstmt('vanishing_exc_value', lltype_of_exception_value)
+            yield '}'
+        yield 'return %s; ' % self.error_return_value()
 
     # ____________________________________________________________
 
@@ -191,7 +199,8 @@
                     exc_cls   = self.expr(block.inputargs[0])
                     exc_value = self.expr(block.inputargs[1])
                     yield 'RaiseException(%s, %s);' % (exc_cls, exc_value)
-                    yield 'return %s;' % self.error_return_value()
+                    for line in self.return_with_error():
+                        yield line 
                 else:
                     # regular return block
                     retval = self.expr(block.inputargs[0])
@@ -287,7 +296,8 @@
                 yield 'err%d_%d:' % (blocknum[block], len(to_release))
                 err_reachable = True
             if err_reachable:
-                yield 'return %s;' % self.error_return_value()
+                for line in self.return_with_error():
+                    yield line
 
     # ____________________________________________________________
 

Modified: pypy/dist/pypy/translator/c/g_exception.h
==============================================================================
--- pypy/dist/pypy/translator/c/g_exception.h	(original)
+++ pypy/dist/pypy/translator/c/g_exception.h	Wed Jun 15 04:09:26 2005
@@ -40,7 +40,7 @@
 	rpython_exc_type = RPYTHON_TYPE_OF_EXC_INST(rpython_exc_value);
 }
 
-static PyObject *ConvertExceptionToCPython(void)
+static void _ConvertExceptionToCPython(void)
 {
 	/* XXX 1. uses officially bad fishing */
 	/* XXX 2. looks for exception classes by name, fragile */
@@ -57,11 +57,15 @@
 	else {
 		PyErr_SetString(RPythonError, clsname);
 	}
-	rpython_exc_type = NULL;    /* XXX leaks! */
-	rpython_exc_value = NULL;
-	return NULL;
 }
 
+#define ConvertExceptionToCPython(vanishing)    \
+	_ConvertExceptionToCPython();		\
+	vanishing = rpython_exc_value;		\
+	rpython_exc_type = NULL;		\
+	rpython_exc_value = NULL;
+        
+
 #define RaiseSimpleException(exc, msg)				\
 		/* XXX 1. uses officially bad fishing */	\
 		/* XXX 2. msg is ignored */			\
@@ -86,7 +90,7 @@
 	}
 #define MatchException(etype)         PyErr_ExceptionMatches(etype)
 #define ConvertExceptionFromCPython() /* nothing */
-#define ConvertExceptionToCPython()   NULL
+#define ConvertExceptionToCPython(vanishing) vanishing = NULL  
 
 #define RaiseSimpleException(exc, msg) \
 		PyErr_SetString(Py##exc, msg)    /* pun */



More information about the Pypy-commit mailing list