summaryrefslogtreecommitdiff
path: root/source3/python
diff options
context:
space:
mode:
authorMartin Pool <mbp@samba.org>2002-10-21 07:41:08 +0000
committerMartin Pool <mbp@samba.org>2002-10-21 07:41:08 +0000
commit3b8d11fe503a1e53d7f6a7f1714014253f2b90e3 (patch)
treed22ff08f353ce8bcedecaf1f7708a8751f65365a /source3/python
parent771fc528ebf3ea6d8580b76449df974c4492aae5 (diff)
downloadsamba-3b8d11fe503a1e53d7f6a7f1714014253f2b90e3.tar.gz
samba-3b8d11fe503a1e53d7f6a7f1714014253f2b90e3.tar.bz2
samba-3b8d11fe503a1e53d7f6a7f1714014253f2b90e3.zip
Give better error messages for TypeError, which will arise if e.g. you
try to pack an Int using a string tdbpack format. (This used to be commit 6139ab3cbca3fc2969d1e578b38394b1f6aeb9c3)
Diffstat (limited to 'source3/python')
-rw-r--r--source3/python/py_tdbpack.c34
1 files changed, 24 insertions, 10 deletions
diff --git a/source3/python/py_tdbpack.c b/source3/python/py_tdbpack.c
index e5044943be..06aebe61eb 100644
--- a/source3/python/py_tdbpack.c
+++ b/source3/python/py_tdbpack.c
@@ -329,18 +329,35 @@ pytdbpack_calc_reqd_len(char *format_str,
}
+static PyObject *pytdbpack_bad_type(char ch,
+ const char *expected,
+ PyObject *val_obj)
+{
+ PyObject *r = PyObject_Repr(val_obj);
+ if (!r)
+ return NULL;
+ PyErr_Format(PyExc_TypeError,
+ "tdbpack: format '%c' requires %s, not %s",
+ ch, expected, PyString_AS_STRING(r));
+ Py_DECREF(r);
+ return val_obj;
+}
+
+
/*
- Calculate the number of bytes required to pack a single value.
-*/
+ * Calculate the number of bytes required to pack a single value. While doing
+ * this, also conduct some initial checks that the argument types are
+ * reasonable.
+ *
+ * Returns -1 on exception.
+ */
static int
pytdbpack_calc_item_len(char ch,
PyObject *val_obj)
{
if (ch == 'd' || ch == 'w') {
if (!PyInt_Check(val_obj)) {
- PyErr_Format(PyExc_TypeError,
- "tdbpack: format '%c' requires an Int",
- ch);
+ pytdbpack_bad_type(ch, "Int", val_obj);
return -1;
}
if (ch == 'w')
@@ -353,10 +370,7 @@ pytdbpack_calc_item_len(char ch,
else if (ch == 'f' || ch == 'P' || ch == 'B') {
/* nul-terminated 8-bit string */
if (!PyString_Check(val_obj)) {
- PyErr_Format(PyExc_TypeError,
- "tdbpack: format '%c' requires a String",
- ch);
- return -1;
+ pytdbpack_bad_type(ch, "String", val_obj);
}
if (ch == 'B') {
@@ -371,7 +385,7 @@ pytdbpack_calc_item_len(char ch,
}
else {
PyErr_Format(PyExc_ValueError,
- __FUNCTION__ ": format character '%c' is not supported",
+ "tdbpack: format character '%c' is not supported",
ch);
return -1;