diff options
author | Raymond Hettinger <python@rcn.com> | 2004-02-10 09:25:40 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-02-10 09:25:40 (GMT) |
commit | 5cab2e3a88903b0cea558ee01b8f5b9e5a31a797 (patch) | |
tree | 520d1fdfefd2c4104669ad18fc30e0e86dbadc2c /Modules | |
parent | 27da291b2c2b32b0f976bb7599f6e9b2d07e6e4c (diff) | |
download | cpython-5cab2e3a88903b0cea558ee01b8f5b9e5a31a797.zip cpython-5cab2e3a88903b0cea558ee01b8f5b9e5a31a797.tar.gz cpython-5cab2e3a88903b0cea558ee01b8f5b9e5a31a797.tar.bz2 |
Give itertools.repeat() a length method.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/itertoolsmodule.c | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 387133c..ebd5f6d 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -2347,6 +2347,19 @@ repeat_next(repeatobject *ro) return ro->element; } +static int +repeat_len(repeatobject *ro) +{ + if (ro->cnt == -1) + PyErr_SetString(PyExc_TypeError, "len() of unsized object"); + return (int)(ro->cnt); +} + +static PySequenceMethods repeat_as_sequence = { + (inquiry)repeat_len, /* sq_length */ + 0, /* sq_concat */ +}; + PyDoc_STRVAR(repeat_doc, "repeat(element [,times]) -> create an iterator which returns the element\n\ for the specified number of times. If not specified, returns the element\n\ @@ -2366,7 +2379,7 @@ static PyTypeObject repeat_type = { 0, /* tp_compare */ 0, /* tp_repr */ 0, /* tp_as_number */ - 0, /* tp_as_sequence */ + &repeat_as_sequence, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ |