From xemacs-m  Mon Jul 21 17:25:37 1997
Received: from altair.xemacs.org (steve@xemacs.miranova.com [206.190.83.19])
	by xemacs.org (8.8.5/8.8.5) with ESMTP id RAA06804
	for <xemacs-beta@xemacs.org>; Mon, 21 Jul 1997 17:25:36 -0500 (CDT)
Received: (from steve@localhost)
	by altair.xemacs.org (8.8.6/8.8.6) id PAA18810;
	Mon, 21 Jul 1997 15:29:08 -0700
Mail-Copies-To: never
To: xemacs-beta@xemacs.org
Subject: Re: Question about lispref: printing of circular structures
References: <87zprgqxpu.fsf@bittersweet.inetarena.com>
X-Face: `'%\i;ySOu]g?NlziJSk_$&@]KP`}~PEQPjZ5;nxSaDW_o$4+4%Ab]%Ifw3ZR;7TIT3,O,'
 @2{L;]ox6kc;$_5kU'n**9vFg-]eV~GbxSVCx|(s%uR[],*:^WKmC`B}(;|k9/m]gwt?&`t;^rfCJg
 khHH>pP1W\)xM0U@!FNDD72{3fDP$PkBhx^7Z?-WxH6DbFN:QOnT`llzW}VGdYv;n9lzljQvKTIBhQ
 YuV
X-Attribution: sb
From: SL Baur <steve@xemacs.org>
In-Reply-To: karlheg+xemacs@inetarena.com's message of "21 Jul 1997 07:35:57 -0700"
Mime-Version: 1.0 (generated by tm-edit 7.108)
Content-Type: text/plain; charset=US-ASCII
Date: 21 Jul 1997 15:29:07 -0700
Message-ID: <m290z0ghu4.fsf@altair.xemacs.org>
Lines: 61
X-Mailer: Gnus v5.4.64/XEmacs 20.3(beta15) - "Berlin"

Karl M Hegbloom <karlheg+xemacs@inetarena.com> writes:

>  I just tried this example, and rather than doing what it says it
> will, XEmacs thrums and says "Apparently circular structure being
> printed".

(setq foo (list nil))
=> (nil)
(setcar foo foo)
=> (#0)

1997-07-21  SL Baur  <steve@altair.xemacs.org>

	* print.c (print_internal): Handle circular objects like Emacs
	handles them (and as documented in the Lispref).

Index: src/print.c
===================================================================
RCS file: /usr/local/xemacs/xemacs-20.0/src/print.c,v
retrieving revision 1.8
diff -u -r1.8 print.c
--- print.c	1997/07/13 22:42:53	1.8
+++ print.c	1997/07/21 22:11:59
@@ -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))

