Skip to content

Commit bc7612e

Browse files
author
James William Pye
committed
Minor corrections and improvements.
Use a TypeError instead of a ValueError for inconsistent processors and tuple items. Document it as a gotcha(rather ambiguous description in the error message). Add comments.
1 parent ec9878b commit bc7612e

5 files changed

Lines changed: 10 additions & 13 deletions

File tree

postgresql/protocol/optimized/element3.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ _pack_tuple_data(PyObject *tup)
5252
PyErr_Format(
5353
PyExc_TypeError,
5454
"cannot serialize attribute %d, expected bytes() or None, got %s",
55-
(long) catt, PyObject_TypeName(ob)
55+
(int) catt, PyObject_TypeName(ob)
5656
);
5757
return(NULL);
5858
}
@@ -76,7 +76,7 @@ _pack_tuple_data(PyObject *tup)
7676
ob = PyTuple_GET_ITEM(tup, catt);
7777
if (ob == Py_None)
7878
{
79-
*((uint32_t *) bufpos) = 0xFFFFFFFF;
79+
*((uint32_t *) bufpos) = (uint32_t) 0xFFFFFFFFL;
8080
bufpos = bufpos + 4;
8181
}
8282
else

postgresql/protocol/optimized/module.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
(((PyTypeObject *) (ob->ob_type))->tp_name)
2424

2525
/*
26-
* buffer.c needs the message_types object from protocol.message_types.
26+
* buffer.c needs the message_types object from .protocol.message_types.
2727
* Initialized in PyInit_optimized.
2828
*/
2929
static PyObject *message_types = NULL;
@@ -40,7 +40,7 @@ static short (*local_ntohs)(short) = NULL;
4040
#include "element3.c"
4141

4242

43-
/* cpp abuse */
43+
/* cpp abuse, read up on X-Macros if you don't understand */
4444
#define mFUNC(name, typ, doc) \
4545
{#name, (PyCFunction) name, typ, PyDoc_STR(doc)},
4646
static PyMethodDef optimized_methods[] = {
@@ -68,6 +68,7 @@ PyInit_optimized(void)
6868
PyObject *fromlist, *fromstr;
6969
long l;
7070

71+
/* make some constants */
7172
if (serialize_strob == NULL)
7273
{
7374
serialize_strob = PyUnicode_FromString("serialize");

postgresql/protocol/optimized/typio.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ _process_tuple(PyObject *procs, PyObject *tup, PyObject *fail)
465465
if (len != PyTuple_GET_SIZE(procs))
466466
{
467467
PyErr_Format(
468-
PyExc_ValueError,
468+
PyExc_TypeError,
469469
"inconsistent items, %d processors and %d items in row",
470470
len,
471471
PyTuple_GET_SIZE(procs)
@@ -520,7 +520,7 @@ _process_tuple(PyObject *procs, PyObject *tup, PyObject *fail)
520520
* in order to properly generalize the failure. There are numerous,
521521
* and (sometimes) inconsistent reasons why a tuple cannot be
522522
* processed and therefore a generalized exception raised in the
523-
* context of the original is very useful.
523+
* context of the original is *very* useful.
524524
*/
525525

526526
Py_DECREF(rob);
@@ -699,10 +699,6 @@ process_chunk(PyObject *self, PyObject *args)
699699

700700
if (PyList_Check(tupc))
701701
{
702-
/*
703-
* If given a list, it's assumed that the user wants the contents
704-
* replaced. The list(tupc) will be returned.
705-
*/
706702
return(_process_chunk_from_list(procs, tupc, fail));
707703
}
708704
else

postgresql/protocol/typio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ def process_tuple(procs, tup, exception_handler):
466466
"""
467467
i = len(procs)
468468
if len(tup) != i:
469-
raise ValueError(
470-
"inconsistent items, %d processors and %d objects" %(
469+
raise TypeError(
470+
"inconsistent items, %d processors and %d items in row" %(
471471
i, len(tup)
472472
)
473473
)

postgresql/test/test_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ def funpass(procs, tup, col):
10001000
self.failUnlessRaises(TypeError, pt, "foo", "bar", funpass)
10011001
self.failUnlessRaises(TypeError, pt, (), "bar", funpass)
10021002
self.failUnlessRaises(TypeError, pt, "foo", (), funpass)
1003-
self.failUnlessRaises(ValueError, pt, (), ("foo",), funpass)
1003+
self.failUnlessRaises(TypeError, pt, (), ("foo",), funpass)
10041004

10051005
def test_pack_tuple_data(self):
10061006
pit = protocol_optimized.pack_tuple_data

0 commit comments

Comments
 (0)