From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Newsgroups: gmane.lisp.guile.bugs Subject: bug#30066: 'get-bytevector-some' returns only 1 byte from unbuffered ports Date: Wed, 10 Jan 2018 16:59:29 +0100 Message-ID: <87tvvtr9ge.fsf@gnu.org> References: <87zi5lrc3x.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1515599909 14231 195.159.176.226 (10 Jan 2018 15:58:29 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 10 Jan 2018 15:58:29 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) Cc: Andy Wingo To: 30066@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Wed Jan 10 16:58:25 2018 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZIlh-0003Fv-0u for guile-bugs@m.gmane.org; Wed, 10 Jan 2018 16:58:21 +0100 Original-Received: from localhost ([::1]:50504 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZIng-0004xX-IG for guile-bugs@m.gmane.org; Wed, 10 Jan 2018 11:00:24 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40282) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZInU-0004qz-Ip for bug-guile@gnu.org; Wed, 10 Jan 2018 11:00:15 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZInK-0006fc-Lk for bug-guile@gnu.org; Wed, 10 Jan 2018 11:00:12 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:43293) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eZInK-0006fK-Hl for bug-guile@gnu.org; Wed, 10 Jan 2018 11:00:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1eZInK-0003oC-6k for bug-guile@gnu.org; Wed, 10 Jan 2018 11:00:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Wed, 10 Jan 2018 16:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 30066 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 30066-submit@debbugs.gnu.org id=B30066.151559997614579 (code B ref 30066); Wed, 10 Jan 2018 16:00:02 +0000 Original-Received: (at 30066) by debbugs.gnu.org; 10 Jan 2018 15:59:36 +0000 Original-Received: from localhost ([127.0.0.1]:51190 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZImu-0003n5-CP for submit@debbugs.gnu.org; Wed, 10 Jan 2018 10:59:36 -0500 Original-Received: from hera.aquilenet.fr ([185.233.100.1]:53600) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZImr-0003mv-3e for 30066@debbugs.gnu.org; Wed, 10 Jan 2018 10:59:34 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id A2C6F100F8; Wed, 10 Jan 2018 16:59:31 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at aquilenet.fr Original-Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Ko28KrS6z37x; Wed, 10 Jan 2018 16:59:30 +0100 (CET) Original-Received: from ribbon (unknown [IPv6:2a01:e0a:1d:7270:af76:b9b:ca24:c465]) by hera.aquilenet.fr (Postfix) with ESMTPSA id 8F938100EE; Wed, 10 Jan 2018 16:59:30 +0100 (CET) In-Reply-To: <87zi5lrc3x.fsf@gnu.org> ("Ludovic \=\?utf-8\?Q\?Court\=C3\=A8s\=22'\?\= \=\?utf-8\?Q\?s\?\= message of "Wed, 10 Jan 2018 16:02:10 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.org gmane.lisp.guile.bugs:8952 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable ludo@gnu.org (Ludovic Court=C3=A8s) skribis: > As discussed on IRC, =E2=80=98get-bytevector-some=E2=80=99 returns only 1= byte from > unbuffered ports: Here=E2=80=99s a tentative fix. WDYT? Ludo=E2=80=99. --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable diff --git a/libguile/ports.c b/libguile/ports.c index 72bb73a01..002dd1433 100644 --- a/libguile/ports.c +++ b/libguile/ports.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1995-2001, 2003-2004, 2006-2017 +/* Copyright (C) 1995-2001, 2003-2004, 2006-2018 * Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or @@ -1543,7 +1543,9 @@ scm_peek_byte_or_eof (SCM port) return peek_byte_or_eof (port, &buf, &cur); } =20 -static size_t +/* Like read(2), read *up to* COUNT bytes from PORT into DST, starting + at OFFSET. Return 0 upon EOF. */ +size_t scm_i_read_bytes (SCM port, SCM dst, size_t start, size_t count) { size_t filled; diff --git a/libguile/ports.h b/libguile/ports.h index d131db5be..7aeacc8f9 100644 --- a/libguile/ports.h +++ b/libguile/ports.h @@ -4,7 +4,7 @@ #define SCM_PORTS_H =20 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, - * 2006, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundati= on, Inc. + * 2006, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2018 Free Software Fo= undation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -69,6 +69,7 @@ SCM_INTERNAL SCM scm_i_port_weak_set; #define SCM_OPOUTPORTP(x) (SCM_OPPORTP (x) && SCM_OUTPUT_PORT_P (x)) #define SCM_OPENP(x) (SCM_OPPORTP (x)) #define SCM_CLOSEDP(x) (!SCM_OPENP (x)) +#define SCM_UNBUFFEREDP(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_BU= F0)) #define SCM_CLR_PORT_OPEN_FLAG(p) \ SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN) #ifdef BUILDING_LIBGUILE @@ -185,6 +186,8 @@ SCM_API int scm_get_byte_or_eof (SCM port); SCM_API int scm_peek_byte_or_eof (SCM port); SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size); SCM_API size_t scm_c_read_bytes (SCM port, SCM dst, size_t start, size_t c= ount); +SCM_INTERNAL size_t scm_i_read_bytes (SCM port, SCM dst, size_t start, + size_t count); SCM_API scm_t_wchar scm_getc (SCM port); SCM_API SCM scm_read_char (SCM port); =20 diff --git a/libguile/r6rs-ports.c b/libguile/r6rs-ports.c index e944c7aab..a3a67f3ca 100644 --- a/libguile/r6rs-ports.c +++ b/libguile/r6rs-ports.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2009, 2010, 2011, 2013-2015 Free Software Foundation, Inc. +/* Copyright (C) 2009, 2010, 2011, 2013-2015, 2018 Free Software Foundatio= n, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -487,16 +487,33 @@ SCM_DEFINE (scm_get_bytevector_some, "get-bytevector-= some", 1, 0, 0, =20 SCM_VALIDATE_BINARY_INPUT_PORT (1, port); =20 - buf =3D scm_fill_input (port, 0, &cur, &avail); - if (avail =3D=3D 0) + if (SCM_UNBUFFEREDP (port)) { - scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F); - return SCM_EOF_VAL; + size_t read; + + bv =3D scm_c_make_bytevector (4096); + read =3D scm_i_read_bytes (port, bv, 0, SCM_BYTEVECTOR_LENGTH (bv)); + + if (read =3D=3D 0) + return SCM_EOF_VAL; + else if (read < SCM_BYTEVECTOR_LENGTH (bv)) + return scm_c_shrink_bytevector (bv, read); + else + return bv; } + else + { + buf =3D scm_fill_input (port, 0, &cur, &avail); + if (avail =3D=3D 0) + { + scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F); + return SCM_EOF_VAL; + } =20 - bv =3D scm_c_make_bytevector (avail); - scm_port_buffer_take (buf, (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv), - avail, cur, avail); + bv =3D scm_c_make_bytevector (avail); + scm_port_buffer_take (buf, (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (= bv), + avail, cur, avail); + } =20 return bv; } diff --git a/test-suite/tests/r6rs-ports.test b/test-suite/tests/r6rs-ports= .test index ba3131f2e..7450b7217 100644 --- a/test-suite/tests/r6rs-ports.test +++ b/test-suite/tests/r6rs-ports.test @@ -1,6 +1,6 @@ ;;;; r6rs-ports.test --- R6RS I/O port tests. -*- coding: utf-8; -*- ;;;; -;;;; Copyright (C) 2009-2012, 2013-2015 Free Software Foundation, Inc. +;;;; Copyright (C) 2009-2012, 2013-2015, 2018 Free Software Foundation, In= c. ;;;; Ludovic Court=C3=A8s ;;;; ;;;; This library is free software; you can redistribute it and/or @@ -183,6 +183,15 @@ (equal? (bytevector->u8-list bv) (map char->integer (string->list str)))))) =20 + (pass-if-equal "get-bytevector-some [unbuffered port]" + (string->utf8 "Hello, world!") + ;; 'get-bytevector-some' used to return a single byte, see + ;; . + (call-with-input-string "Hello, world!" + (lambda (port) + (setvbuf port _IONBF) + (get-bytevector-some port)))) + (pass-if "get-bytevector-all" (let* ((str "GNU Guile") (index 0) --=-=-=--