Bug 17196 - Output text connections are not writable in all cases
Summary: Output text connections are not writable in all cases
Alias: None
Product: R
Classification: Unclassified
Component: I/O (show other bugs)
Version: R-devel (trunk)
Hardware: Other Other
: P5 minor
Assignee: R-core
Depends on:
Reported: 2016-12-20 16:05 UTC by Jim Hester
Modified: 2016-12-23 08:24 UTC (History)
1 user (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Jim Hester 2016-12-20 16:05:29 UTC
zz <- textConnection("foo", "w")
    writeChar("abc", zz)
    #> Error in writeChar("abc", zz) : 'write' not enabled for this connection

The same thing happens if one tries to use `R_WriteConnection()` on a output textConnection. The underlying cause seems to be that textConnections never change the `write` function in the connection object from `null_write` (See src/main/connections.c#L500).
Comment 1 Jim Hester 2016-12-20 16:12:22 UTC
The same behavior occurs with `stdout()` with the same underlying cause (write function not defined).

    writeChar("test", stdout())
    #> Error in writeChar("test", stdout()) :
    #>  'write' not enabled for this connection
Comment 2 Martin Maechler 2016-12-23 08:24:24 UTC
In  ?writeChar,
the 2nd paragraph in 'Details'  says :

     These functions are intended to be used with binary-mode
     connections.  If ‘con’ is a character string, the functions call
     ‘file’ to obtain a binary-mode file connection which is opened for
     the duration of the function call.

so it seems this is all according to documentation.
Maybe we should mention "binary" or "binary-mode" already in the description of the 'con' argument ?

OTOH, I think I also had been surprised in the past by the fact that only binary connections were supported... given that there is also  writeBin().
Then, if you look more closely, the reference explains how the two are different and why both functionalities are needed.

Still, I agree that it would seem natural to *expect* that writeChar() would work for a text connection (and then also for other "text like" connections).

I let others chime in, not being a connection expert.
I'd tend to set this to "WISHLIST" status and be happy to accept patches that enable writing to more connections than currently.