diff options
author | Guido van Rossum <guido@python.org> | 1996-04-09 02:41:06 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1996-04-09 02:41:06 (GMT) |
commit | d17057745c1dc58ec6e245991fd049fd987c6895 (patch) | |
tree | 6e0115e284ab8417a1a7fc04cc36b04605b19624 /Python/bltinmodule.c | |
parent | a5a3db70d445b0cf9064c76d9946dcde18fb775e (diff) | |
download | cpython-d17057745c1dc58ec6e245991fd049fd987c6895.zip cpython-d17057745c1dc58ec6e245991fd049fd987c6895.tar.gz cpython-d17057745c1dc58ec6e245991fd049fd987c6895.tar.bz2 |
Add list() method, analogous to tuple().
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r-- | Python/bltinmodule.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 9f528de..a09cd6f 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -845,6 +845,41 @@ builtin_len(self, args) } static object * +builtin_list(self, args) + object *self; + object *args; +{ + object *v; + sequence_methods *sqf; + + if (!newgetargs(args, "O:list", &v)) + return NULL; + if ((sqf = v->ob_type->tp_as_sequence) != NULL) { + int n = (*sqf->sq_length)(v); + int i; + object *l; + if (n < 0) + return NULL; + l = newlistobject(n); + if (l == NULL) + return NULL; + for (i = 0; i < n; i++) { + object *item = (*sqf->sq_item)(v, i); + if (item == NULL) { + DECREF(l); + l = NULL; + break; + } + setlistitem(l, i, item); + } + /* XXX Should support indefinite-length sequences */ + return l; + } + err_setstr(TypeError, "list() argument must be a sequence"); + return NULL; +} + +static object * builtin_locals(self, args) object *self; object *args; @@ -1462,6 +1497,7 @@ static struct methodlist builtin_methods[] = { {"input", builtin_input, 1}, {"int", builtin_int, 1}, {"len", builtin_len, 1}, + {"list", builtin_list, 1}, {"locals", builtin_locals, 1}, {"long", builtin_long, 1}, {"map", builtin_map, 1}, |