diff src/print.c @ 177:6075d714658b r20-3b15

Import from CVS: tag r20-3b15
author cvs
date Mon, 13 Aug 2007 09:51:16 +0200
parents 8eaf7971accc
children e121b013d1f0
line wrap: on
line diff
--- a/src/print.c	Mon Aug 13 09:50:16 2007 +0200
+++ b/src/print.c	Mon Aug 13 09:51:16 2007 +0200
@@ -54,6 +54,10 @@
 /* Avoid actual stack overflow in print.  */
 static int print_depth;
 
+/* Detect most circularities to print finite output.  */
+#define PRINT_CIRCLE 200
+Lisp_Object being_printed[PRINT_CIRCLE];
+
 /* Maximum length of list or vector to print in full; noninteger means
    effectively infinity */
 
@@ -901,9 +905,25 @@
      output. */
 #endif
 
+  /* Detect circularities and truncate them.
+     No need to offer any alternative--this is better than an error.  */
+  if (CONSP (obj) || VECTORP (obj) || COMPILED_FUNCTIONP (obj))
+    {
+      int i;
+      for (i = 0; i < print_depth; i++)
+	if (EQ (obj, being_printed[i]))
+	  {
+	    sprintf (buf, "#%d", i);
+	    write_c_string (buf, printcharfun);
+	    return;
+	  }
+    }
+
+
+  being_printed[print_depth] = obj;
   print_depth++;
 
-  if (print_depth > 200)
+  if (print_depth > PRINT_CIRCLE)
     error ("Apparently circular structure being printed");
 
   switch (XTYPE (obj))