Commit 8a3fbb66 authored by mevenson@1c010e3e-69d0-11dd-93a8-456734b0d56f's avatar mevenson@1c010e3e-69d0-11dd-93a8-456734b0d56f
Browse files

Fix gray streams for FLEXI-STREAMS.

From Theam Yang Chew, who writes on
http://article.gmane.org/gmane.lisp.armedbear.devel/3059:

Hi all,

I inadvertently discovered some bugs in the gray-streams
implementation due to a stale flexi-streams that did not get upgraded
properly.

There were quite a few things that needed tidying - the type checking
using EQ is wrong. Also, after trying to hunt around CLHS, I think
BYTE is not a CL type (though I may be mistaken). This could be a
reflection of some Corman Lisp specific feature in the past (Yes!
These bugs seem to have been around ABCL for that long!)

Some test examples (avoiding flexi-streams or other dependencies)

;; ----- setup

(require 'gray-streams)

(defclass test-gray-binary-input-stream
(gray-streams:fundamental-binary-input-stream)
 ())

(defclass test-gray-character-input-stream
(gray-streams:fundamental-character-input-stream)
 ())

(let ((bytes (list 65 66 67 68 69))
     (pos -1))
 (defmethod gray-streams::stream-read-byte ((stream
test-gray-binary-input-stream))
   (elt bytes (mod (incf pos) 5))))

(let ((chars (list #\A #\B #\C #\D #\E))
     (pos -1))
 (defmethod gray-streams::stream-read-char ((stream
test-gray-character-input-stream))
   (elt chars (mod (incf pos) 5))))

;; -----

(let ((s (make-instance 'test-gray-binary-input-stream)))
 (loop repeat 10 collect (read-byte s)))
=> (65 66 67 68 69 65 66 67 68 69)

(let ((arr (make-array 10 :initial-element nil)))
 (list (read-sequence arr
                      (make-instance 'test-gray-binary-input-stream)
                      :start 2 :end 7)
       arr))

Expected (7 #(NIL NIL 65 66 67 68 69 NIL NIL NIL)), but got an error
instead, ... no applicable method...

I guess implementors of gray streams would normally define both
read-byte & read-sequence. But ABCL's default/fallback method does try
to support binary streams, it just isn't being dispatched on the right
class.

Also, read-sequence should return the "final" array index, not the
number of bytes/characters read. Similarly, write-sequence needs to do
its work, then return the sequence itself.

Attached is a patch with what I hope are the required fixes, please
review.

Two additional notes,

1. Even after loading my proposed fixes, I'd would still get this,
which is ok, but the error message is not obvious enough. This is
actually a method not found error, not a type error. There appears
to be some type guessing going on, so perhaps the message should
indicate failure to dispatch on STREAM or similar.

(gray-streams::stream-element-type
(make-instance 'test-gray-binary-input-stream))
The value #<TEST-GRAY-BINARY-INPUT-STREAM {500BBBF9}> is not of type STREAM.
  [Condition of type TYPE-ERROR]

So we just need something like this,

(defmethod gray-streams::stream-element-type ((stream
test-gray-binary-input-stream))
 '(unsigned-byte 8))

2. I also don't understand this snippet in the original code,
converting unsigned-bytes/integers (basically the wrong type) to a
character?

(#+nil ccl:int-char code-char (elt sequence n))


Of course, after patching the above, I discovered that re-compiling
flexi-streams
got rid of my particular set of problems :-)
parent 8bbea7ec
......@@ -343,45 +343,43 @@
(dotimes (i (- current column) t)
(stream-write-char stream #\Space)))))
(defun basic-read-sequence (stream sequence start end
expected-element-type read-fun)
(let ((element-type (stream-element-type stream)))
(if (subtypep element-type expected-element-type)
(dotimes (count (- end start)
;; If (< end start), skip the dotimes body but
;; return start
(max start end))
(let ((el (funcall read-fun stream)))
(when (eq el :eof)
(return (+ count start)))
(setf (elt sequence (+ count start)) el)))
(error "Cannot READ-SEQUENCE on stream of :ELEMENT-TYPE ~A"
element-type))))
(defun basic-write-sequence (stream sequence start end
expected-element-type write-fun)
(let ((element-type (stream-element-type stream)))
(if (subtypep element-type expected-element-type)
;; Avoid LOOP because it isn't loaded yet
(do ((n start (+ n 1)))
((= n end))
(funcall write-fun stream (elt sequence n)))
(error "Cannot WRITE-SEQUENCE on stream of :ELEMENT-TYPE ~A"
element-type)))
(stream-force-output stream)
sequence)
(defmethod stream-read-sequence ((stream fundamental-character-input-stream)
sequence &optional (start 0) end)
(let ((element-type (stream-element-type stream))
(end (or end (length sequence)))
(eof (cons nil nil)))
(cond
((eq element-type 'character)
(dotimes (count (- end start) (- end start))
(let ((c (stream-read-char stream nil eof)))
(if (eq c eof)
(return (+ count start)))
(setf (elt sequence (+ count start)) c))))
((or (eq element-type 'byte)
(eq element-type 'unsigned-byte)
(eq element-type 'signed-byte))
(dotimes (count (- end start) (- end start))
(let ((b (stream-read-byte stream nil eof)))
(if (eq b eof)
(return (+ count start)))
(setf (elt sequence (+ count start)) b))))
(t (error "Cannot READ-SEQUENCE on stream of :ELEMENT-TYPE ~A"
element-type)))))
(basic-read-sequence stream sequence start (or end (length sequence))
'character #'stream-read-char))
(defmethod stream-write-sequence ((stream fundamental-character-output-stream)
sequence &optional (start 0) end)
(let ((element-type (stream-element-type stream))
(end (or end (length sequence))))
(if (eq element-type 'character)
(do ((n start (+ n 1)))
((= n end))
(stream-write-char
stream
(if (typep (elt sequence n) 'number)
(#+nil ccl:int-char code-char (elt sequence n))
(elt sequence n))))
(do ((n start (+ n 1)))
((= n end))
(stream-write-byte (elt sequence n) stream)))) ;; recoded to avoid LOOP, because it isn't loaded yet
(stream-force-output stream))
(basic-write-sequence stream sequence start (or end (length sequence))
'character #'stream-write-char))
(defclass fundamental-binary-input-stream
(fundamental-input-stream fundamental-binary-stream))
......@@ -389,6 +387,16 @@
(defclass fundamental-binary-output-stream
(fundamental-output-stream fundamental-binary-stream))
(defmethod stream-read-sequence ((stream fundamental-binary-input-stream)
sequence &optional (start 0) end)
(basic-read-sequence stream sequence start (or end (length sequence))
'signed-byte #'stream-read-byte))
(defmethod stream-write-sequence ((stream fundamental-binary-output-stream)
sequence &optional (start 0) end)
(basic-write-sequence stream sequence start (or end (length sequence))
'signed-byte #'stream-write-byte))
(defun decode-read-arg (arg)
(cond ((null arg) *standard-input*)
((eq arg t) *terminal-io*)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment