]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Made dir() more robust.
authorGuido van Rossum <guido@python.org>
Thu, 24 Oct 1991 14:54:44 +0000 (14:54 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 24 Oct 1991 14:54:44 +0000 (14:54 +0000)
Added hex() and oct().

Python/bltinmodule.c

index 8fac03c5988052987c4cefe13217d784206edf2d..3751ec3fd2ba92fb3b033bd18a5cf92c6b1e53ef 100644 (file)
@@ -37,6 +37,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #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 @@ builtin_chr(self, v)
        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 @@ builtin_dir(self, v)
                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;
 }
@@ -194,6 +197,29 @@ builtin_float(self, v)
        return NULL;
 }
 
+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;
@@ -342,6 +368,29 @@ builtin_max(self, v)
        return min_max(v, 1);
 }
 
+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;
@@ -508,12 +557,14 @@ static struct methodlist builtin_methods[] = {
        {"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},