From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.bugs Subject: bug#30066: 'get-bytevector-some' returns only 1 byte from unbuffered ports Date: Thu, 11 Jan 2018 14:55:07 -0500 Message-ID: <87fu7cf9wk.fsf@netris.org> References: <87zi5lrc3x.fsf@gnu.org> <87tvvtr9ge.fsf@gnu.org> <87fu7dptdn.fsf@igalia.com> <87o9m08nx2.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1515701221 24161 195.159.176.226 (11 Jan 2018 20:07:01 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 11 Jan 2018 20:07:01 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) Cc: Andy Wingo , 30066@debbugs.gnu.org To: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Thu Jan 11 21:06:57 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 1eZj7i-0005M0-1e for guile-bugs@m.gmane.org; Thu, 11 Jan 2018 21:06:50 +0100 Original-Received: from localhost ([::1]:38635 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZj9f-0003aD-Mf for guile-bugs@m.gmane.org; Thu, 11 Jan 2018 15:08:51 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34942) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZizG-0001oY-QM for bug-guile@gnu.org; Thu, 11 Jan 2018 14:58:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZizC-0005yA-Sq for bug-guile@gnu.org; Thu, 11 Jan 2018 14:58:06 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:44585) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eZizC-0005xt-Ow for bug-guile@gnu.org; Thu, 11 Jan 2018 14:58:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1eZizC-0006vM-9F for bug-guile@gnu.org; Thu, 11 Jan 2018 14:58:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Mark H Weaver Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Thu, 11 Jan 2018 19:58: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.151570064526572 (code B ref 30066); Thu, 11 Jan 2018 19:58:02 +0000 Original-Received: (at 30066) by debbugs.gnu.org; 11 Jan 2018 19:57:25 +0000 Original-Received: from localhost ([127.0.0.1]:52482 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZiya-0006uV-12 for submit@debbugs.gnu.org; Thu, 11 Jan 2018 14:57:25 -0500 Original-Received: from world.peace.net ([50.252.239.5]:59574) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZiyY-0006uI-HN for 30066@debbugs.gnu.org; Thu, 11 Jan 2018 14:57:22 -0500 Original-Received: from [98.216.255.118] (helo=yeeloong) by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1eZiyS-0000hc-40; Thu, 11 Jan 2018 14:57:16 -0500 In-Reply-To: <87o9m08nx2.fsf@gnu.org> ("Ludovic \=\?utf-8\?Q\?Court\=C3\=A8s\=22'\?\= \=\?utf-8\?Q\?s\?\= message of "Thu, 11 Jan 2018 15:34:17 +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:8959 Archived-At: Hi Ludovic, ludo@gnu.org (Ludovic Court=C3=A8s) writes: > Andy Wingo skribis: > >> I suggest taking the buffered bytes from the read buffer, if any. Then >> if the port is unbuffered, make a bytevector and call scm_i_read_bytes; >> otherwise do the scm_fill_input path that's there already. >> >> One more thing, if the port goes EOF, you need to >> scm_port_buffer_set_has_eof_p. > > I think the attached patch addresses these issues. WDYT? [...] > diff --git a/libguile/r6rs-ports.c b/libguile/r6rs-ports.c > index e944c7aab..a3d638ca0 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, I= nc. > +/* Copyright (C) 2009, 2010, 2011, 2013-2015, 2018 Free Software Foundat= ion, Inc. > * > * This library is free software; you can redistribute it and/or > * modify it under the terms of the GNU Lesser General Public License > @@ -481,9 +481,9 @@ SCM_DEFINE (scm_get_bytevector_some, "get-bytevector-= some", 1, 0, 0, > "position to point just past these bytes.") > #define FUNC_NAME s_scm_get_bytevector_some > { > - SCM buf; > + SCM buf, bv; > size_t cur, avail; > - SCM bv; > + const size_t max_buffer_size =3D 4096; >=20=20 > SCM_VALIDATE_BINARY_INPUT_PORT (1, port); >=20=20 > @@ -494,9 +494,31 @@ SCM_DEFINE (scm_get_bytevector_some, "get-bytevector= -some", 1, 0, 0, > return SCM_EOF_VAL; > } >=20=20 > - bv =3D scm_c_make_bytevector (avail); > - scm_port_buffer_take (buf, (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv= ), > - avail, cur, avail); > + if (SCM_UNBUFFEREDP (port) && (avail < max_buffer_size)) > + { > + /* PORT is unbuffered. Read as much as possible from PORT. */ > + size_t read; > + > + bv =3D scm_c_make_bytevector (max_buffer_size); > + scm_port_buffer_take (buf, (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS= (bv), > + avail, cur, avail); > + > + read =3D scm_i_read_bytes (port, bv, avail, > + SCM_BYTEVECTOR_LENGTH (bv) - avail); Here's the R6RS specification for 'get-bytevector-some': "Reads from BINARY-INPUT-PORT, blocking as necessary, until bytes are available from BINARY-INPUT-PORT or until an end of file is reached. If bytes become available, 'get-bytevector-some' returns a freshly allocated bytevector containing the initial available bytes (at least one), and it updates BINARY-INPUT-PORT to point just past these bytes. If no input bytes are seen before an end of file is reached, the end-of-file object is returned." By my reading of this, we should block only if necessary to ensure that we return at least one byte (or EOF). In other words, if we can return at least one byte (or EOF), then we must not block, which means that we must not initiate another 'read'. Out of curiosity, is there a reason why you're using an unbuffered port in your use case? Mark