Mercurial > cpython-lru_cache-weakref
diff Python/bltinmodule.c @ 404:9cd38d1ac391 legacy-trunk
Made dir() more robust.
Added hex() and oct().
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 24 Oct 1991 14:54:44 +0000 |
| parents | 039aee6cbd78 |
| children | c97f4c6ba6c6 |
line wrap: on
line diff
--- a/Python/bltinmodule.c Thu Oct 24 14:54:25 1991 +0000 +++ b/Python/bltinmodule.c Thu Oct 24 14:54:44 1991 +0000 @@ -37,6 +37,9 @@ #include "ceval.h" #include "modsupport.h" +/* Should be in longobject.h */ +extern stringobject *long_format PROTO((object *, int)); + static object * builtin_abs(self, v) object *self; @@ -58,7 +61,7 @@ long x; char s[1]; if (v == NULL || !is_intobject(v)) { - err_setstr(TypeError, "chr() must have int argument"); + err_setstr(TypeError, "chr() requires int argument"); return NULL; } x = getintvalue(v); @@ -84,20 +87,20 @@ d = getattr(v, "__dict__"); if (d == NULL) { err_setstr(TypeError, - "dir() argument has no variable attributes"); + "dir() argument must have __dict__ attribute"); return NULL; } } - if (!is_dictobject(d)) { /* Assume it is None */ - v = newlistobject(0); - } - else { + if (is_dictobject(d)) { v = getdictkeys(d); if (sortlist(v) != 0) { DECREF(v); v = NULL; } } + else { + v = newlistobject(0); + } DECREF(d); return v; } @@ -195,6 +198,29 @@ } static object * +builtin_hex(self, v) + object *self; + object *v; +{ + if (v != NULL) { + if (is_intobject(v)) { + char buf[20]; + long x = getintvalue(v); + if (x >= 0) + sprintf(buf, "0x%lx", x); + else + sprintf(buf, "-0x%lx", -x); + return newstringobject(buf); + } + if (is_longobject(v)) { + return (object *) long_format(v, 16); + } + } + err_setstr(TypeError, "hex() requires int/long argument"); + return NULL; +} + +static object * builtin_input(self, v) object *self; object *v; @@ -343,6 +369,29 @@ } static object * +builtin_oct(self, v) + object *self; + object *v; +{ + if (v != NULL) { + if (is_intobject(v)) { + char buf[20]; + long x = getintvalue(v); + if (x >= 0) + sprintf(buf, "0%lo", x); + else + sprintf(buf, "-0%lo", -x); + return newstringobject(buf); + } + if (is_longobject(v)) { + return (object *) long_format(v, 8); + } + } + err_setstr(TypeError, "oct() requires int/long argument"); + return NULL; +} + +static object * builtin_open(self, v) object *self; object *v; @@ -508,12 +557,14 @@ {"eval", builtin_eval}, {"exec", builtin_exec}, {"float", builtin_float}, + {"hex", builtin_hex}, {"input", builtin_input}, {"int", builtin_int}, {"len", builtin_len}, {"long", builtin_long}, {"max", builtin_max}, {"min", builtin_min}, + {"oct", builtin_oct}, {"open", builtin_open}, /* XXX move to OS module */ {"ord", builtin_ord}, {"pow", builtin_pow},
