mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-09 23:40:29 +02:00
Fix call-with-input-file & relatives for multiple values
* module/ice-9/r4rs.scm (call-with-input-file, call-with-output-file): Rewrite with call-with-values. (with-input-from-file): use call-with-input-file. (with-output-to-file, with-error-to-file): use call-with-output-file. Update docstrings to make clear that multiple values may be yielded.
This commit is contained in:
parent
f3c6a02c88
commit
7ff0f239b2
1 changed files with 25 additions and 27 deletions
|
@ -1,7 +1,7 @@
|
||||||
;;;; r4rs.scm --- definitions needed for libguile to be R4RS compliant
|
;;;; r4rs.scm --- definitions needed for libguile to be R4RS compliant
|
||||||
;;;; Jim Blandy <jimb@cyclic.com> --- October 1996
|
;;;; Jim Blandy <jimb@cyclic.com> --- October 1996
|
||||||
|
|
||||||
;;;; Copyright (C) 1996, 1997, 1998, 2000, 2001, 2006, 2010 Free Software Foundation, Inc.
|
;;;; Copyright (C) 1996, 1997, 1998, 2000, 2001, 2006, 2010, 2011 Free Software Foundation, Inc.
|
||||||
;;;;
|
;;;;
|
||||||
;;;; This library is free software; you can redistribute it and/or
|
;;;; This library is free software; you can redistribute it and/or
|
||||||
;;;; modify it under the terms of the GNU Lesser General Public
|
;;;; modify it under the terms of the GNU Lesser General Public
|
||||||
|
@ -140,14 +140,16 @@ already exist. These procedures call PROC
|
||||||
with one argument: the port obtained by opening the named file for
|
with one argument: the port obtained by opening the named file for
|
||||||
input or output. If the file cannot be opened, an error is
|
input or output. If the file cannot be opened, an error is
|
||||||
signalled. If the procedure returns, then the port is closed
|
signalled. If the procedure returns, then the port is closed
|
||||||
automatically and the value yielded by the procedure is returned.
|
automatically and the values yielded by the procedure are returned.
|
||||||
If the procedure does not return, then the port will not be closed
|
If the procedure does not return, then the port will not be closed
|
||||||
automatically unless it is possible to prove that the port will
|
automatically unless it is possible to prove that the port will
|
||||||
never again be used for a read or write operation."
|
never again be used for a read or write operation."
|
||||||
(let* ((file (open-input-file str))
|
(let ((p (open-input-file str)))
|
||||||
(ans (proc file)))
|
(call-with-values
|
||||||
(close-input-port file)
|
(lambda () (proc p))
|
||||||
ans))
|
(lambda vals
|
||||||
|
(close-input-port p)
|
||||||
|
(apply values vals)))))
|
||||||
|
|
||||||
(define (call-with-output-file str proc)
|
(define (call-with-output-file str proc)
|
||||||
"PROC should be a procedure of one argument, and STR should be a
|
"PROC should be a procedure of one argument, and STR should be a
|
||||||
|
@ -156,14 +158,16 @@ already exists. These procedures call PROC
|
||||||
with one argument: the port obtained by opening the named file for
|
with one argument: the port obtained by opening the named file for
|
||||||
input or output. If the file cannot be opened, an error is
|
input or output. If the file cannot be opened, an error is
|
||||||
signalled. If the procedure returns, then the port is closed
|
signalled. If the procedure returns, then the port is closed
|
||||||
automatically and the value yielded by the procedure is returned.
|
automatically and the values yielded by the procedure are returned.
|
||||||
If the procedure does not return, then the port will not be closed
|
If the procedure does not return, then the port will not be closed
|
||||||
automatically unless it is possible to prove that the port will
|
automatically unless it is possible to prove that the port will
|
||||||
never again be used for a read or write operation."
|
never again be used for a read or write operation."
|
||||||
(let* ((file (open-output-file str))
|
(let ((p (open-output-file str)))
|
||||||
(ans (proc file)))
|
(call-with-values
|
||||||
(close-output-port file)
|
(lambda () (proc p))
|
||||||
ans))
|
(lambda vals
|
||||||
|
(close-output-port p)
|
||||||
|
(apply values vals)))))
|
||||||
|
|
||||||
(define (with-input-from-port port thunk)
|
(define (with-input-from-port port thunk)
|
||||||
(let* ((swaports (lambda () (set! port (set-current-input-port port)))))
|
(let* ((swaports (lambda () (set! port (set-current-input-port port)))))
|
||||||
|
@ -184,13 +188,11 @@ input, an input port connected to it is made
|
||||||
the default value returned by `current-input-port',
|
the default value returned by `current-input-port',
|
||||||
and the THUNK is called with no arguments.
|
and the THUNK is called with no arguments.
|
||||||
When the THUNK returns, the port is closed and the previous
|
When the THUNK returns, the port is closed and the previous
|
||||||
default is restored. Returns the value yielded by THUNK. If an
|
default is restored. Returns the values yielded by THUNK. If an
|
||||||
escape procedure is used to escape from the continuation of these
|
escape procedure is used to escape from the continuation of these
|
||||||
procedures, their behavior is implementation dependent."
|
procedures, their behavior is implementation dependent."
|
||||||
(let* ((nport (open-input-file file))
|
(call-with-input-file file
|
||||||
(ans (with-input-from-port nport thunk)))
|
(lambda (p) (with-input-from-port p thunk))))
|
||||||
(close-port nport)
|
|
||||||
ans))
|
|
||||||
|
|
||||||
(define (with-output-to-file file thunk)
|
(define (with-output-to-file file thunk)
|
||||||
"THUNK must be a procedure of no arguments, and FILE must be a
|
"THUNK must be a procedure of no arguments, and FILE must be a
|
||||||
|
@ -199,13 +201,11 @@ The file is opened for output, an output port connected to it is made
|
||||||
the default value returned by `current-output-port',
|
the default value returned by `current-output-port',
|
||||||
and the THUNK is called with no arguments.
|
and the THUNK is called with no arguments.
|
||||||
When the THUNK returns, the port is closed and the previous
|
When the THUNK returns, the port is closed and the previous
|
||||||
default is restored. Returns the value yielded by THUNK. If an
|
default is restored. Returns the values yielded by THUNK. If an
|
||||||
escape procedure is used to escape from the continuation of these
|
escape procedure is used to escape from the continuation of these
|
||||||
procedures, their behavior is implementation dependent."
|
procedures, their behavior is implementation dependent."
|
||||||
(let* ((nport (open-output-file file))
|
(call-with-output-file file
|
||||||
(ans (with-output-to-port nport thunk)))
|
(lambda (p) (with-output-to-port p thunk))))
|
||||||
(close-port nport)
|
|
||||||
ans))
|
|
||||||
|
|
||||||
(define (with-error-to-file file thunk)
|
(define (with-error-to-file file thunk)
|
||||||
"THUNK must be a procedure of no arguments, and FILE must be a
|
"THUNK must be a procedure of no arguments, and FILE must be a
|
||||||
|
@ -214,13 +214,11 @@ The file is opened for output, an output port connected to it is made
|
||||||
the default value returned by `current-error-port',
|
the default value returned by `current-error-port',
|
||||||
and the THUNK is called with no arguments.
|
and the THUNK is called with no arguments.
|
||||||
When the THUNK returns, the port is closed and the previous
|
When the THUNK returns, the port is closed and the previous
|
||||||
default is restored. Returns the value yielded by THUNK. If an
|
default is restored. Returns the values yielded by THUNK. If an
|
||||||
escape procedure is used to escape from the continuation of these
|
escape procedure is used to escape from the continuation of these
|
||||||
procedures, their behavior is implementation dependent."
|
procedures, their behavior is implementation dependent."
|
||||||
(let* ((nport (open-output-file file))
|
(call-with-output-file file
|
||||||
(ans (with-error-to-port nport thunk)))
|
(lambda (p) (with-error-to-port p thunk))))
|
||||||
(close-port nport)
|
|
||||||
ans))
|
|
||||||
|
|
||||||
(define (with-input-from-string string thunk)
|
(define (with-input-from-string string thunk)
|
||||||
"THUNK must be a procedure of no arguments.
|
"THUNK must be a procedure of no arguments.
|
||||||
|
@ -228,7 +226,7 @@ The test of STRING is opened for
|
||||||
input, an input port connected to it is made,
|
input, an input port connected to it is made,
|
||||||
and the THUNK is called with no arguments.
|
and the THUNK is called with no arguments.
|
||||||
When the THUNK returns, the port is closed.
|
When the THUNK returns, the port is closed.
|
||||||
Returns the value yielded by THUNK. If an
|
Returns the values yielded by THUNK. If an
|
||||||
escape procedure is used to escape from the continuation of these
|
escape procedure is used to escape from the continuation of these
|
||||||
procedures, their behavior is implementation dependent."
|
procedures, their behavior is implementation dependent."
|
||||||
(call-with-input-string string
|
(call-with-input-string string
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue