Raymond Toy pushed to branch issue-373-handle-temp-files at cmucl / cmucl

Commits:

2 changed files:

Changes:

  • src/code/extensions.lisp
    ... ... @@ -645,57 +645,6 @@
    645 645
     				default-name))
    
    646 646
     	       "XXXXXX"))
    
    647 647
     
    
    648
    -;;; WITH-TEMPORARY-STREAM  -- Public
    
    649
    -;;;
    
    650
    -(defmacro with-temporary-stream ((s &key
    
    651
    -				      (direction :output)
    
    652
    -				      (element-type 'base-char)
    
    653
    -				      (external-format :default)
    
    654
    -				      decoding-error
    
    655
    -				      encoding-error)
    
    656
    -				 &parse-body (forms decls))
    
    657
    -  _N"Return a stream to a temporary file that is automatically created."
    
    658
    -  (let ((fd (gensym "FD-"))
    
    659
    -	(filename (gensym "FILENAME-"))
    
    660
    -	(dir (gensym "DIRECTION-"))
    
    661
    -	(okay (gensym "OKAY-"))
    
    662
    -	(err (gensym "ERR-"))
    
    663
    -	(file-template (gensym "FILE-TEMPLATE-")))
    
    664
    -    `(progn
    
    665
    -       (unless (member ,direction '(:output :io))
    
    666
    -	 (error ":direction must be one of :output or :io, not ~S"
    
    667
    -		,direction))
    
    668
    -       (let ((,file-template (create-template nil "cmucl-temp-stream-"))
    
    669
    -	     ,fd ,filename ,s)
    
    670
    -	 (unwind-protect
    
    671
    -	      (progn
    
    672
    -		(multiple-value-setq (,fd ,filename)
    
    673
    -		  (unix::unix-mkstemp ,file-template))
    
    674
    -		(unless ,fd
    
    675
    -		  (error "Unable to create temporary stream at ~S: ~A~%"
    
    676
    -			 ,file-template
    
    677
    -			 (unix:get-unix-error-msg ,filename)))
    
    678
    -		(let* ((,dir ,direction))
    
    679
    -		  (setf ,s (make-fd-stream ,fd
    
    680
    -					   :input (member ,dir '(:input :io))
    
    681
    -					   :output (member ,dir '(:output :io))
    
    682
    -					   :element-type ',element-type
    
    683
    -					   :external-format ,external-format
    
    684
    -					   :decoding-error ,decoding-error
    
    685
    -					   :encoding-error ,encoding-error)))
    
    686
    -		;; Delete the file; we have an open fd to the file, though.
    
    687
    -		(multiple-value-bind (,okay ,err)
    
    688
    -		    (unix::unix-unlink ,filename)
    
    689
    -		  (unless ,okay
    
    690
    -		    (error "Unable to unlink temporary file ~S: ~A"
    
    691
    -			   ,filename (unix:get-unix-error-msg ,err))))
    
    692
    -		(locally ,@decls
    
    693
    -		  ,@forms))
    
    694
    -	   ;; Close the stream which will close the fd now that we're
    
    695
    -	   ;; done.
    
    696
    -	   (when ,s
    
    697
    -	     (close ,s)))))))
    
    698
    -
    
    699 648
     ;;; WITH-TEMPORARY-FILE  -- Public
    
    700 649
     (defmacro with-temporary-file ((filename &key prefix)
    
    701 650
     			       &parse-body (forms decls))
    

  • tests/fd-streams.lisp
    ... ... @@ -36,6 +36,25 @@
    36 36
     	  (file-length s))
    
    37 37
          (delete-file *test-file*))))
    
    38 38
     
    
    39
    +(define-test clear-output-1
    
    40
    +    (:tag :trac)
    
    41
    +  (assert-eql
    
    42
    +   0
    
    43
    +   (ext:with-temporary-file (test-file)
    
    44
    +     (let ((s (open test-file
    
    45
    +		    :direction :output
    
    46
    +		    :if-exists :supersede)))
    
    47
    +       ;; Write a character to the (fully buffered) output
    
    48
    +       ;; stream. Clear the output and close the file. Nothing
    
    49
    +       ;; should have been written to the file.
    
    50
    +       (write-char #\a s)
    
    51
    +       (clear-output s)
    
    52
    +       (close s)
    
    53
    +       (setf s (open test-file))
    
    54
    +       (prog1
    
    55
    +	   (file-length s)
    
    56
    +	 (close s))))))
    
    57
    +
    
    39 58
     (define-test file-position.1
    
    40 59
         (:tag :issues)
    
    41 60
       ;; Create a short test file