Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed copyfile refcount in copy_expert

In case of early error, jumping to exit would have decref'd the borrowed
reference to file.

Issue spotted by Dave Malcolm, thanks!
  • Loading branch information...
commit 679af4a97528c140ac8e9cd85598f717b3f5cfb9 1 parent b6e710b
Daniele Varrazzo authored June 07, 2011
11  psycopg/cursor_type.c
@@ -1505,14 +1505,15 @@ psyco_curs_copy_expert(cursorObject *self, PyObject *args, PyObject *kwargs)
1505 1505
     self->copyfile = file;
1506 1506
 
1507 1507
     /* At this point, the SQL statement must be str, not unicode */
1508  
-    if (pq_execute(self, Bytes_AS_STRING(sql), 0) != 1) { goto exit; }
  1508
+    if (pq_execute(self, Bytes_AS_STRING(sql), 0) == 1) {
  1509
+        res = Py_None;
  1510
+        Py_INCREF(res);
  1511
+    }
1509 1512
 
1510  
-    res = Py_None;
1511  
-    Py_INCREF(res);
  1513
+    self->copyfile = NULL;
  1514
+    Py_DECREF(file);
1512 1515
 
1513 1516
 exit:
1514  
-    self->copyfile = NULL;
1515  
-    Py_XDECREF(file);
1516 1517
     Py_XDECREF(sql);
1517 1518
 
1518 1519
     return res;
9  tests/test_copy.py
@@ -244,6 +244,15 @@ def _copy_to(self, curs, srec):
244 244
 
245 245
         self.assertEqual(ntests, len(string.ascii_letters))
246 246
 
  247
+    def test_copy_expert_file_refcount(self):
  248
+        class Whatever(object):
  249
+            pass
  250
+
  251
+        f = Whatever()
  252
+        curs = self.conn.cursor()
  253
+        self.assertRaises(TypeError,
  254
+            curs.copy_expert, 'COPY tcopy (data) FROM STDIN', f)
  255
+
247 256
 decorate_all_tests(CopyTests, skip_if_green)
248 257
 
249 258
 

0 notes on commit 679af4a

Please sign in to comment.
Something went wrong with that request. Please try again.