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!
This commit is contained in:
Daniele Varrazzo 2011-06-07 01:20:25 +01:00
parent b6e710b0fc
commit 679af4a975
2 changed files with 15 additions and 5 deletions

View File

@ -1505,14 +1505,15 @@ psyco_curs_copy_expert(cursorObject *self, PyObject *args, PyObject *kwargs)
self->copyfile = file; self->copyfile = file;
/* At this point, the SQL statement must be str, not unicode */ /* 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; res = Py_None;
Py_INCREF(res); Py_INCREF(res);
}
self->copyfile = NULL;
Py_DECREF(file);
exit: exit:
self->copyfile = NULL;
Py_XDECREF(file);
Py_XDECREF(sql); Py_XDECREF(sql);
return res; return res;

View File

@ -244,6 +244,15 @@ class CopyTests(unittest.TestCase):
self.assertEqual(ntests, len(string.ascii_letters)) 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) decorate_all_tests(CopyTests, skip_if_green)