diff options
author | Thomas Heller <theller@ctypes.org> | 2003-04-24 16:14:27 (GMT) |
---|---|---|
committer | Thomas Heller <theller@ctypes.org> | 2003-04-24 16:14:27 (GMT) |
commit | 3457e4bd80fc2496bc3b9e1410365c97ee8dcb8f (patch) | |
tree | b34902348a94f7b52a02263e0eb1398d3f74984e /Modules/_testcapimodule.c | |
parent | 0eadaac7dc3ae49974c105ff9e8c1e98a04d7d5a (diff) | |
download | cpython-3457e4bd80fc2496bc3b9e1410365c97ee8dcb8f.zip cpython-3457e4bd80fc2496bc3b9e1410365c97ee8dcb8f.tar.gz cpython-3457e4bd80fc2496bc3b9e1410365c97ee8dcb8f.tar.bz2 |
New support functions for test_getargs2.
Theres now a separate function for each of the format codes
b, B, H, I, k, i, l, L, K.
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r-- | Modules/_testcapimodule.c | 139 |
1 files changed, 70 insertions, 69 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 83f7b83..fd16d5f 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -294,94 +294,89 @@ test_L_code(PyObject *self) #endif /* ifdef HAVE_LONG_LONG */ -/* Call PyArg_ParseTuple, and return the result as unsigned long */ +/* Functions to call PyArg_ParseTuple with integer format codes, + and return the result. +*/ static PyObject * -getargs_ul(PyObject *self, PyObject *args) +getargs_b(PyObject *self, PyObject *args) { - PyObject *ob, *result = NULL, *argtuple; - char *fmt; - unsigned long value = 0; + unsigned char value; + if (!PyArg_ParseTuple(args, "b", &value)) + return NULL; + return PyLong_FromUnsignedLong((unsigned long)value); +} - if (!PyArg_ParseTuple(args, "sO", &fmt, &ob)) +static PyObject * +getargs_B(PyObject *self, PyObject *args) +{ + unsigned char value; + if (!PyArg_ParseTuple(args, "B", &value)) return NULL; - argtuple = PyTuple_New(1); - Py_INCREF(ob); - PyTuple_SET_ITEM(argtuple, 0, ob); - if (PyArg_ParseTuple(argtuple, fmt, &value)) - result = PyLong_FromUnsignedLong(value); - Py_DECREF(argtuple); - return result; + return PyLong_FromUnsignedLong((unsigned long)value); } -/* Call PyArg_ParseTuple, and return the result as signed long */ static PyObject * -getargs_l(PyObject *self, PyObject *args) +getargs_H(PyObject *self, PyObject *args) { - PyObject *ob, *result = NULL, *argtuple; - char *fmt; + unsigned short value; + if (!PyArg_ParseTuple(args, "H", &value)) + return NULL; + return PyLong_FromUnsignedLong((unsigned long)value); +} - if (!PyArg_ParseTuple(args, "sO", &fmt, &ob)) +static PyObject * +getargs_I(PyObject *self, PyObject *args) +{ + unsigned int value; + if (!PyArg_ParseTuple(args, "I", &value)) return NULL; - argtuple = PyTuple_New(1); - Py_INCREF(ob); - PyTuple_SET_ITEM(argtuple, 0, ob); - /* It's necessary to distinguish between ints and longs, since - sizeof(int) != sizeof(long) on some (64 bit) platforms. - value must be an int for: PyArg_ParseTuple(t, 'i', &value) - value must be an long for: PyArg_ParseTuple(t, 'l', &value) - */ - if (*fmt == 'i') { - int value; - if (PyArg_ParseTuple(argtuple, fmt, &value)) - result = PyLong_FromLong(value); - } else if (*fmt == 'l') { - long value; - if (PyArg_ParseTuple(argtuple, fmt, &value)) - result = PyLong_FromLong(value); - } else { - PyErr_SetString(PyExc_TypeError, "format was not i or l"); - } - Py_DECREF(argtuple); - return result; + return PyLong_FromUnsignedLong((unsigned long)value); +} + +static PyObject * +getargs_k(PyObject *self, PyObject *args) +{ + unsigned long value; + if (!PyArg_ParseTuple(args, "k", &value)) + return NULL; + return PyLong_FromUnsignedLong(value); } -#ifdef HAVE_LONG_LONG -/* Call PyArg_ParseTuple, and return the result as signed long long */ static PyObject * -getargs_ll(PyObject *self, PyObject *args) +getargs_i(PyObject *self, PyObject *args) { - PyObject *ob, *result = NULL, *argtuple; - char *fmt; - PY_LONG_LONG value = 0; + int value; + if (!PyArg_ParseTuple(args, "i", &value)) + return NULL; + return PyLong_FromLong((long)value); +} - if (!PyArg_ParseTuple(args, "sO", &fmt, &ob)) +static PyObject * +getargs_l(PyObject *self, PyObject *args) +{ + long value; + if (!PyArg_ParseTuple(args, "l", &value)) return NULL; - argtuple = PyTuple_New(1); - Py_INCREF(ob); - PyTuple_SET_ITEM(argtuple, 0, ob); - if (PyArg_ParseTuple(argtuple, fmt, &value)) - result = PyLong_FromLongLong(value); - Py_DECREF(argtuple); - return result; + return PyLong_FromLong(value); } -/* Call PyArg_ParseTuple, and return the result as unsigned long long */ +#ifdef HAVE_LONG_LONG static PyObject * -getargs_ull(PyObject *self, PyObject *args) +getargs_L(PyObject *self, PyObject *args) { - PyObject *ob, *result = NULL, *argtuple; - char *fmt; - unsigned PY_LONG_LONG value = 0; + PY_LONG_LONG value; + if (!PyArg_ParseTuple(args, "L", &value)) + return NULL; + return PyLong_FromLongLong(value); +} - if (!PyArg_ParseTuple(args, "sO", &fmt, &ob)) +static PyObject * +getargs_K(PyObject *self, PyObject *args) +{ + unsigned PY_LONG_LONG value; + if (!PyArg_ParseTuple(args, "K", &value)) return NULL; - argtuple = PyTuple_New(1); - Py_INCREF(ob); - PyTuple_SET_ITEM(argtuple, 0, ob); - if (PyArg_ParseTuple(argtuple, fmt, &value)) - result = PyLong_FromUnsignedLongLong(value); - Py_DECREF(argtuple); - return result; + return PyLong_FromUnsignedLongLong(value); } #endif @@ -600,11 +595,17 @@ static PyMethodDef TestMethods[] = { {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, - {"getargs_ul", (PyCFunction)getargs_ul, METH_VARARGS}, + + {"getargs_b", (PyCFunction)getargs_b, METH_VARARGS}, + {"getargs_B", (PyCFunction)getargs_B, METH_VARARGS}, + {"getargs_H", (PyCFunction)getargs_H, METH_VARARGS}, + {"getargs_I", (PyCFunction)getargs_I, METH_VARARGS}, + {"getargs_k", (PyCFunction)getargs_k, METH_VARARGS}, + {"getargs_i", (PyCFunction)getargs_i, METH_VARARGS}, {"getargs_l", (PyCFunction)getargs_l, METH_VARARGS}, #ifdef HAVE_LONG_LONG - {"getargs_ll", (PyCFunction)getargs_ll, METH_VARARGS}, - {"getargs_ull", (PyCFunction)getargs_ull, METH_VARARGS}, + {"getargs_L", (PyCFunction)getargs_L, METH_VARARGS}, + {"getargs_K", (PyCFunction)getargs_K, METH_VARARGS}, {"test_longlong_api", (PyCFunction)test_longlong_api, METH_NOARGS}, {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, #endif |