[plt-scheme] confusing behavior with "reencode-input-port"

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Tue Jan 6 14:24:10 EST 2009

I'm confused about a behavior of "reencode-input-port".

If the input is a port created with "open-input-bytes", then it works as 

If the input port is created with "open-input-string", however, then 
"reencode-input-port" has an effect that looks like the input is being 
*doubly* reencoded.

I have reduced my confusion to the below demonstration code.  Perhaps 
someone else can immediately see what I am doing wrong?

(require mzlib/port)

;; First, these two expressions are as expected.

 (read-bytes 1000
             (open-input-string (bytes->string/latin-1 (bytes 169)))))
;; ==> (194 169)

 (read-char (open-input-string (bytes->string/latin-1 (bytes 169)))))
;; ==> 169

;; The following is what is confusing.

(define (foo in-port)
  (let* ((re-in-port (reencode-input-port
                      in-port           ; in
                      "ISO-8859-1"      ; encoding
                      #f                ; error-bytes
                      #f                ; close?
                      "foo"             ; name
                      #f                ; convert-newlines?
    (begin0 (bytes->list (read-bytes 1000 re-in-port))
      (close-input-port re-in-port))))

(let ((in-bytes (bytes 169)))
     (input  ,(bytes->list in-bytes))
     (output ,(foo (open-input-bytes in-bytes))))
     ,@(let* ((in-str (bytes->string/latin-1 in-bytes)))
         `((input  ,(bytes->list (string->bytes/latin-1 in-str)))
           (output ,(foo (open-input-string in-str))))))))
;; ==>
;; ((BYTES-INPUT-PORT  (input (169)) (output (194 169)))
;;  (STRING-INPUT-PORT (input (169)) (output (195 130 194 169))))

Posted on the users mailing list.