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

Odd behavior when trying to capture C functions #3

Open
mtolly opened this issue Jun 16, 2017 · 1 comment
Open

Odd behavior when trying to capture C functions #3

mtolly opened this issue Jun 16, 2017 · 1 comment

Comments

@mtolly
Copy link

mtolly commented Jun 16, 2017

cfunc.c:

#include <stdio.h>

void cFunc() {
  printf("from C\n");
}

Main.hs:

module Main where

import System.IO.Silently

foreign import ccall unsafe "cFunc"
  cFunc :: IO ()

main :: IO ()
main = do
  capture_ (putStrLn "from haskell") >>= print
  capture_ cFunc >>= print

On my system (macOS 10.12.5, GHC 8.0.2, silently 1.2.5) this produces the output:

"from haskell\n"
""
from C

So I figured that was that, silently only works for Haskell code. Not sure why the C printf comes after the Haskell print but chalked it up to buffering weirdness. But then I modified main to look like this:

main :: IO ()
main = do
  capture_ (putStrLn "from haskell") >>= print
  cFunc
  capture_ cFunc >>= print

And that produces this output:

"from haskell\n"
from C
"from C\n"

So if I have C code print at least once to stdout, then thereafter it can be captured by silently...? I don't understand why at all. Any insight?

@andreasabel
Copy link
Collaborator

I can reproduce this as of 2022-08-20 (GHC 9.4.1).
I don't know the FFI enough to be able to comprehend this behavior.
But I experienced capture failures of stderr on Windows which I could fix using evaluate. However, in this case evaluate did not help (the cFunc returns (), nothing to evaluate here).

Is there literature how the FFI handles I/O? Didn't find anything...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants