diff options
author | Guido van Rossum <guido@python.org> | 1994-08-29 12:53:11 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1994-08-29 12:53:11 (GMT) |
commit | cae027b2988de4ad7165288907f91ba2f280bd98 (patch) | |
tree | 43e9ec5012f82681f36c1db98bf22dcd67545004 /Python | |
parent | df05ac6dfb9f7210cf332ec288493c27d72ba0e7 (diff) | |
download | cpython-cae027b2988de4ad7165288907f91ba2f280bd98.zip cpython-cae027b2988de4ad7165288907f91ba2f280bd98.tar.gz cpython-cae027b2988de4ad7165288907f91ba2f280bd98.tar.bz2 |
* Python/bltinmodule.c: added tuple() builtin
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bltinmodule.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 7e872ad..fd98365 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1152,6 +1152,76 @@ builtin_str(self, v) } static object * +builtin_tuple(self, v) + object *self; + object *v; +{ + sequence_methods *sqf; + if (v == NULL) + v = None; /* Force error later */ + if (is_tupleobject(v)) { + INCREF(v); + return v; + } + if (is_listobject(v)) { + int n = getlistsize(v); + object *t = newtupleobject(n); + if (t != NULL) { + int i; + for (i = 0; i < n; i++) { + object *item = getlistitem(v, i); + INCREF(item); + settupleitem(t, i, item); + } + } + return t; + } + if (is_stringobject(v)) { + int n = getstringsize(v); + object *t = newtupleobject(n); + if (t != NULL) { + int i; + char *p = getstringvalue(v); + for (i = 0; i < n; i++) { + object *item = newsizedstringobject(p+i, 1); + if (item == NULL) { + DECREF(t); + t = NULL; + break; + } + settupleitem(t, i, item); + } + } + return t; + } + /* Generic sequence object */ + if ((sqf = v->ob_type->tp_as_sequence) != NULL) { + int n = (*sqf->sq_length)(v); + int i; + object *t; + if (n < 0) + return NULL; + t = newtupleobject(n); + if (t == NULL) + return NULL; + for (i = 0; i < n; i++) { + object *item = (*sqf->sq_item)(v, i); + if (item == NULL) { + DECREF(t); + t = NULL; + break; + } + settupleitem(t, i, item); + } + /* XXX Should support indefinite-length sequences */ + return t; + } + /* None of the above */ + err_setstr(TypeError, "tuple() argument must be a sequence"); + return NULL; +} + +static object * builtin_type(self, v) object *self; object *v; @@ -1224,6 +1294,7 @@ static struct methodlist builtin_methods[] = { {"round", builtin_round}, {"setattr", builtin_setattr}, {"str", builtin_str}, + {"tuple", builtin_tuple}, {"type", builtin_type}, {"vars", builtin_vars}, {"xrange", builtin_xrange}, |