Changeset 909


Ignore:
Timestamp:
Apr 29, 2017, 5:57:19 AM (2 years ago)
Author:
cito
Message:

Stricter large object error checks, with unit tests

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/changelog.rst

    r905 r909  
    88- The automatic renaming of columns that are invalid as field names of
    99  named tuples now works more accurately in Python 2.6 and 3.0.
     10- Fixed error checks for unlink() and export() methods of large objects
     11  (bug report by Justin Pryzby).
    1012
    1113Version 5.0.3 (2016-12-10)
  • trunk/pgmodule.c

    r902 r909  
    17021702
    17031703        /* opens large object */
    1704         if ((fd = lo_open(self->pgcnx->cnx, self->lo_oid, mode)) < 0)
     1704        if ((fd = lo_open(self->pgcnx->cnx, self->lo_oid, mode)) == -1)
    17051705        {
    17061706                PyErr_SetString(PyExc_IOError, "Can't open large object");
     
    17721772
    17731773        if ((size = lo_read(self->pgcnx->cnx, self->lo_fd,
    1774                 PyBytes_AS_STRING((PyBytesObject *)(buffer)), size)) < 0)
     1774                PyBytes_AS_STRING((PyBytesObject *)(buffer)), size)) == -1)
    17751775        {
    17761776                PyErr_SetString(PyExc_IOError, "Error while reading");
     
    18101810        /* sends query */
    18111811        if ((size = lo_write(self->pgcnx->cnx, self->lo_fd, buffer,
    1812                                                  bufsize)) < bufsize)
     1812                                                 bufsize)) != bufsize)
    18131813        {
    18141814                PyErr_SetString(PyExc_IOError, "Buffer truncated during write");
     
    19481948
    19491949        /* runs command */
    1950         if (!lo_export(self->pgcnx->cnx, self->lo_oid, name))
     1950        if (lo_export(self->pgcnx->cnx, self->lo_oid, name) != 1)
    19511951        {
    19521952                PyErr_SetString(PyExc_IOError, "Error while exporting large object");
     
    19711971
    19721972        /* deletes the object, invalidate it on success */
    1973         if (!lo_unlink(self->pgcnx->cnx, self->lo_oid))
     1973        if (lo_unlink(self->pgcnx->cnx, self->lo_oid) != 1)
    19741974        {
    19751975                PyErr_SetString(PyExc_IOError, "Error while unlinking large object");
  • trunk/tests/test_classic_largeobj.py

    r770 r909  
    374374        unlink()
    375375        self.assertIsNone(self.obj.oid)
     376        # unlinking after object has been already unlinked
     377        self.assertRaises(pg.IntegrityError, unlink)
     378
     379    def testUnlinkInexistent(self):
     380        unlink = self.obj.unlink
     381        self.obj.open(pg.INV_WRITE)
     382        self.obj.close()
     383        self.pgcnx.query('select lo_unlink(%d)' % self.obj.oid)
     384        self.assertRaises(IOError, unlink)
    376385
    377386    def testSize(self):
     
    446455        self.assertEqual(r, data)
    447456
     457    def testExportInExistent(self):
     458        export = self.obj.export
     459        f = tempfile.NamedTemporaryFile()
     460        self.obj.open(pg.INV_WRITE)
     461        self.obj.close()
     462        self.pgcnx.query('select lo_unlink(%d)' % self.obj.oid)
     463        self.assertRaises(IOError, export, f.name)
     464        f.close()
     465
    448466
    449467if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.