Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc71cac

Browse files
author
Peter Schneider-Kamp
committed
replaced PyArgs_Parse by PyArgs_ParseTuple
changed error messages for extend method from "append" to "extend"
1 parent 5a65c2d commit dc71cac

1 file changed

Lines changed: 39 additions & 34 deletions

File tree

Modules/arraymodule.c

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -801,14 +801,14 @@ array_extend(self, args)
801801

802802
if (!is_arrayobject(bb)) {
803803
PyErr_Format(PyExc_TypeError,
804-
"can only append array (not \"%.200s\") to array",
804+
"can only extend array with array (not \"%.200s\")",
805805
bb->ob_type->tp_name);
806806
return NULL;
807807
}
808808
#define b ((arrayobject *)bb)
809809
if (self->ob_descr != b->ob_descr) {
810810
PyErr_SetString(PyExc_TypeError,
811-
"can only append arrays of same kind");
811+
"can only extend with array of same kind");
812812
return NULL;
813813
}
814814
size = self->ob_size + b->ob_size;
@@ -835,7 +835,7 @@ array_insert(arrayobject *self, PyObject *args)
835835
{
836836
int i;
837837
PyObject *v;
838-
if (!PyArg_Parse(args, "(iO)", &i, &v))
838+
if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
839839
return NULL;
840840
return ins(self, i, v);
841841
}
@@ -869,7 +869,7 @@ static PyObject *
869869
array_append(arrayobject *self, PyObject *args)
870870
{
871871
PyObject *v;
872-
if (!PyArg_Parse(args, "O", &v))
872+
if (!PyArg_ParseTuple(args, "O:append", &v))
873873
return NULL;
874874
return ins(self, (int) self->ob_size, v);
875875
}
@@ -979,7 +979,7 @@ array_fromfile(arrayobject *self, PyObject *args)
979979
PyObject *f;
980980
int n;
981981
FILE *fp;
982-
if (!PyArg_Parse(args, "(Oi)", &f, &n))
982+
if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
983983
return NULL;
984984
fp = PyFile_AsFile(f);
985985
if (fp == NULL) {
@@ -1032,7 +1032,7 @@ array_tofile(arrayobject *self, PyObject *args)
10321032
{
10331033
PyObject *f;
10341034
FILE *fp;
1035-
if (!PyArg_Parse(args, "O", &f))
1035+
if (!PyArg_ParseTuple(args, "O:tofile", &f))
10361036
return NULL;
10371037
fp = PyFile_AsFile(f);
10381038
if (fp == NULL) {
@@ -1064,7 +1064,7 @@ array_fromlist(arrayobject *self, PyObject *args)
10641064
int n;
10651065
PyObject *list;
10661066
int itemsize = self->ob_descr->itemsize;
1067-
if (!PyArg_Parse(args, "O", &list))
1067+
if (!PyArg_ParseTuple(args, "O:fromlist", &list))
10681068
return NULL;
10691069
if (!PyList_Check(list)) {
10701070
PyErr_SetString(PyExc_TypeError, "arg must be list");
@@ -1108,6 +1108,8 @@ array_tolist(arrayobject *self, PyObject *args)
11081108
{
11091109
PyObject *list = PyList_New(self->ob_size);
11101110
int i;
1111+
if (!PyArg_ParseTuple(args, ":tolist"))
1112+
return NULL;
11111113
if (list == NULL)
11121114
return NULL;
11131115
for (i = 0; i < self->ob_size; i++) {
@@ -1133,7 +1135,7 @@ array_fromstring(arrayobject *self, PyObject *args)
11331135
char *str;
11341136
int n;
11351137
int itemsize = self->ob_descr->itemsize;
1136-
if (!PyArg_Parse(args, "s#", &str, &n))
1138+
if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
11371139
return NULL;
11381140
if (n % itemsize != 0) {
11391141
PyErr_SetString(PyExc_ValueError,
@@ -1167,7 +1169,7 @@ values,as if it had been read from a file using the fromfile() method).";
11671169
static PyObject *
11681170
array_tostring(arrayobject *self, PyObject *args)
11691171
{
1170-
if (!PyArg_Parse(args, ""))
1172+
if (!PyArg_ParseTuple(args, ":tostring"))
11711173
return NULL;
11721174
return PyString_FromStringAndSize(self->ob_item,
11731175
self->ob_size * self->ob_descr->itemsize);
@@ -1180,26 +1182,25 @@ Convert the array to an array of machine values and return the string\n\
11801182
representation.";
11811183

11821184
PyMethodDef array_methods[] = {
1183-
{"append", (PyCFunction)array_append, 0, append_doc},
1184-
{"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc},
1185-
{"byteswap", (PyCFunction)array_byteswap, METH_VARARGS,
1186-
byteswap_doc},
1187-
{"count", (PyCFunction)array_count, 1, count_doc},
1188-
{"extend", (PyCFunction)array_extend, 1, extend_doc},
1189-
{"fromfile", (PyCFunction)array_fromfile, 0, fromfile_doc},
1190-
{"fromlist", (PyCFunction)array_fromlist, 0, fromlist_doc},
1191-
{"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc},
1192-
{"index", (PyCFunction)array_index, 1, index_doc},
1193-
{"insert", (PyCFunction)array_insert, 0, insert_doc},
1194-
{"pop", (PyCFunction)array_pop, 1, pop_doc},
1195-
{"read", (PyCFunction)array_fromfile, 0, fromfile_doc},
1196-
{"remove", (PyCFunction)array_remove, 1, remove_doc},
1197-
{"reverse", (PyCFunction)array_reverse, 0, reverse_doc},
1198-
/* {"sort", (PyCFunction)array_sort, 0, sort_doc},*/
1199-
{"tofile", (PyCFunction)array_tofile, 0, tofile_doc},
1200-
{"tolist", (PyCFunction)array_tolist, 0, tolist_doc},
1201-
{"tostring", (PyCFunction)array_tostring, 0, tostring_doc},
1202-
{"write", (PyCFunction)array_tofile, 0, tofile_doc},
1185+
{"append", (PyCFunction)array_append, METH_VARARGS, append_doc},
1186+
{"buffer_info", (PyCFunction)array_buffer_info, METH_VARARGS, buffer_info_doc},
1187+
{"byteswap", (PyCFunction)array_byteswap, METH_VARARGS, byteswap_doc},
1188+
{"count", (PyCFunction)array_count, METH_VARARGS, count_doc},
1189+
{"extend", (PyCFunction)array_extend, METH_VARARGS, extend_doc},
1190+
{"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc},
1191+
{"fromlist", (PyCFunction)array_fromlist, METH_VARARGS, fromlist_doc},
1192+
{"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, fromstring_doc},
1193+
{"index", (PyCFunction)array_index, METH_VARARGS, index_doc},
1194+
{"insert", (PyCFunction)array_insert, METH_VARARGS, insert_doc},
1195+
{"pop", (PyCFunction)array_pop, METH_VARARGS, pop_doc},
1196+
{"read", (PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc},
1197+
{"remove", (PyCFunction)array_remove, METH_VARARGS, remove_doc},
1198+
{"reverse", (PyCFunction)array_reverse, METH_VARARGS, reverse_doc},
1199+
/* {"sort", (PyCFunction)array_sort, METH_VARARGS, sort_doc},*/
1200+
{"tofile", (PyCFunction)array_tofile, METH_VARARGS, tofile_doc},
1201+
{"tolist", (PyCFunction)array_tolist, METH_VARARGS, tolist_doc},
1202+
{"tostring", (PyCFunction)array_tostring, METH_VARARGS, tostring_doc},
1203+
{"write", (PyCFunction)array_tofile, METH_VARARGS, tofile_doc},
12031204
{NULL, NULL} /* sentinel */
12041205
};
12051206

@@ -1235,6 +1236,7 @@ array_print(arrayobject *a, FILE *fp, int flags)
12351236
{
12361237
int ok = 0;
12371238
int i, len;
1239+
PyObject *t_empty = PyTuple_New(0);
12381240
PyObject *v;
12391241
len = a->ob_size;
12401242
if (len == 0) {
@@ -1243,7 +1245,8 @@ array_print(arrayobject *a, FILE *fp, int flags)
12431245
}
12441246
if (a->ob_descr->typecode == 'c') {
12451247
fprintf(fp, "array('c', ");
1246-
v = array_tostring(a, (PyObject *)NULL);
1248+
v = array_tostring(a, t_empty);
1249+
Py_DECREF(t_empty);;
12471250
ok = PyObject_Print(v, fp, 0);
12481251
Py_XDECREF(v);
12491252
fprintf(fp, ")");
@@ -1355,9 +1358,9 @@ a_array(PyObject *self, PyObject *args)
13551358
char c;
13561359
PyObject *initial = NULL;
13571360
struct arraydescr *descr;
1358-
if (!PyArg_Parse(args, "c", &c)) {
1361+
if (!PyArg_ParseTuple(args, "c:array", &c)) {
13591362
PyErr_Clear();
1360-
if (!PyArg_Parse(args, "(cO)", &c, &initial))
1363+
if (!PyArg_ParseTuple(args, "cO:array", &c, &initial))
13611364
return NULL;
13621365
if (!PyList_Check(initial) && !PyString_Check(initial)) {
13631366
PyErr_SetString(PyExc_TypeError,
@@ -1388,8 +1391,10 @@ a_array(PyObject *self, PyObject *args)
13881391
}
13891392
}
13901393
if (initial != NULL && PyString_Check(initial)) {
1394+
PyObject *t_initial = Py_BuildValue("(O)", initial);
13911395
PyObject *v =
1392-
array_fromstring((arrayobject *)a, initial);
1396+
array_fromstring((arrayobject *)a, t_initial);
1397+
Py_DECREF(t_initial);
13931398
if (v == NULL) {
13941399
Py_DECREF(a);
13951400
return NULL;
@@ -1412,7 +1417,7 @@ initialized from the optional initializer value, which must be a list\n\
14121417
or a string.";
14131418

14141419
static PyMethodDef a_methods[] = {
1415-
{"array", a_array, 0, a_array_doc},
1420+
{"array", a_array, METH_VARARGS, a_array_doc},
14161421
{NULL, NULL} /* sentinel */
14171422
};
14181423

0 commit comments

Comments
 (0)