diff options
author | Georg Brandl <georg@python.org> | 2005-08-26 06:43:16 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2005-08-26 06:43:16 (GMT) |
commit | 66e75ac6e092fd1b5b9a7e9378928c9ec0829f32 (patch) | |
tree | 203bed2e39badf89a4bef7e1a281f81192a8097e | |
parent | a74a655d816399e1168680c20daff3032665450d (diff) | |
download | cpython-66e75ac6e092fd1b5b9a7e9378928c9ec0829f32.zip cpython-66e75ac6e092fd1b5b9a7e9378928c9ec0829f32.tar.gz cpython-66e75ac6e092fd1b5b9a7e9378928c9ec0829f32.tar.bz2 |
Disallow keyword arguments for type constructors that don't use them
(fixes #1119418).
-rw-r--r-- | Include/modsupport.h | 1 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Modules/_randommodule.c | 3 | ||||
-rw-r--r-- | Modules/arraymodule.c | 15 | ||||
-rw-r--r-- | Modules/collectionsmodule.c | 3 | ||||
-rw-r--r-- | Modules/itertoolsmodule.c | 36 | ||||
-rw-r--r-- | Modules/operator.c | 6 | ||||
-rw-r--r-- | Modules/zipimport.c | 3 | ||||
-rw-r--r-- | Objects/bufferobject.c | 5 | ||||
-rw-r--r-- | Objects/rangeobject.c | 3 | ||||
-rw-r--r-- | Objects/setobject.c | 6 | ||||
-rw-r--r-- | Objects/sliceobject.c | 3 | ||||
-rw-r--r-- | Python/getargs.c | 26 |
13 files changed, 100 insertions, 13 deletions
diff --git a/Include/modsupport.h b/Include/modsupport.h index 2d67f3e..bc30c3f 100644 --- a/Include/modsupport.h +++ b/Include/modsupport.h @@ -15,6 +15,7 @@ PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, char *, char **, ...); PyAPI_FUNC(int) PyArg_UnpackTuple(PyObject *, char *, int, int, ...); 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_VaParseTupleAndKeywords(PyObject *, PyObject *, @@ -12,6 +12,9 @@ What's New in Python 2.4.2a Core and builtins ----------------- +- Disallow keyword arguments for type constructors that don't use them + (fixes bug #1119418). + - Forward UnicodeDecodeError into SyntaxError for source encoding errors. - SF bug #900092: When tracing (e.g. for hotshot), restore 'return' events for diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c index 1189036..8fe2b2b 100644 --- a/Modules/_randommodule.c +++ b/Modules/_randommodule.c @@ -481,6 +481,9 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds) RandomObject *self; PyObject *tmp; + if (!_PyArg_NoKeywords("Random()", kwds)) + return NULL; + self = (RandomObject *)type->tp_alloc(type, 0); if (self == NULL) return NULL; diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 7ed3b73..0e78e65 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1774,18 +1774,9 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) char c; PyObject *initial = NULL, *it = NULL; struct arraydescr *descr; - - if (kwds != NULL) { - int i = PyObject_Length(kwds); - if (i < 0) - return NULL; - else if (i > 0) { - PyErr_SetString(PyExc_TypeError, - "array.array constructor takes " - "no keyword arguments"); - return NULL; - } - } + + if (!_PyArg_NoKeywords("array.array()", kwds)) + return NULL; if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial)) return NULL; diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c index ed10999..5af568d 100644 --- a/Modules/collectionsmodule.c +++ b/Modules/collectionsmodule.c @@ -95,6 +95,9 @@ deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds) dequeobject *deque; block *b; + if (!_PyArg_NoKeywords("deque()", kwds)) + return NULL; + /* create dequeobject structure */ deque = (dequeobject *)type->tp_alloc(type, 0); if (deque == NULL) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index bf148ac..3d5e8a2 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -618,6 +618,9 @@ cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *saved; cycleobject *lz; + if (!_PyArg_NoKeywords("cycle()", kwds)) + return NULL; + if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable)) return NULL; @@ -765,6 +768,9 @@ dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *it; dropwhileobject *lz; + if (!_PyArg_NoKeywords("dropwhile()", kwds)) + return NULL; + if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq)) return NULL; @@ -906,6 +912,9 @@ takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *it; takewhileobject *lz; + if (!_PyArg_NoKeywords("takewhile()", kwds)) + return NULL; + if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq)) return NULL; @@ -1048,6 +1057,9 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) int numargs; isliceobject *lz; + if (!_PyArg_NoKeywords("islice()", kwds)) + return NULL; + numargs = PyTuple_Size(args); if (!PyArg_ParseTuple(args, "OO|Ol:islice", &seq, &a1, &a2, &step)) return NULL; @@ -1234,6 +1246,9 @@ starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *it; starmapobject *lz; + if (!_PyArg_NoKeywords("starmap()", kwds)) + return NULL; + if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq)) return NULL; @@ -1363,6 +1378,9 @@ imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) imapobject *lz; int numargs, i; + if (!_PyArg_NoKeywords("imap()", kwds)) + return NULL; + numargs = PyTuple_Size(args); if (numargs < 2) { PyErr_SetString(PyExc_TypeError, @@ -1542,6 +1560,9 @@ chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) int i; PyObject *ittuple; + if (!_PyArg_NoKeywords("chain()", kwds)) + return NULL; + /* obtain iterators */ assert(PyTuple_Check(args)); ittuple = PyTuple_New(tuplesize); @@ -1682,6 +1703,9 @@ ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *it; ifilterobject *lz; + if (!_PyArg_NoKeywords("ifilter()", kwds)) + return NULL; + if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq)) return NULL; @@ -1823,6 +1847,9 @@ ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *it; ifilterfalseobject *lz; + if (!_PyArg_NoKeywords("ifilterfalse()", kwds)) + return NULL; + if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq)) return NULL; @@ -1962,6 +1989,9 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds) countobject *lz; long cnt = 0; + if (!_PyArg_NoKeywords("count()", kwds)) + return NULL; + if (!PyArg_ParseTuple(args, "|l:count", &cnt)) return NULL; @@ -2058,6 +2088,9 @@ izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *result; int tuplesize = PySequence_Length(args); + if (!_PyArg_NoKeywords("izip()", kwds)) + return NULL; + /* args must be a tuple */ assert(PyTuple_Check(args)); @@ -2238,6 +2271,9 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *element; long cnt = -1; + if (!_PyArg_NoKeywords("repeat()", kwds)) + return NULL; + if (!PyArg_ParseTuple(args, "O|l:repeat", &element, &cnt)) return NULL; diff --git a/Modules/operator.c b/Modules/operator.c index 468440e..00135d9b 100644 --- a/Modules/operator.c +++ b/Modules/operator.c @@ -267,6 +267,9 @@ itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) itemgetterobject *ig; PyObject *item; + if (!_PyArg_NoKeywords("itemgetter()", kdws)) + return NULL; + if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item)) return NULL; @@ -374,6 +377,9 @@ attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) attrgetterobject *ag; PyObject *attr; + if (!_PyArg_NoKeywords("attrgetter()", kwds)) + return NULL; + if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr)) return NULL; diff --git a/Modules/zipimport.c b/Modules/zipimport.c index 9630dea..e445300 100644 --- a/Modules/zipimport.c +++ b/Modules/zipimport.c @@ -65,6 +65,9 @@ zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds) char *path, *p, *prefix, buf[MAXPATHLEN+2]; int len; + if (!_PyArg_NoKeywords("zipimporter()", kwds)) + return -1; + if (!PyArg_ParseTuple(args, "s:zipimporter", &path)) return -1; diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c index 674d2b5..da8d9fc 100644 --- a/Objects/bufferobject.c +++ b/Objects/bufferobject.c @@ -192,7 +192,10 @@ buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw) int offset = 0; 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 PyBuffer_FromObject(ob, offset, size); } diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index dabb8d4..6feb217 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -90,6 +90,9 @@ range_new(PyTypeObject *type, PyObject *args, PyObject *kw) long ilow = 0, ihigh = 0, istep = 1; long n; + if (!_PyArg_NoKeywords("xrange()", kw)) + return NULL; + if (PyTuple_Size(args) <= 1) { if (!PyArg_ParseTuple(args, "l;xrange() requires 1-3 int arguments", diff --git a/Objects/setobject.c b/Objects/setobject.c index 0e65320..e36ae4f 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -81,6 +81,9 @@ frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *iterable = NULL; + if (!_PyArg_NoKeywords("frozenset()", kwds)) + return NULL; + if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) return NULL; if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) { @@ -93,6 +96,9 @@ frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static PyObject * set_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + if (!_PyArg_NoKeywords("set()", kwds)) + return NULL; + return make_new_set(type, NULL); } diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index c37af2b..f5ed898 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -174,6 +174,9 @@ slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) start = stop = step = NULL; + if (!_PyArg_NoKeywords("slice()", kw)) + return NULL; + if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step)) return NULL; diff --git a/Python/getargs.c b/Python/getargs.c index 48f9dc4..87d8096 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -1594,3 +1594,29 @@ PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...) va_end(vargs); 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; +} + + + |