Re: [R] dput(..., file = stderr())

2024-03-01 Thread Ivan Krylov via R-help
В Fri, 1 Mar 2024 10:54:08 -0500
Benjamin Tyner  пишет:

>  > dput(letters, file = stderr())  
> c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
> "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
> "z")
> Warning message:
> In dput(letters, file = stderr()) : wrote too few characters

Sounds like a bug.

This is where the warning is produced:

int res = Rconn_printf(con, "%s\n", CHAR(STRING_ELT(tval, i)));
if(!havewarned &&
   res < strlen(CHAR(STRING_ELT(tval, i))) + 1) {
warning(_("wrote too few characters"));
havewarned = TRUE;
}

At this point, `res` is 0, and CHAR(STRING_ELT(tval, i)) is what you'd
expect:

(gdb) x /s (void*)(STRING_ELT(tval, i))+48
0x561bf0a8: "c(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\",
\"h\", \"i\", \"j\", \"k\", \"l\", "

`res` is 0 because that's what (con->vfprintf)(...) returns:

2813static int stderr_vfprintf(Rconnection con, const char *format, va_list 
ap)
2814{
2815REvprintf(format, ap);
2816return 0;
2817}

Why doesn't it warn for stdout()? Doesn't stdout_vfprintf(...) behave
the same? That's because stdout() (connection #1) is special for dput(),
and instead of going through the connection system, it goes straight for
Rprintf():

426 else { // ifile == 1 : "Stdout"
427 for (int i = 0; i < LENGTH(tval); i++)
428 Rprintf("%s\n", CHAR(STRING_ELT(tval, i)));
429 }

Not sure what the right fix is. There may be code depending on
(con->vfprintf)(...) returning 0.

-- 
Best regards,
Ivan

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] dput(..., file = stderr())

2024-03-01 Thread Benjamin Tyner
Curious to know if this warning is expected behavior, and if so, what is 
the recommended way instead:


> dput(letters, file = stderr())
   c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
   "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
   "z")
   Warning message:
   In dput(letters, file = stderr()) : wrote too few characters

(I ask because it doesn't happen when using file = stdout())

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.