unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* multiple values bug in head / call-with-input-file
@ 2011-05-02 13:23 Daniel Llorens
  2011-05-02 17:46 ` Mark H Weaver
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Llorens @ 2011-05-02 13:23 UTC (permalink / raw)
  To: bug-guile


Hello,

I noticed this:

scheme@(guile-user)> (call-with-input-string "hello" (lambda (p) (values 1 2)))
$1 = 1
$2 = 2

but:

scheme@(guile-user)> (call-with-input-file "hello" (lambda (p) (values 1 2)))
$1 = 1

I look up call-with-input-file and I see:

(define (call-with-input-file str proc)
  (let* ((file (open-input-file str))
         (ans (proc file)))
    (close-input-port file)
    ans))

Now, in Guile 1.8:

guile> (let ((a (values 1 2))) a)
1
2

But in Guile 2.0.1:

scheme@(guile-user)> (let ((a (values 1 2))) a)
$1 = 1

The second one breaks call-with-input-file, but probably both should be errors.

Anyway, here is a patch for call-with-input/output-file that avoids the issue.

Regards,
	
	Daniel



From 06f8aea901cd3da68a409a9932757209d91efc40 Mon Sep 17 00:00:00 2001
From: Daniel Llorens <daniel.llorens@bluewin.ch>
Date: Mon, 2 May 2011 14:54:20 +0200
Subject: [PATCH] Fix call-with-input-file, call-with-output-file with multiple values

* module/ice-9/r4rs.scm: Write call-with-input-file, call-with-input-file in terms of dynamic-wind.
---
 module/ice-9/r4rs.scm |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/module/ice-9/r4rs.scm b/module/ice-9/r4rs.scm
index 4d3feba..337e196 100644
--- a/module/ice-9/r4rs.scm
+++ b/module/ice-9/r4rs.scm
@@ -144,10 +144,11 @@ automatically and the value yielded by the procedure is returned.
 If the procedure does not return, then the port will not be closed
 automatically unless it is possible to prove that the port will
 never again be used for a read or write operation."
-  (let* ((file (open-input-file str))
-	 (ans (proc file)))
-    (close-input-port file)
-    ans))
+  (let ((port #f))
+    (dynamic-wind
+      (lambda () (set! port (open-input-file str)))
+      (lambda () (proc port))
+      (lambda () (if port (close-input-port port))))))
 
 (define (call-with-output-file str proc)
   "PROC should be a procedure of one argument, and STR should be a
@@ -160,10 +161,11 @@ automatically and the value yielded by the procedure is returned.
 If the procedure does not return, then the port will not be closed
 automatically unless it is possible to prove that the port will
 never again be used for a read or write operation."
-  (let* ((file (open-output-file str))
-	 (ans (proc file)))
-    (close-output-port file)
-    ans))
+  (let ((port #f))
+    (dynamic-wind
+      (lambda () (set! port (open-output-file str)))
+      (lambda () (proc port))
+      (lambda () (if port (close-output-port port))))))
 
 (define (with-input-from-port port thunk)
   (let* ((swaports (lambda () (set! port (set-current-input-port port)))))
-- 
1.7.1





^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-05-02 21:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-02 13:23 multiple values bug in head / call-with-input-file Daniel Llorens
2011-05-02 17:46 ` Mark H Weaver
2011-05-02 19:09   ` Daniel Llorens
2011-05-02 20:55     ` Andy Wingo
2011-05-02 21:20       ` Daniel Llorens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).