Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@dvarrazzo authored
Showing with 15 additions and 5 deletions.
  1. +6 −5 psycopg/cursor_type.c
  2. +9 −0 tests/test_copy.py
View
11 psycopg/cursor_type.c
@@ -1505,14 +1505,15 @@ psyco_curs_copy_expert(cursorObject *self, PyObject *args, PyObject *kwargs)
self->copyfile = file;
/* At this point, the SQL statement must be str, not unicode */
- if (pq_execute(self, Bytes_AS_STRING(sql), 0) != 1) { goto exit; }
+ if (pq_execute(self, Bytes_AS_STRING(sql), 0) == 1) {
+ res = Py_None;
+ Py_INCREF(res);
+ }
- res = Py_None;
- Py_INCREF(res);
+ self->copyfile = NULL;
+ Py_DECREF(file);
exit:
- self->copyfile = NULL;
- Py_XDECREF(file);
Py_XDECREF(sql);
return res;
View
9 tests/test_copy.py
@@ -244,6 +244,15 @@ def _copy_to(self, curs, srec):
self.assertEqual(ntests, len(string.ascii_letters))
+ def test_copy_expert_file_refcount(self):
+ class Whatever(object):
+ pass
+
+ f = Whatever()
+ curs = self.conn.cursor()
+ self.assertRaises(TypeError,
+ curs.copy_expert, 'COPY tcopy (data) FROM STDIN', f)
+
decorate_all_tests(CopyTests, skip_if_green)
Please sign in to comment.
Something went wrong with that request. Please try again.