Changeset 985 for trunk


Ignore:
Timestamp:
Apr 22, 2019, 6:07:43 PM (3 months ago)
Author:
cito
Message:

Let single() and variants raise more specific error

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/pg/query.rst

    r980 r985  
    245245    :returns: single row from the query results as a tuple of fields
    246246    :rtype: tuple
    247         :raises ProgrammingError: result does not have exactly one row
     247        :raises InvalidResultError: result does not have exactly one row
    248248    :raises TypeError: too many (any) parameters
    249249    :raises MemoryError: internal memory error
     
    252252
    253253This method returns the same single row when called multiple times.
    254 It raises a ProgrammingError if the result does not have exactly one row.
     254It raises an :exc:`pg.InvalidResultError` if the result does not have exactly
     255one row. More specifically, this will be of type :exc:`pg.NoResultError` if it
     256is empty and of type :exc:`pg.MultipleResultsError` if it has multiple rows.
    255257
    256258.. versionadded:: 5.1
     
    262264    :returns: single row from the query results as a dictionary
    263265    :rtype: dict
    264         :raises ProgrammingError: result does not have exactly one row
     266        :raises InvalidResultError: result does not have exactly one row
    265267    :raises TypeError: too many (any) parameters
    266268    :raises MemoryError: internal memory error
     
    270272
    271273This method returns the same single row when called multiple times.
    272 It raises a ProgrammingError if the result does not have exactly one row.
     274It raises an :exc:`pg.InvalidResultError` if the result does not have exactly
     275one row. More specifically, this will be of type :exc:`pg.NoResultError` if it
     276is empty and of type :exc:`pg.MultipleResultsError` if it has multiple rows.
    273277
    274278.. versionadded:: 5.1
     
    280284    :returns: single row from the query results as a named tuple
    281285    :rtype: named tuple
    282         :raises ProgrammingError: result does not have exactly one row
     286        :raises InvalidResultError: result does not have exactly one row
    283287    :raises TypeError: too many (any) parameters
    284288    :raises MemoryError: internal memory error
     
    291295
    292296This method returns the same single row when called multiple times.
    293 It raises a ProgrammingError if the result does not have exactly one row.
     297It raises an :exc:`pg.InvalidResultError` if the result does not have exactly
     298one row. More specifically, this will be of type :exc:`pg.NoResultError` if it
     299is empty and of type :exc:`pg.MultipleResultsError` if it has multiple rows.
    294300
    295301.. versionadded:: 5.1
     
    301307    :returns: single row from the query results as a scalar value
    302308    :rtype: type of first field
    303         :raises ProgrammingError: result does not have exactly one row
     309        :raises InvalidResultError: result does not have exactly one row
    304310    :raises TypeError: too many (any) parameters
    305311    :raises MemoryError: internal memory error
     
    308314
    309315This method returns the same single row as scalar when called multiple times.
    310 It raises a ProgrammingError if the result does not have exactly one row.
     316It raises an :exc:`pg.InvalidResultError` if the result does not have exactly
     317one row. More specifically, this will be of type :exc:`pg.NoResultError` if it
     318is empty and of type :exc:`pg.MultipleResultsError` if it has multiple rows.
    311319
    312320.. versionadded:: 5.1
  • trunk/pgmodule.c

    r983 r985  
    4343static PyObject *Error, *Warning, *InterfaceError, *DatabaseError,
    4444                *InternalError, *OperationalError, *ProgrammingError,
    45                 *IntegrityError, *DataError, *NotSupportedError;
     45                *IntegrityError, *DataError, *NotSupportedError,
     46                *InvalidResultError, *NoResultError, *MultipleResultsError;
    4647
    4748#define _TOSTRING(x) #x
     
    46134614"The single row from the query result is returned as a tuple of fields.\n"
    46144615"This method returns the same single row when called multiple times.\n"
    4615 "It raises a ProgrammingError if the result does not have exactly one row.\n";
     4616"It raises an InvalidResultError if the result doesn't have exactly one row,\n"
     4617"which will be of type NoResultError or MultipleResultsError specifically.\n";
    46164618
    46174619static PyObject *
     
    46214623
    46224624    if (self->max_row != 1) {
    4623         set_error_msg(ProgrammingError,
    4624             self->max_row ? "Multiple results found" : "No result found");
     4625        if (self->max_row)
     4626            set_error_msg(MultipleResultsError, "Multiple results found");
     4627        else
     4628            set_error_msg(NoResultError, "No result found");
    46254629        return NULL;
    46264630    }
     
    47284732"the field names used as the keys.\n"
    47294733"This method returns the same single row when called multiple times.\n"
    4730 "It raises a ProgrammingError if the result does not have exactly one row.\n";
     4734"It raises an InvalidResultError if the result doesn't have exactly one row,\n"
     4735"which will be of type NoResultError or MultipleResultsError specifically.\n";
    47314736
    47324737static PyObject *
     
    47364741
    47374742    if (self->max_row != 1) {
    4738         set_error_msg(ProgrammingError,
    4739             self->max_row ? "Multiple results found" : "No result found");
     4743        if (self->max_row)
     4744            set_error_msg(MultipleResultsError, "Multiple results found");
     4745        else
     4746            set_error_msg(NoResultError, "No result found");
    47404747        return NULL;
    47414748    }
     
    48174824"The single row from the query result is returned as named tuple of fields.\n"
    48184825"This method returns the same single row when called multiple times.\n"
    4819 "It raises a ProgrammingError if the result does not have exactly one row.\n";
     4826"It raises an InvalidResultError if the result doesn't have exactly one row,\n"
     4827"which will be of type NoResultError or MultipleResultsError specifically.\n";
    48204828
    48214829static PyObject *
     
    48274835
    48284836    if (self->max_row != 1) {
    4829         set_error_msg(ProgrammingError,
    4830             self->max_row ? "Multiple results found" : "No result found");
     4837        if (self->max_row)
     4838            set_error_msg(MultipleResultsError, "Multiple results found");
     4839        else
     4840            set_error_msg(NoResultError, "No result found");
    48314841        return NULL;
    48324842    }
     
    49694979"Returns the first field of the next row from the result as a scalar value.\n"
    49704980"This method returns the same single row when called multiple times.\n"
    4971 "It raises a ProgrammingError if the result does not have exactly one row.\n";
     4981"It raises an InvalidResultError if the result doesn't have exactly one row,\n"
     4982"which will be of type NoResultError or MultipleResultsError specifically.\n";
    49724983
    49734984static PyObject *
     
    49824993
    49834994    if (self->max_row != 1) {
    4984         set_error_msg(ProgrammingError,
    4985             self->max_row ? "Multiple results found" : "No result found");
     4995        if (self->max_row)
     4996            set_error_msg(MultipleResultsError, "Multiple results found");
     4997        else
     4998            set_error_msg(NoResultError, "No result found");
    49864999        return NULL;
    49875000    }
     
    62016214    PyDict_SetItemString(dict, "Warning", Warning);
    62026215
    6203     InterfaceError = PyErr_NewException("pg.InterfaceError", Error, NULL);
     6216    InterfaceError = PyErr_NewException(
     6217        "pg.InterfaceError", Error, NULL);
    62046218    PyDict_SetItemString(dict, "InterfaceError", InterfaceError);
    62056219
    6206     DatabaseError = PyErr_NewException("pg.DatabaseError", Error, NULL);
     6220    DatabaseError = PyErr_NewException(
     6221        "pg.DatabaseError", Error, NULL);
    62076222    PyDict_SetItemString(dict, "DatabaseError", DatabaseError);
    62086223
    6209     InternalError =
    6210         PyErr_NewException("pg.InternalError", DatabaseError, NULL);
     6224    InternalError = PyErr_NewException(
     6225        "pg.InternalError", DatabaseError, NULL);
    62116226    PyDict_SetItemString(dict, "InternalError", InternalError);
    62126227
    6213     OperationalError =
    6214         PyErr_NewException("pg.OperationalError", DatabaseError, NULL);
     6228    OperationalError = PyErr_NewException(
     6229        "pg.OperationalError", DatabaseError, NULL);
    62156230    PyDict_SetItemString(dict, "OperationalError", OperationalError);
    62166231
    6217     ProgrammingError =
    6218         PyErr_NewException("pg.ProgrammingError", DatabaseError, NULL);
     6232    ProgrammingError = PyErr_NewException(
     6233        "pg.ProgrammingError", DatabaseError, NULL);
    62196234    PyDict_SetItemString(dict, "ProgrammingError", ProgrammingError);
    62206235
    6221     IntegrityError =
    6222         PyErr_NewException("pg.IntegrityError", DatabaseError, NULL);
     6236    IntegrityError = PyErr_NewException(
     6237        "pg.IntegrityError", DatabaseError, NULL);
    62236238    PyDict_SetItemString(dict, "IntegrityError", IntegrityError);
    62246239
    6225     DataError = PyErr_NewException("pg.DataError", DatabaseError, NULL);
     6240    DataError = PyErr_NewException(
     6241        "pg.DataError", DatabaseError, NULL);
    62266242    PyDict_SetItemString(dict, "DataError", DataError);
    62276243
    6228     NotSupportedError =
    6229         PyErr_NewException("pg.NotSupportedError", DatabaseError, NULL);
     6244    NotSupportedError = PyErr_NewException(
     6245        "pg.NotSupportedError", DatabaseError, NULL);
    62306246    PyDict_SetItemString(dict, "NotSupportedError", NotSupportedError);
     6247
     6248    InvalidResultError = PyErr_NewException(
     6249        "pg.InvalidResultError", DataError, NULL);
     6250    PyDict_SetItemString(dict, "InvalidResultError", InvalidResultError);
     6251
     6252    NoResultError = PyErr_NewException(
     6253        "pg.NoResultError", InvalidResultError, NULL);
     6254    PyDict_SetItemString(dict, "NoResultError", NoResultError);
     6255
     6256    MultipleResultsError = PyErr_NewException(
     6257        "pg.MultipleResultsError", InvalidResultError, NULL);
     6258    PyDict_SetItemString(dict, "MultipleResultsError", MultipleResultsError);
    62316259
    62326260    /* Make the version available */
  • trunk/tests/test_classic_connection.py

    r984 r985  
    13621362        try:
    13631363            q.single()
    1364         except pg.ProgrammingError as e:
    1365             r = str(e)
     1364        except pg.InvalidResultError as e:
     1365            r = e
    13661366        else:
    13671367            r = None
    1368         self.assertEqual(r, 'No result found')
     1368        self.assertIsInstance(r, pg.NoResultError)
     1369        self.assertEqual(str(r), 'No result found')
    13691370
    13701371    def testSingleWithSingleRow(self):
     
    13811382        try:
    13821383            q.single()
    1383         except pg.ProgrammingError as e:
    1384             r = str(e)
     1384        except pg.InvalidResultError as e:
     1385            r = e
    13851386        else:
    13861387            r = None
    1387         self.assertEqual(r, 'Multiple results found')
     1388        self.assertIsInstance(r, pg.MultipleResultsError)
     1389        self.assertEqual(str(r), 'Multiple results found')
    13881390
    13891391    def testSingleDictWithEmptyQuery(self):
     
    13911393        try:
    13921394            q.singledict()
    1393         except pg.ProgrammingError as e:
    1394             r = str(e)
     1395        except pg.InvalidResultError as e:
     1396            r = e
    13951397        else:
    13961398            r = None
    1397         self.assertEqual(r, 'No result found')
     1399        self.assertIsInstance(r, pg.NoResultError)
     1400        self.assertEqual(str(r), 'No result found')
    13981401
    13991402    def testSingleDictWithSingleRow(self):
     
    14101413        try:
    14111414            q.singledict()
    1412         except pg.ProgrammingError as e:
    1413             r = str(e)
     1415        except pg.InvalidResultError as e:
     1416            r = e
    14141417        else:
    14151418            r = None
    1416         self.assertEqual(r, 'Multiple results found')
     1419        self.assertIsInstance(r, pg.MultipleResultsError)
     1420        self.assertEqual(str(r), 'Multiple results found')
    14171421
    14181422    def testSingleNamedWithEmptyQuery(self):
     
    14201424        try:
    14211425            q.singlenamed()
    1422         except pg.ProgrammingError as e:
    1423             r = str(e)
     1426        except pg.InvalidResultError as e:
     1427            r = e
    14241428        else:
    14251429            r = None
    1426         self.assertEqual(r, 'No result found')
     1430        self.assertIsInstance(r, pg.NoResultError)
     1431        self.assertEqual(str(r), 'No result found')
    14271432
    14281433    def testSingleNamedWithSingleRow(self):
     
    14431448        try:
    14441449            q.singlenamed()
    1445         except pg.ProgrammingError as e:
    1446             r = str(e)
     1450        except pg.InvalidResultError as e:
     1451            r = e
    14471452        else:
    14481453            r = None
    1449         self.assertEqual(r, 'Multiple results found')
     1454        self.assertIsInstance(r, pg.MultipleResultsError)
     1455        self.assertEqual(str(r), 'Multiple results found')
    14501456
    14511457    def testSingleScalarWithEmptyQuery(self):
     
    14531459        try:
    14541460            q.singlescalar()
    1455         except pg.ProgrammingError as e:
    1456             r = str(e)
     1461        except pg.InvalidResultError as e:
     1462            r = e
    14571463        else:
    14581464            r = None
    1459         self.assertEqual(r, 'No result found')
     1465        self.assertIsInstance(r, pg.NoResultError)
     1466        self.assertEqual(str(r), 'No result found')
    14601467
    14611468    def testSingleScalarWithSingleRow(self):
     
    14721479        try:
    14731480            q.singlescalar()
    1474         except pg.ProgrammingError as e:
    1475             r = str(e)
     1481        except pg.InvalidResultError as e:
     1482            r = e
    14761483        else:
    14771484            r = None
    1478         self.assertEqual(r, 'Multiple results found')
     1485        self.assertIsInstance(r, pg.MultipleResultsError)
     1486        self.assertEqual(str(r), 'Multiple results found')
    14791487
    14801488    def testScalarResult(self):
  • trunk/tests/test_classic_functions.py

    r980 r985  
    6666    def testhasPgNotSupportedError(self):
    6767        self.assertTrue(issubclass(pg.NotSupportedError, pg.DatabaseError))
     68
     69    def testhasPgInvalidResultError(self):
     70        self.assertTrue(issubclass(pg.InvalidResultError, pg.DataError))
     71
     72    def testhasPgNoResultError(self):
     73        self.assertTrue(issubclass(pg.NoResultError, pg.InvalidResultError))
     74
     75    def testhasPgMultipleResultsError(self):
     76        self.assertTrue(
     77            issubclass(pg.MultipleResultsError, pg.InvalidResultError))
    6878
    6979    def testhasConnect(self):
Note: See TracChangeset for help on using the changeset viewer.