Skip to content

Conversation

@anutosh491
Copy link
Collaborator

Description

Fixes #381

Type of change

Please tick all options which are relevant.

  • Bug fix
  • New feature
  • Added/removed dependencies
  • Required documentation updates

@anutosh491
Copy link
Collaborator Author

The Problem here

C++ error output via std::cerr appeared correctly because xeus-cpp redirects the iostream buffers to its xoutput_buffer. However, C-level error output via fprintf(stderr, ...) did not appear. Only printf(...) and fprintf(stdout, ...) worked, because StreamRedirectRAII forwarded the captured stdout stream but silently dropped the captured stderr stream during normal execution.

Hence causing this

image

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.81%. Comparing base (190caf6) to head (028cec0).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #402      +/-   ##
==========================================
+ Coverage   81.77%   81.81%   +0.04%     
==========================================
  Files          20       20              
  Lines         845      847       +2     
  Branches       87       89       +2     
==========================================
+ Hits          691      693       +2     
  Misses        154      154              
Files with missing lines Coverage Δ
src/xinterpreter.cpp 90.68% <100.00%> (+0.11%) ⬆️
Files with missing lines Coverage Δ
src/xinterpreter.cpp 90.68% <100.00%> (+0.11%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

namespace xcpp
{
struct StreamRedirectRAII {
std::string &out;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: member 'out' of type 'std::string &' (aka 'basic_string &') is a reference [cppcoreguidelines-avoid-const-or-ref-data-members]

      std::string &out;
                   ^

: out(o), err(e)
{
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::kStdErr" is directly included [misc-include-cleaner]

        Cpp::BeginStdStreamCapture(Cpp::kStdErr);
                                        ^

@anutosh491
Copy link
Collaborator Author

Fix introduced

  • StreamRedirectRAII now:

    • accepts two references (out, err)
    • begins capture in the correct order (stdout → stderr)
    • ends capture in LIFO order (stderr → stdout)
    • forwards neither inside the destructor
  • execute_request_impl now:

    • emits the captured out → std::cout and captured err → std::cerr once
    • flushes them immediately.

This fixes what we want

image

And C++ based streams worked already

image

@mcbarton
Copy link
Collaborator

mcbarton commented Nov 6, 2025

I have checked this PR, and it does solve the problem. @anutosh491 make the clang tidy and clang format workflows happy, and I will approve and take in.

@anutosh491
Copy link
Collaborator Author

I have checked this PR, and it does solve the problem. @anutosh491 make the clang tidy and clang format workflows happy, and I will approve and take in.

Thank you, I shall do that soon (afk for sometime)

We technically need to track that we should test such fixes in favour of the C Kernel whenever we end up with a framework for that. I haven't gone through all the discussion but as far as I remember, cppinterop needs to provide us with a utility to differentiate between C++ and C snippets isn't it ?

@mcbarton
Copy link
Collaborator

mcbarton commented Nov 6, 2025

I have checked this PR, and it does solve the problem. @anutosh491 make the clang tidy and clang format workflows happy, and I will approve and take in.

Thank you, I shall do that soon (afk for sometime)

We technically need to track that we should test such fixes in favour of the C Kernel whenever we end up with a framework for that. I haven't gone through all the discussion but as far as I remember, cppinterop needs to provide us with a utility to differentiate between C++ and C snippets isn't it ?

Yes, that is exactly what needs to be done to enable c kernel tests. I have already tested the other small change which would be needed in test_xcpp_kernel.py to run the c kernel tests. If you mention all the things you think the c kernel tests should be testing in the issue already open about adding c tests #394 then I can add them once the cppinterop utility is available.

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

Successfully merging this pull request may close these issues.

fprintf(stderr, …) doesn't work in C wasm kernel

3 participants