From 6e3dee43d4b8a6f6254fed105aa05546b67a3abf Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 30 Jul 2010 15:58:33 -0400 Subject: [PATCH] io.ports: calling '0 read' now returns an empty sequence without blocking (reported by Brennan Cheung) --- basis/io/pipes/pipes-tests.factor | 17 +++++++++++++---- basis/io/ports/ports.factor | 15 ++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/basis/io/pipes/pipes-tests.factor b/basis/io/pipes/pipes-tests.factor index 5ece6cfdf3..0f15faff90 100644 --- a/basis/io/pipes/pipes-tests.factor +++ b/basis/io/pipes/pipes-tests.factor @@ -1,7 +1,7 @@ USING: io io.pipes io.streams.string io.encodings.utf8 -io.streams.duplex io.encodings io.timeouts namespaces -continuations tools.test kernel calendar destructors -accessors debugger math ; +io.encodings.binary io.streams.duplex io.encodings io.timeouts +namespaces continuations tools.test kernel calendar destructors +accessors debugger math sequences ; IN: io.pipes.tests [ "Hello" ] [ @@ -28,7 +28,7 @@ IN: io.pipes.tests [ utf8 [ - 5 seconds over set-timeout + 1 seconds over set-timeout stream-readln ] with-disposal ] must-fail @@ -42,3 +42,12 @@ IN: io.pipes.tests ] curry ignore-errors ] times ] unit-test + +! 0 read should not block +[ f ] [ + [ + binary &dispose + in>> + [ 0 read ] with-input-stream + ] with-destructors +] unit-test diff --git a/basis/io/ports/ports.factor b/basis/io/ports/ports.factor index 8517910b0f..6c2f75ec80 100644 --- a/basis/io/ports/ports.factor +++ b/basis/io/ports/ports.factor @@ -46,11 +46,17 @@ M: input-port stream-read1 dup wait-to-read [ drop f ] [ buffer>> buffer-pop ] if ; inline : read-step ( count port -- byte-array/f ) - dup wait-to-read [ 2drop f ] [ buffer>> buffer-read ] if ; + { + { [ over 0 = ] [ 2drop f ] } + { [ dup wait-to-read ] [ 2drop f ] } + [ buffer>> buffer-read ] + } cond ; + +: prepare-read ( count stream -- count stream ) + dup check-disposed [ 0 max >fixnum ] dip ; inline M: input-port stream-read-partial ( max stream -- byte-array/f ) - dup check-disposed - [ 0 max >integer ] dip read-step ; + prepare-read read-step ; : read-loop ( count port accum -- ) pick over length - dup 0 > [ @@ -64,8 +70,7 @@ M: input-port stream-read-partial ( max stream -- byte-array/f ) ] if ; M: input-port stream-read - dup check-disposed - [ 0 max >fixnum ] dip + prepare-read 2dup read-step dup [ pick over length > [ pick