SF patch 660559: Use METH_O and METH_NOARGS where possible

Simplify code and speed access by using PyArg_UnpackTuple, METH_O and
METH_NOARGS in three modules that can benefit from it.
This commit is contained in:
Raymond Hettinger 2003-01-03 08:24:58 +00:00
parent f349e26e4d
commit 36cd2bf459
3 changed files with 70 additions and 117 deletions

View File

@ -800,14 +800,11 @@ ins(arrayobject *self, int where, PyObject *v)
}
static PyObject *
array_count(arrayobject *self, PyObject *args)
array_count(arrayobject *self, PyObject *v)
{
int count = 0;
int i;
PyObject *v;
if (!PyArg_ParseTuple(args, "O:count", &v))
return NULL;
for (i = 0; i < self->ob_size; i++) {
PyObject *selfi = getarrayitem((PyObject *)self, i);
int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
@ -826,13 +823,10 @@ PyDoc_STRVAR(count_doc,
Return number of occurences of x in the array.");
static PyObject *
array_index(arrayobject *self, PyObject *args)
array_index(arrayobject *self, PyObject *v)
{
int i;
PyObject *v;
if (!PyArg_ParseTuple(args, "O:index", &v))
return NULL;
for (i = 0; i < self->ob_size; i++) {
PyObject *selfi = getarrayitem((PyObject *)self, i);
int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
@ -853,13 +847,10 @@ PyDoc_STRVAR(index_doc,
Return index of first occurence of x in the array.");
static PyObject *
array_remove(arrayobject *self, PyObject *args)
array_remove(arrayobject *self, PyObject *v)
{
int i;
PyObject *v;
if (!PyArg_ParseTuple(args, "O:remove", &v))
return NULL;
for (i = 0; i < self->ob_size; i++) {
PyObject *selfi = getarrayitem((PyObject *)self,i);
int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
@ -915,12 +906,8 @@ PyDoc_STRVAR(pop_doc,
Return the i-th element and delete it from the array. i defaults to -1.");
static PyObject *
array_extend(arrayobject *self, PyObject *args)
array_extend(arrayobject *self, PyObject *bb)
{
PyObject *bb;
if (!PyArg_ParseTuple(args, "O:extend", &bb))
return NULL;
if (array_do_extend(self, bb) == -1)
return NULL;
Py_INCREF(Py_None);
@ -949,11 +936,9 @@ Insert a new item x into the array before position i.");
static PyObject *
array_buffer_info(arrayobject *self, PyObject *args)
array_buffer_info(arrayobject *self, PyObject *unused)
{
PyObject* retval = NULL;
if (!PyArg_ParseTuple(args, ":buffer_info"))
return NULL;
retval = PyTuple_New(2);
if (!retval)
return NULL;
@ -974,11 +959,8 @@ the buffer length in bytes.");
static PyObject *
array_append(arrayobject *self, PyObject *args)
array_append(arrayobject *self, PyObject *v)
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:append", &v))
return NULL;
return ins(self, (int) self->ob_size, v);
}
@ -989,14 +971,11 @@ Append new value x to the end of the array.");
static PyObject *
array_byteswap(arrayobject *self, PyObject *args)
array_byteswap(arrayobject *self, PyObject *unused)
{
char *p;
int i;
if (!PyArg_ParseTuple(args, ":byteswap"))
return NULL;
switch (self->ob_descr->itemsize) {
case 1:
break;
@ -1049,7 +1028,7 @@ Byteswap all items of the array. If the items in the array are not 1, 2,\n\
4, or 8 bytes in size, RuntimeError is raised.");
static PyObject *
array_reverse(arrayobject *self, PyObject *args)
array_reverse(arrayobject *self, PyObject *unused)
{
register int itemsize = self->ob_descr->itemsize;
register char *p, *q;
@ -1057,9 +1036,6 @@ array_reverse(arrayobject *self, PyObject *args)
char tmp[256]; /* 8 is probably enough -- but why skimp */
assert(itemsize <= sizeof(tmp));
if (!PyArg_ParseTuple(args, ":reverse"))
return NULL;
if (self->ob_size > 1) {
for (p = self->ob_item,
q = self->ob_item + (self->ob_size - 1)*itemsize;
@ -1138,12 +1114,10 @@ array. Also called as read.");
static PyObject *
array_tofile(arrayobject *self, PyObject *args)
array_tofile(arrayobject *self, PyObject *f)
{
PyObject *f;
FILE *fp;
if (!PyArg_ParseTuple(args, "O:tofile", &f))
return NULL;
fp = PyFile_AsFile(f);
if (fp == NULL) {
PyErr_SetString(PyExc_TypeError, "arg must be open file");
@ -1169,13 +1143,11 @@ write.");
static PyObject *
array_fromlist(arrayobject *self, PyObject *args)
array_fromlist(arrayobject *self, PyObject *list)
{
int n;
PyObject *list;
int itemsize = self->ob_descr->itemsize;
if (!PyArg_ParseTuple(args, "O:fromlist", &list))
return NULL;
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, "arg must be list");
return NULL;
@ -1214,12 +1186,11 @@ Append items to array from list.");
static PyObject *
array_tolist(arrayobject *self, PyObject *args)
array_tolist(arrayobject *self, PyObject *unused)
{
PyObject *list = PyList_New(self->ob_size);
int i;
if (!PyArg_ParseTuple(args, ":tolist"))
return NULL;
if (list == NULL)
return NULL;
for (i = 0; i < self->ob_size; i++) {
@ -1277,10 +1248,8 @@ values,as if it had been read from a file using the fromfile() method).");
static PyObject *
array_tostring(arrayobject *self, PyObject *args)
array_tostring(arrayobject *self, PyObject *unused)
{
if (!PyArg_ParseTuple(args, ":tostring"))
return NULL;
return PyString_FromStringAndSize(self->ob_item,
self->ob_size * self->ob_descr->itemsize);
}
@ -1335,10 +1304,8 @@ append Unicode data to an array of some other type.");
static PyObject *
array_tounicode(arrayobject *self, PyObject *args)
array_tounicode(arrayobject *self, PyObject *unused)
{
if (!PyArg_ParseTuple(args, ":tounicode"))
return NULL;
if (self->ob_descr->typecode != 'u') {
PyErr_SetString(PyExc_ValueError,
"tounicode() may only be called on type 'u' arrays");
@ -1380,19 +1347,19 @@ static PyGetSetDef array_getsets [] = {
};
PyMethodDef array_methods[] = {
{"append", (PyCFunction)array_append, METH_VARARGS,
{"append", (PyCFunction)array_append, METH_O,
append_doc},
{"buffer_info", (PyCFunction)array_buffer_info, METH_VARARGS,
{"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
buffer_info_doc},
{"byteswap", (PyCFunction)array_byteswap, METH_VARARGS,
{"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
byteswap_doc},
{"count", (PyCFunction)array_count, METH_VARARGS,
{"count", (PyCFunction)array_count, METH_O,
count_doc},
{"extend", (PyCFunction)array_extend, METH_VARARGS,
{"extend", (PyCFunction)array_extend, METH_O,
extend_doc},
{"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
fromfile_doc},
{"fromlist", (PyCFunction)array_fromlist, METH_VARARGS,
{"fromlist", (PyCFunction)array_fromlist, METH_O,
fromlist_doc},
{"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
fromstring_doc},
@ -1400,7 +1367,7 @@ PyMethodDef array_methods[] = {
{"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
fromunicode_doc},
#endif
{"index", (PyCFunction)array_index, METH_VARARGS,
{"index", (PyCFunction)array_index, METH_O,
index_doc},
{"insert", (PyCFunction)array_insert, METH_VARARGS,
insert_doc},
@ -1408,23 +1375,23 @@ PyMethodDef array_methods[] = {
pop_doc},
{"read", (PyCFunction)array_fromfile, METH_VARARGS,
fromfile_doc},
{"remove", (PyCFunction)array_remove, METH_VARARGS,
{"remove", (PyCFunction)array_remove, METH_O,
remove_doc},
{"reverse", (PyCFunction)array_reverse, METH_VARARGS,
{"reverse", (PyCFunction)array_reverse, METH_NOARGS,
reverse_doc},
/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
sort_doc},*/
{"tofile", (PyCFunction)array_tofile, METH_VARARGS,
{"tofile", (PyCFunction)array_tofile, METH_O,
tofile_doc},
{"tolist", (PyCFunction)array_tolist, METH_VARARGS,
{"tolist", (PyCFunction)array_tolist, METH_NOARGS,
tolist_doc},
{"tostring", (PyCFunction)array_tostring, METH_VARARGS,
{"tostring", (PyCFunction)array_tostring, METH_NOARGS,
tostring_doc},
#ifdef Py_USING_UNICODE
{"tounicode", (PyCFunction)array_tounicode, METH_VARARGS,
{"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
tounicode_doc},
#endif
{"write", (PyCFunction)array_tofile, METH_VARARGS,
{"write", (PyCFunction)array_tofile, METH_O,
tofile_doc},
{NULL, NULL} /* sentinel */
};
@ -1444,18 +1411,16 @@ array_repr(arrayobject *a)
}
if (typecode == 'c' || typecode == 'u') {
PyObject *t_empty = PyTuple_New(0);
PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
s = PyString_FromString(buf);
#ifdef Py_USING_UNICODE
if (typecode == 'c')
#endif
v = array_tostring(a, t_empty);
v = array_tostring(a, NULL);
#ifdef Py_USING_UNICODE
else
v = array_tounicode(a, t_empty);
v = array_tounicode(a, NULL);
#endif
Py_DECREF(t_empty);
t = PyObject_Repr(v);
Py_XDECREF(v);
PyString_ConcatAndDel(&s, t);

View File

@ -87,10 +87,9 @@ IO__opencheck(IOobject *self) {
}
static PyObject *
IO_flush(IOobject *self, PyObject *args) {
IO_flush(IOobject *self, PyObject *unused) {
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_ParseTuple(args, ":flush")) return NULL;
Py_INCREF(Py_None);
return Py_None;
@ -115,7 +114,7 @@ IO_getval(IOobject *self, PyObject *args) {
int s;
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_ParseTuple(args,"|O:getval",&use_pos)) return NULL;
UNLESS (PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
if (PyObject_IsTrue(use_pos)) {
s=self->pos;
@ -129,10 +128,7 @@ IO_getval(IOobject *self, PyObject *args) {
PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0");
static PyObject *
IO_isatty(IOobject *self, PyObject *args) {
UNLESS (PyArg_ParseTuple(args, ":isatty")) return NULL;
IO_isatty(IOobject *self, PyObject *unused) {
Py_INCREF(Py_False);
return Py_False;
}
@ -243,10 +239,9 @@ PyDoc_STRVAR(IO_reset__doc__,
"reset() -- Reset the file position to the beginning");
static PyObject *
IO_reset(IOobject *self, PyObject *args) {
IO_reset(IOobject *self, PyObject *unused) {
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_ParseTuple(args, ":reset")) return NULL;
self->pos = 0;
@ -257,10 +252,9 @@ IO_reset(IOobject *self, PyObject *args) {
PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position.");
static PyObject *
IO_tell(IOobject *self, PyObject *args) {
IO_tell(IOobject *self, PyObject *unused) {
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_ParseTuple(args, ":tell")) return NULL;
return PyInt_FromLong(self->pos);
}
@ -379,10 +373,7 @@ O_write(Oobject *self, PyObject *args) {
PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held.");
static PyObject *
O_close(Oobject *self, PyObject *args) {
UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
O_close(Oobject *self, PyObject *unused) {
if (self->buf != NULL) free(self->buf);
self->buf = NULL;
@ -400,8 +391,6 @@ O_writelines(Oobject *self, PyObject *args) {
PyObject *tmp = 0;
static PyObject *joiner = NULL;
UNLESS (PyArg_ParseTuple(args, "O:writelines", &args)) return NULL;
if (!joiner) {
PyObject *empty_string = PyString_FromString("");
if (empty_string == NULL)
@ -428,21 +417,21 @@ O_writelines(Oobject *self, PyObject *args) {
static struct PyMethodDef O_methods[] = {
/* Common methods: */
{"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
{"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
{"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
{"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
{"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
{"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
{"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
{"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
{"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
{"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
{"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
/* Read-write StringIO specific methods: */
{"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
{"close", (PyCFunction)O_close, METH_NOARGS, O_close__doc__},
{"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
{"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
{"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
{"writelines", (PyCFunction)O_writelines, METH_O, O_writelines__doc__},
{NULL, NULL} /* sentinel */
};
@ -527,10 +516,7 @@ newOobject(int size) {
/* -------------------------------------------------------- */
static PyObject *
I_close(Iobject *self, PyObject *args) {
UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
I_close(Iobject *self, PyObject *unused) {
Py_XDECREF(self->pbuf);
self->pbuf = NULL;
self->buf = NULL;
@ -562,18 +548,18 @@ I_seek(Iobject *self, PyObject *args) {
static struct PyMethodDef I_methods[] = {
/* Common methods: */
{"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
{"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
{"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
{"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
{"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
{"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
{"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
{"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
{"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
{"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
{"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
/* Read-only StringIO specific methods: */
{"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
{"close", (PyCFunction)I_close, METH_NOARGS, O_close__doc__},
{"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
{NULL, NULL}
};
@ -674,7 +660,7 @@ static PyObject *
IO_StringIO(PyObject *self, PyObject *args) {
PyObject *s=0;
if (!PyArg_ParseTuple(args, "|O:StringIO", &s)) return NULL;
if (!PyArg_UnpackTuple(args, "StringIO", 0, 1, &s)) return NULL;
if (s) return newIobject(s);
return newOobject(128);

View File

@ -10,9 +10,7 @@ is equivalent to the expression x+y. The function names are those\n\
used for special class methods; variants without leading and trailing\n\
'__' are also provided for convenience.");
#define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
PyObject *a1; \
if(! PyArg_UnpackTuple(a,#OP,1,1,&a1)) return NULL; \
#define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
return AOP(a1); }
#define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
@ -39,9 +37,8 @@ used for special class methods; variants without leading and trailing\n\
Py_INCREF(Py_None); \
return Py_None; }
#define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
PyObject *a1; long r; \
if(! PyArg_UnpackTuple(a,#OP,1,1,&a1)) return NULL; \
#define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
long r; \
if(-1 == (r=AOP(a1))) return NULL; \
return PyBool_FromLong(r); }
@ -155,19 +152,24 @@ op_delslice(PyObject *s, PyObject *a)
#undef spam1
#undef spam2
#undef spam1o
#undef spam1o
#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, DOC}, \
{#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, DOC}, \
{#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
static struct PyMethodDef operator_methods[] = {
spam1(isCallable,
spam1o(isCallable,
"isCallable(a) -- Same as callable(a).")
spam1(isNumberType,
spam1o(isNumberType,
"isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
spam1(isSequenceType,
spam1o(isSequenceType,
"isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
spam1(truth,
spam1o(truth,
"truth(a) -- Return True if a is true, False otherwise.")
spam2(contains,__contains__,
"contains(a, b) -- Same as b in a (note reversed operands).")
@ -177,7 +179,7 @@ spam1(indexOf,
"indexOf(a, b) -- Return the first index of b in a.")
spam1(countOf,
"countOf(a, b) -- Return the number of times b occurs in a.")
spam1(isMappingType,
spam1o(isMappingType,
"isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
spam2(add,__add__, "add(a, b) -- Same as a + b.")
@ -187,14 +189,14 @@ spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not i
spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
spam2(neg,__neg__, "neg(a) -- Same as -a.")
spam2(pos,__pos__, "pos(a) -- Same as +a.")
spam2(abs,__abs__, "abs(a) -- Same as abs(a).")
spam2(inv,__inv__, "inv(a) -- Same as ~a.")
spam2(invert,__invert__, "invert(a) -- Same as ~a.")
spam2o(neg,__neg__, "neg(a) -- Same as -a.")
spam2o(pos,__pos__, "pos(a) -- Same as +a.")
spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
spam2(not_,__not__, "not_(a) -- Same as not a.")
spam2o(not_,__not__, "not_(a) -- Same as not a.")
spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
spam2(or_,__or__, "or_(a, b) -- Same as a | b.")