From d17057745c1dc58ec6e245991fd049fd987c6895 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 9 Apr 1996 02:41:06 +0000 Subject: [PATCH] Add list() method, analogous to tuple(). --- Python/bltinmodule.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 9f528de2fbe..a09cd6f7eac 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -844,6 +844,41 @@ builtin_len(self, args) return newintobject(len); } +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; @@ -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},