Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make 'echo' raise IOErrors when appropriate #16367

Merged
merged 7 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@

- Added `math.isNaN`.

- `echo` and `debugEcho` will now raise `IOError` if writing to stdout fails. Previous behavior
silently ignored errors. See #16366. Use `-d:nimLegacyEchoNoRaise` for previous behavior.

## Language changes

- `nimscript` now handles `except Exception as e`.
Expand Down
24 changes: 16 additions & 8 deletions lib/system/io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ when defined(windows):
# But we cannot call printf directly as the string might contain \0.
# So we have to loop over all the sections separated by potential \0s.
var i = c_fprintf(f, "%s", s)
if i < 0:
if doRaise: raiseEIO("cannot write string to file")
return
while i < s.len:
if s[i] == '\0':
let w = c_fputc('\0', f)
Expand Down Expand Up @@ -780,6 +783,13 @@ when declared(stdout):
not defined(nintendoswitch) and not defined(freertos) and
hostOS != "any"

const echoDoRaise = not defined(nimLegacyEchoNoRaise) # see PR #16366

template checkErrMaybe(succeeded: bool): untyped =
if not succeeded:
when echoDoRaise:
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
checkErr(stdout)

proc echoBinSafe(args: openArray[string]) {.compilerproc.} =
when defined(androidNDK):
var s = ""
Expand All @@ -792,20 +802,18 @@ when declared(stdout):
proc flockfile(f: File) {.importc, nodecl.}
proc funlockfile(f: File) {.importc, nodecl.}
flockfile(stdout)
defer: funlockfile(stdout)
when defined(windows) and compileOption("threads"):
acquireSys echoLock
defer: releaseSys echoLock
for s in args:
when defined(windows):
writeWindows(stdout, s)
writeWindows(stdout, s, doRaise = echoDoRaise)
else:
discard c_fwrite(s.cstring, cast[csize_t](s.len), 1, stdout)
checkErrMaybe(c_fwrite(s.cstring, cast[csize_t](s.len), 1, stdout) == s.len)
const linefeed = "\n"
discard c_fwrite(linefeed.cstring, linefeed.len, 1, stdout)
discard c_fflush(stdout)
when stdOutLock:
funlockfile(stdout)
when defined(windows) and compileOption("threads"):
releaseSys echoLock
checkErrMaybe(c_fwrite(linefeed.cstring, linefeed.len, 1, stdout) == linefeed.len)
checkErrMaybe(c_fflush(stdout) == 0)


when defined(windows) and not defined(nimscript) and not defined(js):
Expand Down
11 changes: 11 additions & 0 deletions tests/exception/t16366.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
discard """
action: run
exitcode: 0
targets: "c cpp"
iffy marked this conversation as resolved.
Show resolved Hide resolved
disabled: openbsd
"""

echo "foo1"
close stdout
doAssertRaises(IOError):
echo "foo"