Henrik Bengtsson
2003-Mar-01  03:05 UTC
[Rd] RE: [R] File opening error after 1020 files opened
Maybe open.connection() should also check if the connection is already
open and given an error or at least a warning if it is. Suggested code
update:
open.connection <- function(con, open="r", blocking=TRUE, ...) {
  if (!inherits(con, "connection"))
    stop("argument is not a connection")
  if (isOpen(con))                                               # NEW
    stop("Trying to open a connection that is already open.")    # NEW
  invisible(.Internal(open(con, open, blocking)))
}
Henrik Bengtsson
> -----Original Message-----
> From: Roger Bivand [mailto:Roger.Bivand@nhh.no] 
> Sent: den 1 mars 2003 06:25
> To: ripley@stats.ox.ac.uk
> Cc: Dirk Eddelbuettel; jonathan_li@agilent.com; hb@maths.lth.se
> Subject: Re: [R] File opening error after 1020 files opened
> 
> 
> On Fri, 28 Feb 2003 ripley@stats.ox.ac.uk wrote:
> 
> > Let's take this off R-help.
> > 
> > I did a quick check that connections are behaving:
> > 
> > for(i in 1:2000) {
> > print(i)
> > zz <- file("foo", "rb")
> > foo <- readLines(zz)
> > close(zz)
> > }
> > 
> > for(i in 1:2000) {
> > print(i)
> > zz <- file(tempfile(), "wb")
> > cat("foobar\n", file=zz)
> > close(zz)
> > }
> > 
> > work for me, several times in a session in both Windows and 
> Linux. I 
> > do know that sometimes under Windows file closing fails, but not 
> > often.
> > 
> > The file-handle limit on RH7.2 seems to 8096, and I used at least 
> > 20000 opens.
> > 
> > However, read.pnm has
> > 
> >     con <- file(file, open = "rb")
> >     open(con, open = "rb")
> > ...
> >     close(con)
> 
> Yes, of course. Revision (pixmap_0.3-2.tar.gz) on incoming, 
> should be on 
> CRAN before long. If needed, I can attach it. Runs 10000 
> read.pnm() now 
> without difficulty. I can't recall why I once thought file() 
> just set a 
> handle and open() actually opened it, wrong assumptions can 
> survive for 
> years!
> 
> Thanks,
> 
> Roger
> 
> > 
> > which opens the connection *twice* and that's creating two file 
> > handles and only closing one.  See ?file.
> > 
> > Brian
[SNIP]
ripley@stats.ox.ac.uk
2003-Mar-01  08:35 UTC
[Rd] RE: [R] File opening error after 1020 files opened
It already does, at a lower level where it will always check.>From the NEWS file:o An attempt to open() an already open connection will be detected and ignored with a warning. This avoids improperly closing some types of connections if they are opened repeatedly. Please check such things first .... On Sat, 1 Mar 2003, Henrik Bengtsson wrote:> Maybe open.connection() should also check if the connection is already > open and given an error or at least a warning if it is. Suggested code > update: > > open.connection <- function(con, open="r", blocking=TRUE, ...) { > if (!inherits(con, "connection")) > stop("argument is not a connection") > if (isOpen(con)) # NEW > stop("Trying to open a connection that is already open.") # NEW > invisible(.Internal(open(con, open, blocking))) > } > > Henrik Bengtsson > > > > -----Original Message----- > > From: Roger Bivand [mailto:Roger.Bivand@nhh.no] > > Sent: den 1 mars 2003 06:25 > > To: ripley@stats.ox.ac.uk > > Cc: Dirk Eddelbuettel; jonathan_li@agilent.com; hb@maths.lth.se > > Subject: Re: [R] File opening error after 1020 files opened > > > > > > On Fri, 28 Feb 2003 ripley@stats.ox.ac.uk wrote: > > > > > Let's take this off R-help. > > > > > > I did a quick check that connections are behaving: > > > > > > for(i in 1:2000) { > > > print(i) > > > zz <- file("foo", "rb") > > > foo <- readLines(zz) > > > close(zz) > > > } > > > > > > for(i in 1:2000) { > > > print(i) > > > zz <- file(tempfile(), "wb") > > > cat("foobar\n", file=zz) > > > close(zz) > > > } > > > > > > work for me, several times in a session in both Windows and > > Linux. I > > > do know that sometimes under Windows file closing fails, but not > > > often. > > > > > > The file-handle limit on RH7.2 seems to 8096, and I used at least > > > 20000 opens. > > > > > > However, read.pnm has > > > > > > con <- file(file, open = "rb") > > > open(con, open = "rb") > > > ... > > > close(con) > > > > Yes, of course. Revision (pixmap_0.3-2.tar.gz) on incoming, > > should be on > > CRAN before long. If needed, I can attach it. Runs 10000 > > read.pnm() now > > without difficulty. I can't recall why I once thought file() > > just set a > > handle and open() actually opened it, wrong assumptions can > > survive for > > years! > > > > Thanks, > > > > Roger > > > > > > > > which opens the connection *twice* and that's creating two file > > > handles and only closing one. See ?file. > > > > > > Brian > > [SNIP] > >-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595