From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Fix std::print test case for Windows
Date: Fri, 15 Dec 2023 13:22:45 +0000 [thread overview]
Message-ID: <20231215132254.2139490-1-jwakely@redhat.com> (raw)
Tested x86_64-linux and x86_64-w64-mingw. Pushed to trunk.
-- >8 --
libstdc++-v3/ChangeLog:
* src/c++23/print.cc (__write_to_terminal) [_WIN32]: If handle
does not refer to the console then just write to it using normal
file I/O.
* testsuite/27_io/print/2.cc (as_printed_to_terminal): Print
error message on failure.
(test_utf16_transcoding): Adjust for as_printed_to_terminal
modifying its argument.
---
libstdc++-v3/src/c++23/print.cc | 13 ++++++++++++-
libstdc++-v3/testsuite/27_io/print/2.cc | 7 ++++++-
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/libstdc++-v3/src/c++23/print.cc b/libstdc++-v3/src/c++23/print.cc
index 2fe7a2e3565..d72ab856017 100644
--- a/libstdc++-v3/src/c++23/print.cc
+++ b/libstdc++-v3/src/c++23/print.cc
@@ -35,7 +35,8 @@
#ifdef _WIN32
# include <stdio.h> // _fileno
-# include <io.h> // _get_osfhandle
+# include <io.h> // _get_osfhandle, _open_osfhandle, _write
+# include <fcntl.h> // _O_APPEND
# include <windows.h> // GetLastError, WriteConsoleW
#elifdef _GLIBCXX_HAVE_UNISTD_H
# include <stdio.h> // fileno
@@ -324,6 +325,16 @@ namespace
if (!to_valid_utf16(str, wstr))
ec = std::make_error_code(errc::illegal_byte_sequence);
+ // This allows us to test this function with a normal file,
+ // see testsuite/27_io/print/2.cc
+ if (!check_for_console(term))
+ {
+ int fd = _open_osfhandle((intptr_t)term, _O_APPEND);
+ if (_write(fd, wstr.data(), wstr.size() * 2) == -1)
+ ec = {errno, generic_category()};
+ return ec;
+ }
+
unsigned long nchars = 0;
WriteConsoleW(term, wstr.data(), wstr.size(), &nchars, nullptr);
if (nchars != wstr.size())
diff --git a/libstdc++-v3/testsuite/27_io/print/2.cc b/libstdc++-v3/testsuite/27_io/print/2.cc
index e101201f109..8aa7888e7bd 100644
--- a/libstdc++-v3/testsuite/27_io/print/2.cc
+++ b/libstdc++-v3/testsuite/27_io/print/2.cc
@@ -39,7 +39,11 @@ as_printed_to_terminal(std::string& s)
#else
const auto ec = std::__write_to_terminal(strm, s);
#endif
- VERIFY( !ec || ec == std::make_error_code(std::errc::illegal_byte_sequence) );
+ if (ec && ec != std::make_error_code(std::errc::illegal_byte_sequence))
+ {
+ std::println("Failed to : {}", ec.message());
+ VERIFY(!ec);
+ }
std::fclose(strm);
std::ifstream in(f.path);
s.assign(std::istreambuf_iterator<char>(in), {});
@@ -114,6 +118,7 @@ test_utf16_transcoding()
VERIFY( as_printed_to_terminal(s) );
VERIFY( utf16_from_bytes(s) == s2 );
+ s = (const char*)u8"£ð¬ð§ â¬ðªðº";
s += " \xa3 10.99 \xee\xdd";
VERIFY( ! as_printed_to_terminal(s) );
std::u16string repl = u"\uFFFD";
--
2.43.0
reply other threads:[~2023-12-15 13:22 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231215132254.2139490-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).