From 8910c8c0ead7e6bea872ecd0f22d0a53cb25c40b Mon Sep 17 00:00:00 2001
From: Dag Sverre Seljebotn <dagss@student.matnat.uio.no>
Date: Thu, 29 Jan 2009 21:00:15 +0100
Subject: [PATCH] More refnanny fixes

---
 Cython/Compiler/Code.py       | 3 +++
 Cython/Compiler/ModuleNode.py | 1 +
 Cython/Compiler/Nodes.py      | 6 ++++++
 3 files changed, 10 insertions(+)

diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py
index 3ebb184c..b380a59f 100644
--- a/Cython/Compiler/Code.py
+++ b/Cython/Compiler/Code.py
@@ -749,6 +749,9 @@ class CCodeWriter(object):
     def put_xgiveref(self, cname):
         self.putln("__Pyx_XGIVEREF(%s);" % cname)
 
+    def put_xgotref(self, cname):
+        self.putln("__Pyx_XGOTREF(%s);" % cname)
+
     def put_incref(self, cname, type, nanny=True):
         if nanny:
             self.putln("__Pyx_INCREF(%s);" % self.as_pyobject(cname, type))
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py
index 19827750..c4aaf1a0 100644
--- a/Cython/Compiler/ModuleNode.py
+++ b/Cython/Compiler/ModuleNode.py
@@ -2348,4 +2348,5 @@ int __Pyx_Refnanny_FinishContext(void*);
 #define __Pyx_FinishRefcountContext() 0
 #endif /* CYTHON_REFNANNY */
 #define __Pyx_XGIVEREF(r) (r ? __Pyx_GIVEREF(r) : 0)
+#define __Pyx_XGOTREF(r) (r ? __Pyx_GOTREF(r) : 0)
 """)
diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py
index 46c11dca..0d655620 100644
--- a/Cython/Compiler/Nodes.py
+++ b/Cython/Compiler/Nodes.py
@@ -2145,6 +2145,7 @@ class DefNode(FuncDefNode):
                     self.starstar_arg.entry.cname,
                     self.starstar_arg.entry.cname,
                     self.error_value()))
+            code.put_gotref(self.starstar_arg.entry.cname)
         if self.star_arg:
             self.star_arg.entry.xdecref_cleanup = 0
             code.putln('if (PyTuple_GET_SIZE(%s) > %d) {' % (
@@ -2153,6 +2154,7 @@ class DefNode(FuncDefNode):
             code.put('%s = PyTuple_GetSlice(%s, %d, PyTuple_GET_SIZE(%s)); ' % (
                     self.star_arg.entry.cname, Naming.args_cname,
                     max_positional_args, Naming.args_cname))
+            code.put_gotref(self.star_arg.entry.cname)
             if self.starstar_arg:
                 code.putln("")
                 code.putln("if (unlikely(!%s)) {" % self.star_arg.entry.cname)
@@ -4026,6 +4028,8 @@ class TryExceptStatNode(StatNode):
                    ', '.join(['*%s' % var for var in Naming.exc_save_vars]))
         code.putln("__Pyx_ExceptionSave(%s);" %
                    ', '.join(['&%s' % var for var in Naming.exc_save_vars]))
+        for var in Naming.exc_save_vars:
+            code.put_xgotref(var)
         code.putln(
             "/*try:*/ {")
         code.return_label = try_return_label
@@ -4066,12 +4070,14 @@ class TryExceptStatNode(StatNode):
 
         if code.label_used(except_return_label):
             code.put_label(except_return_label)
+            for var in Naming.exc_save_vars: code.put_xgiveref(var)
             code.putln("__Pyx_ExceptionReset(%s);" %
                        ', '.join(Naming.exc_save_vars))
             code.put_goto(old_return_label)
 
         if code.label_used(except_end_label):
             code.put_label(except_end_label)
+            for var in Naming.exc_save_vars: code.put_xgiveref(var)
             code.putln("__Pyx_ExceptionReset(%s);" %
                        ', '.join(Naming.exc_save_vars))
         code.put_label(try_end_label)
-- 
2.26.2