]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
* mpzmodule.c: removed redundant mpz_print function.
authorGuido van Rossum <guido@python.org>
Fri, 5 Nov 1993 10:22:19 +0000 (10:22 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 5 Nov 1993 10:22:19 +0000 (10:22 +0000)
* object.[ch], bltinmodule.c, fileobject.c: changed str() to call
  strobject() which calls an object's __str__ method if it has one.
  strobject() is also called by writeobject() when PRINT_RAW is passed.
* ceval.c: rationalize code for PRINT_ITEM (no change in function!)
* funcobject.c, codeobject.c: added compare and hash functionality.
  Functions with identical code objects and the same global dictionary are
  equal.  Code objects are equal when their code, constants list and names
  list are identical (i.e. the filename and code name don't count).
  (hash doesn't work yet since the constants are in a list and lists can't
  be hashed -- suppose this should really be done with a tuple now we have
  resizetuple!)

Include/object.h
Modules/mpzmodule.c
Objects/fileobject.c
Objects/object.c
Python/bltinmodule.c
Python/ceval.c

index ab270f861d443656a0d8e2aeb18ac4942db70b64..24989bf32ba1a1e16848679518615dae9c0de865 100644 (file)
@@ -214,6 +214,7 @@ extern typeobject Typetype; /* The type of type objects */
 /* Generic operations on objects */
 extern int printobject PROTO((object *, FILE *, int));
 extern object * reprobject PROTO((object *));
+extern object * strobject PROTO((object *));
 extern int cmpobject PROTO((object *, object *));
 extern object *getattr PROTO((object *, char *));
 extern int hasattr PROTO((object *, char *));
index a3e903e4ac8918b0ed0d7f84fa2b21325a2d23d3..5ff0801954ec60ae1acd0ee05b2876c263189352 100644 (file)
@@ -265,24 +265,6 @@ mpz_dealloc(mpzp)
        DEL(mpzp);
 } /* mpz_dealloc() */
 
-/* ARGSUSED */
-static int
-mpz_print(v, fp, flags)
-       object *v;
-       FILE *fp;
-       int flags; /* Not used but required by interface */
-{
-       stringobject *str
-               = (stringobject *)mpz_format(v, 10, (unsigned char)1);
-
-       if (str == NULL)
-               return -1;
-
-       fputs(GETSTRINGVALUE(str), fp);
-       DECREF(str);
-       return 0;
-} /* mpz_print() */
-
 
 /* pointers to frequently used values 0, 1 and -1 */
 static mpzobject *mpz_value_zero, *mpz_value_one, *mpz_value_mone;
@@ -1658,7 +1640,7 @@ static typeobject MPZtype = {
        0,                      /*tp_itemsize*/
        /* methods */
        mpz_dealloc,    /*tp_dealloc*/
-       mpz_print,      /*tp_print*/
+       0,              /*tp_print*/
        mpz_getattr,    /*tp_getattr*/
        0,              /*tp_setattr*/
        mpz_compare,    /*tp_compare*/
index 7ed4fcd08288b5c0a22d885171ba094604d07da2..518fe04f141318919de61c0fa5f043c6d16831c9 100644 (file)
@@ -680,16 +680,13 @@ writeobject(v, f, flags)
        writer = getattr(f, "write");
        if (writer == NULL)
                return -1;
-       if ((flags & PRINT_RAW) && is_stringobject(v)) {
-               value = v;
-               INCREF(value);
-       }
-       else {
+       if (flags & PRINT_RAW)
+               value = strobject(v);
+       else
                value = reprobject(v);
-               if (value == NULL) {
-                       DECREF(writer);
-                       return -1;
-               }
+       if (value == NULL) {
+               DECREF(writer);
+               return -1;
        }
        result = call_object(writer, value);
        DECREF(writer);
index 73fba504595e7146e62c935b37a363c160e21196..7a7383e08aa279d7bd58faf5b9e08f2bc9f88ef3 100644 (file)
@@ -124,7 +124,11 @@ printobject(op, fp, flags)
                                        op->ob_type->tp_name, (long)op);
                        }
                        else {
-                               object *s = reprobject(op);
+                               object *s;
+                               if (flags & PRINT_RAW)
+                                       s = strobject(op);
+                               else
+                                       s = reprobject(op);
                                if (s == NULL)
                                        ret = -1;
                                else if (!is_stringobject(s)) {
@@ -171,6 +175,36 @@ reprobject(v)
                return (*v->ob_type->tp_repr)(v);
 }
 
+object *
+strobject(v)
+       object *v;
+{
+       if (v == NULL)
+               return newstringobject("<NULL>");
+       if (is_stringobject(v)) {
+               INCREF(v);
+               return v;
+       }
+       else {
+               object *func = getattr(v, "__str__");
+               object *args;
+               object *res;
+               if (func == NULL) {
+                       err_clear();
+                       return reprobject(v);
+               }
+               args = newtupleobject(0);
+               if (args == NULL)
+                       res = NULL;
+               else {
+                       res = call_object(func, args);
+                       DECREF(args);
+               }
+               DECREF(func);
+               return res;
+       }
+}
+
 int
 cmpobject(v, w)
        object *v, *w;
index 9210bd146c3214bfad64d0f95bfcc50b262e1bf5..97ed2f4def58e54e7cd19a52fa3c13f2f0b9f73c 100644 (file)
@@ -1096,12 +1096,7 @@ builtin_str(self, v)
                err_badarg();
                return NULL;
        }
-       if (is_stringobject(v)) {
-               INCREF(v);
-               return v;
-       }
-       else
-               return reprobject(v);
+       return strobject(v);
 }
 
 static object *
index 324ecdf97f79b8e2841825af54747734c371c03f..425e2a06a6cf634b60d1c92ea134493c36e7a847 100644 (file)
@@ -38,6 +38,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #include "graminit.h"
 #include "pythonrun.h"
 
+#include <ctype.h>
+
 /* Turn this on if your compiler chokes on the big switch: */
 /* #define CASE_TOO_BIG 1      /**/
 
@@ -660,16 +662,15 @@ eval_code(co, globals, locals, owner, arg)
                        w = sysget("stdout");
                        if (softspace(w, 1))
                                writestring(" ", w);
-                       if (is_stringobject(v)) {
+                       err = writeobject(v, w, PRINT_RAW);
+                       if (err == 0 && is_stringobject(v)) {
+                               /* XXX move into writeobject() ? */
                                char *s = getstringvalue(v);
                                int len = getstringsize(v);
-                               err = writeobject(v, w, PRINT_RAW);
-                               if (err == 0 && len > 0 && s[len-1] == '\n')
+                               if (len > 0 && isspace(s[len-1]) &&
+                                   s[len-1] != ' ')
                                        softspace(w, 0);
                        }
-                       else {
-                               err = writeobject(v, w, 0);
-                       }
                        DECREF(v);
                        break;