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

Re: with-open-file



[Message forwarded from Gilbert Baumann <gilbert@ma2s2.mathematik.uni-karlsruhe.de>.]

sshteingold@cctrading.com writes:
> 
>      > (macroexpand  '(with-open-file (str "file" :direction :io) (format 
>      str "zz")))
>      
>      (let ((str (open "file" :direction :io)))
>        (unwind-protect
>          (multiple-value-prog1 (progn (format str "zz")) (when str (close 
>      str)))
>          (when str (close str :abort t))
>      ) ) ;
>      t
>      > 
>      
>      why close is done twice?
>      Why not just
>      (unwind-protect (progn ,@body) (when str (close str :abort t)))
>      ?

At least I assume WITH-OPEN-FILE to close the stream regulary [:abort
being nil], when leaving the body normally. I never use a a call to
close within WITH-OPEN-FILE. The :abort option should only been used
when the body is left thru' some non-local exit [THROW, RETURN-FROM,
ERROR etc.]

Let me quote the relevant part from CLTL2 (p 651):

<quote>
     When control leaves the body, either normally or abnormally (such
     as by use of `throw'), the file is automatically closed.  If a new
     output file is being written, and control leaves abnormally, the
     file is aborted and the file system is left, so far as possible,
     as if the file had never been opened.  Because `with-open-file'
     always closes the file, even when an error exit is taken, it is
     preferred over `open' for most applications.
</quote>

In my opinion the implementation of WITH-OPEN-FILE is 100% correct in
CLISP.

Gilbert

-- 
;;; You know you have hacked Lisp too much, when you m-c-x in a C buffer.