bug-guile
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: multiple values bug in head / call-with-input-file


From: Mark H Weaver
Subject: Re: multiple values bug in head / call-with-input-file
Date: Mon, 02 May 2011 13:46:45 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)

Daniel Llorens <address@hidden> writes:
> 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

Indeed this is suboptimal, and probably a bug.
Thanks for reporting this!

However, your fix is incorrect.  By using dynamic-wind, your patch
significantly changes the semantics of call-with-{input,output}-file.
The docs state:

  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.

By using dynamic-wind, you have changed this.  Now, if a continuation is
invoked that causes control to leave the dynamic extent of
call-with-{input,output}-file, the file will be closed.  This is
different from what the docs claim.

Furthermore, if control re-enters that dynamic extent, the file will be
_re-opened_ with a _fresh_ port.  This causes several problems.  First,
the code within proc is probably not expecting that, and will continue
to have copies of the old port around.  To make matters worse:
call-with-input-file will start reading from the beginning of the file
again, and call-with-output-file will _truncate_ the file and resume
writing from the beginning.

I believe the proper fix is something like this:

  (define (call-with-input-file str proc)
    (let ((file (open-input-file str)))
      (call-with-values
        (lambda () (proc file))
        (lambda vals
          (close-input-port file)
          (apply values vals)))))

Would you like to prepare a new patch?

     Best,
      Mark


> From 06f8aea901cd3da68a409a9932757209d91efc40 Mon Sep 17 00:00:00 2001
> From: Daniel Llorens <address@hidden>
> 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)))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]