mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Disallow keyword arguments for type constructors that don't use them.
(fixes bug #1119418)
This commit is contained in:
parent
bd77da6dab
commit
02c42871cf
13 changed files with 100 additions and 13 deletions
|
@ -15,6 +15,7 @@ PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
|
||||||
char *, char **, ...);
|
char *, char **, ...);
|
||||||
PyAPI_FUNC(int) PyArg_UnpackTuple(PyObject *, char *, int, int, ...);
|
PyAPI_FUNC(int) PyArg_UnpackTuple(PyObject *, char *, int, int, ...);
|
||||||
PyAPI_FUNC(PyObject *) Py_BuildValue(char *, ...);
|
PyAPI_FUNC(PyObject *) Py_BuildValue(char *, ...);
|
||||||
|
PyAPI_FUNC(int) _PyArg_NoKeywords(char *funcname, PyObject *kw);
|
||||||
|
|
||||||
PyAPI_FUNC(int) PyArg_VaParse(PyObject *, char *, va_list);
|
PyAPI_FUNC(int) PyArg_VaParse(PyObject *, char *, va_list);
|
||||||
PyAPI_FUNC(int) PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
|
PyAPI_FUNC(int) PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
|
||||||
|
|
|
@ -12,6 +12,9 @@ What's New in Python 2.5 alpha 1?
|
||||||
Core and builtins
|
Core and builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Disallow keyword arguments for type constructors that don't use it
|
||||||
|
(fixes bug #1119418).
|
||||||
|
|
||||||
- Forward UnicodeDecodeError into SyntaxError for source encoding errors.
|
- Forward UnicodeDecodeError into SyntaxError for source encoding errors.
|
||||||
|
|
||||||
- SF bug #900092: When tracing (e.g. for hotshot), restore 'return' events for
|
- SF bug #900092: When tracing (e.g. for hotshot), restore 'return' events for
|
||||||
|
|
|
@ -481,6 +481,9 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
RandomObject *self;
|
RandomObject *self;
|
||||||
PyObject *tmp;
|
PyObject *tmp;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("Random()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
self = (RandomObject *)type->tp_alloc(type, 0);
|
self = (RandomObject *)type->tp_alloc(type, 0);
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -1800,17 +1800,8 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *initial = NULL, *it = NULL;
|
PyObject *initial = NULL, *it = NULL;
|
||||||
struct arraydescr *descr;
|
struct arraydescr *descr;
|
||||||
|
|
||||||
if (kwds != NULL) {
|
if (!_PyArg_NoKeywords("array.array()", kwds))
|
||||||
int i = PyObject_Length(kwds);
|
|
||||||
if (i < 0)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
else if (i > 0) {
|
|
||||||
PyErr_SetString(PyExc_TypeError,
|
|
||||||
"array.array constructor takes "
|
|
||||||
"no keyword arguments");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
|
if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -95,6 +95,9 @@ deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
dequeobject *deque;
|
dequeobject *deque;
|
||||||
block *b;
|
block *b;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("deque()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
/* create dequeobject structure */
|
/* create dequeobject structure */
|
||||||
deque = (dequeobject *)type->tp_alloc(type, 0);
|
deque = (dequeobject *)type->tp_alloc(type, 0);
|
||||||
if (deque == NULL)
|
if (deque == NULL)
|
||||||
|
|
|
@ -618,6 +618,9 @@ cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *saved;
|
PyObject *saved;
|
||||||
cycleobject *lz;
|
cycleobject *lz;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("cycle()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable))
|
if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -765,6 +768,9 @@ dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *it;
|
PyObject *it;
|
||||||
dropwhileobject *lz;
|
dropwhileobject *lz;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("dropwhile()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq))
|
if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -906,6 +912,9 @@ takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *it;
|
PyObject *it;
|
||||||
takewhileobject *lz;
|
takewhileobject *lz;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("takewhile()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq))
|
if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -1048,6 +1057,9 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
int numargs;
|
int numargs;
|
||||||
isliceobject *lz;
|
isliceobject *lz;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("islice()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3))
|
if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -1236,6 +1248,9 @@ starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *it;
|
PyObject *it;
|
||||||
starmapobject *lz;
|
starmapobject *lz;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("starmap()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq))
|
if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -1365,6 +1380,9 @@ imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
imapobject *lz;
|
imapobject *lz;
|
||||||
int numargs, i;
|
int numargs, i;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("imap()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
numargs = PyTuple_Size(args);
|
numargs = PyTuple_Size(args);
|
||||||
if (numargs < 2) {
|
if (numargs < 2) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
@ -1544,6 +1562,9 @@ chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
int i;
|
int i;
|
||||||
PyObject *ittuple;
|
PyObject *ittuple;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("chain()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
/* obtain iterators */
|
/* obtain iterators */
|
||||||
assert(PyTuple_Check(args));
|
assert(PyTuple_Check(args));
|
||||||
ittuple = PyTuple_New(tuplesize);
|
ittuple = PyTuple_New(tuplesize);
|
||||||
|
@ -1684,6 +1705,9 @@ ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *it;
|
PyObject *it;
|
||||||
ifilterobject *lz;
|
ifilterobject *lz;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("ifilter()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq))
|
if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -1825,6 +1849,9 @@ ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *it;
|
PyObject *it;
|
||||||
ifilterfalseobject *lz;
|
ifilterfalseobject *lz;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("ifilterfalse()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq))
|
if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -1964,6 +1991,9 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
countobject *lz;
|
countobject *lz;
|
||||||
long cnt = 0;
|
long cnt = 0;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("count()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "|l:count", &cnt))
|
if (!PyArg_ParseTuple(args, "|l:count", &cnt))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -2060,6 +2090,9 @@ izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
int tuplesize = PySequence_Length(args);
|
int tuplesize = PySequence_Length(args);
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("izip()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
/* args must be a tuple */
|
/* args must be a tuple */
|
||||||
assert(PyTuple_Check(args));
|
assert(PyTuple_Check(args));
|
||||||
|
|
||||||
|
@ -2240,6 +2273,9 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *element;
|
PyObject *element;
|
||||||
long cnt = -1;
|
long cnt = -1;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("repeat()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O|l:repeat", &element, &cnt))
|
if (!PyArg_ParseTuple(args, "O|l:repeat", &element, &cnt))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
|
|
@ -269,6 +269,9 @@ itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *item;
|
PyObject *item;
|
||||||
int nitems;
|
int nitems;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("itemgetter()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
nitems = PyTuple_GET_SIZE(args);
|
nitems = PyTuple_GET_SIZE(args);
|
||||||
if (nitems <= 1) {
|
if (nitems <= 1) {
|
||||||
if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
|
if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
|
||||||
|
@ -405,6 +408,9 @@ attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
PyObject *attr;
|
PyObject *attr;
|
||||||
int nattrs;
|
int nattrs;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("attrgetter()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
nattrs = PyTuple_GET_SIZE(args);
|
nattrs = PyTuple_GET_SIZE(args);
|
||||||
if (nattrs <= 1) {
|
if (nattrs <= 1) {
|
||||||
if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
|
if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
|
||||||
|
|
|
@ -65,6 +65,9 @@ zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
|
||||||
char *path, *p, *prefix, buf[MAXPATHLEN+2];
|
char *path, *p, *prefix, buf[MAXPATHLEN+2];
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("zipimporter()", kwds))
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "s:zipimporter",
|
if (!PyArg_ParseTuple(args, "s:zipimporter",
|
||||||
&path))
|
&path))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -192,7 +192,10 @@ buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
int size = Py_END_OF_BUFFER;
|
int size = Py_END_OF_BUFFER;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
|
if (!_PyArg_NoKeywords("buffer()", kw))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size))
|
||||||
return NULL;
|
return NULL;
|
||||||
return PyBuffer_FromObject(ob, offset, size);
|
return PyBuffer_FromObject(ob, offset, size);
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,9 @@ range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
||||||
long ilow = 0, ihigh = 0, istep = 1;
|
long ilow = 0, ihigh = 0, istep = 1;
|
||||||
long n;
|
long n;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("xrange()", kw))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (PyTuple_Size(args) <= 1) {
|
if (PyTuple_Size(args) <= 1) {
|
||||||
if (!PyArg_ParseTuple(args,
|
if (!PyArg_ParseTuple(args,
|
||||||
"l;xrange() requires 1-3 int arguments",
|
"l;xrange() requires 1-3 int arguments",
|
||||||
|
|
|
@ -935,6 +935,9 @@ frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
PyObject *iterable = NULL, *result;
|
PyObject *iterable = NULL, *result;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("frozenset()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
|
if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -976,6 +979,9 @@ PySet_Fini(void)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
|
if (!_PyArg_NoKeywords("set()", kwds))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return make_new_set(type, NULL);
|
return make_new_set(type, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -174,6 +174,9 @@ slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
||||||
|
|
||||||
start = stop = step = NULL;
|
start = stop = step = NULL;
|
||||||
|
|
||||||
|
if (!_PyArg_NoKeywords("slice()", kw))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
|
if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
|
|
@ -1595,3 +1595,29 @@ PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
|
||||||
va_end(vargs);
|
va_end(vargs);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* For type constructors that don't take keyword args
|
||||||
|
*
|
||||||
|
* Sets a TypeError and returns 0 if the kwds dict is
|
||||||
|
* not emtpy, returns 1 otherwise
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
_PyArg_NoKeywords(char *funcname, PyObject *kw)
|
||||||
|
{
|
||||||
|
if (kw == NULL)
|
||||||
|
return 1;
|
||||||
|
if (!PyDict_CheckExact(kw)) {
|
||||||
|
PyErr_BadInternalCall();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (PyDict_Size(kw) == 0)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
|
||||||
|
funcname);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue