From 3c4f16034602d5ddbeebafb1027ccbda61db36d7 Mon Sep 17 00:00:00 2001 From: Gustavo Massaccesi Date: Tue, 5 Feb 2019 22:53:25 -0300 Subject: [PATCH] make file-stream-port? and terminal-port? total --- .../scribblings/reference/port-procs.scrbl | 22 ++++++++++----- pkgs/racket-test-core/tests/racket/file.rktl | 27 +++++++++++++++++++ racket/src/io/port/file-stream.rkt | 12 +++------ racket/src/racket/src/port.c | 2 -- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/pkgs/racket-doc/scribblings/reference/port-procs.scrbl b/pkgs/racket-doc/scribblings/reference/port-procs.scrbl index 03b7a9607f..d0f16a8c21 100644 --- a/pkgs/racket-doc/scribblings/reference/port-procs.scrbl +++ b/pkgs/racket-doc/scribblings/reference/port-procs.scrbl @@ -47,16 +47,24 @@ determines an output port that is typically used for errors and logging. For example, the default error display handler writes to this port.} -@defproc[(file-stream-port? [port port?]) boolean?]{ -Returns @racket[#t] if the given port is a @tech{file-stream port} (see -@secref["file-ports"]), @racket[#f] otherwise.} +@defproc[(file-stream-port? [v any/c]) boolean?]{ +Returns @racket[#t] if @racket[v] is a @tech{file-stream port} (see +@secref["file-ports"]), @racket[#f] otherwise. -@defproc[(terminal-port? [port port?]) boolean?]{ -Returns @racket[#t] if the given port is attached to an interactive -terminal, @racket[#f] otherwise.} +@history[#:changed "7.2.0.5" @elem{Extended @racket[file-stream-port?] + to any value, instead of resticting + the domain to ports}]} + +@defproc[(terminal-port? [v any/c]) boolean?]{ +Returns @racket[#t] if @racket[v] is a port that is attached to an +interactive terminal, @racket[#f] otherwise. + +@history[#:changed "7.2.0.5" @elem{Extended @racket[terminal-port?] + to any value, instead of resticting + the domain to ports}]} @defthing[eof eof-object?]{A value (distinct from all other values) that represents an end-of-file.} -@defproc[(eof-object? [a any/c]) boolean?]{Returns @racket[#t] if +@defproc[(eof-object? [v any/c]) boolean?]{Returns @racket[#t] if @racket[v] is @racket[eof], @racket[#f] otherwise.} diff --git a/pkgs/racket-test-core/tests/racket/file.rktl b/pkgs/racket-test-core/tests/racket/file.rktl index c1083ee64c..7009188a66 100644 --- a/pkgs/racket-test-core/tests/racket/file.rktl +++ b/pkgs/racket-test-core/tests/racket/file.rktl @@ -11,6 +11,8 @@ (define work-dir (make-temporary-file "path~a" 'directory)) (current-directory work-dir) +(test #t port? (current-input-port)) +(test #t port? (current-output-port)) (test #t input-port? (current-input-port)) (test #t output-port? (current-output-port)) (test #t output-port? (current-error-port)) @@ -18,16 +20,40 @@ (test (void) current-output-port (current-output-port)) (test (void) current-error-port (current-error-port)) (test #t call-with-input-file testing.rktl input-port?) + +(test #f port? 7) +(test #f input-port? 7) +(test #f output-port? 7) +(test #f terminal-port? 7) +(test #f file-stream-port? 7) + (define this-file (open-input-file testing.rktl)) +(test #t port? this-file) (test #t input-port? this-file) +(test #f output-port? this-file) +(test #f terminal-port? this-file) +(test #t file-stream-port? this-file) (close-input-port this-file) + (define this-file (open-input-file testing.rktl #:mode 'binary)) +(test #t port? this-file) (test #t input-port? this-file) +(test #f output-port? this-file) +(test #f terminal-port? this-file) +(test #t file-stream-port? this-file) (close-input-port this-file) + (define this-file (open-input-file testing.rktl #:mode 'text)) +(test #t port? this-file) (test #t input-port? this-file) +(test #f output-port? this-file) +(test #f terminal-port? this-file) +(test #t file-stream-port? this-file) +(arity-test port? 1 1) (arity-test input-port? 1 1) (arity-test output-port? 1 1) +(arity-test terminal-port? 1 1) +(arity-test file-stream-port? 1 1) (arity-test current-input-port 0 1) (arity-test current-output-port 0 1) (arity-test current-error-port 0 1) @@ -61,6 +87,7 @@ (err/rt-test (close-output-port 5)) (err/rt-test (close-input-port (current-output-port))) (err/rt-test (close-output-port (current-input-port))) + (define (check-test-file name) (define test-file (open-input-file name)) (test #t 'input-port? diff --git a/racket/src/io/port/file-stream.rkt b/racket/src/io/port/file-stream.rkt index 188bb9f972..e5c2eec10f 100644 --- a/racket/src/io/port/file-stream.rkt +++ b/racket/src/io/port/file-stream.rkt @@ -12,11 +12,7 @@ (make-struct-type-property 'file-stream)) (define (file-stream-port? p) - (and (file-stream-ref - (or (->core-input-port p #:default #f) - (->core-output-port p #:default #f) - (raise-argument-error 'file-stream-port? - "port?" - p)) - #f) - #t)) + (define core-port (or (->core-input-port p #:default #f) + (->core-output-port p #:default #f)) + (and (file-stream-ref core-port #f) #t)) + diff --git a/racket/src/racket/src/port.c b/racket/src/racket/src/port.c index 3dcb34ad23..b71c6e1b5b 100644 --- a/racket/src/racket/src/port.c +++ b/racket/src/racket/src/port.c @@ -3291,8 +3291,6 @@ scheme_file_stream_port_p (int argc, Scheme_Object *argv[]) return scheme_true; else if (SAME_OBJ(op->sub_type, fd_output_port_type)) return scheme_true; - } else { - scheme_wrong_contract("file-stream-port?", "port?", 0, argc, argv); } return scheme_false;