Repository: SCons/scons

Issue:
UnicodeDecodeError
Why i get error if type help command?
Python 3.8.10
Scons 4.2.0
![Безымянный](https://user-images.githubusercontent.com/64489444/131680807-3b5b91f2-32f5-428b-ae24-39e820ce0c88.png)

UnicodeDecodeError
Why i get error if type help command?
Python 3.8.10
Scons 4.2.0
![Безымянный](https://user-images.githubusercontent.com/64489444/131680807-3b5b91f2-32f5-428b-ae24-39e820ce0c88.png)



Discussion:
Can you paste the output of `gcc --version` ?
![Безымянный](https://user-images.githubusercontent.com/64489444/131846980-f363dd50-10aa-4539-aada-3236939094d9.png)
I use TDM on windows. In linux all works. I install SCons from pip and github rep (setup.py) but all of it not works
> ![Безымянный](https://user-images.githubusercontent.com/64489444/131846980-f363dd50-10aa-4539-aada-3236939094d9.png)
> I use TDM on windows. In linux all works. I install SCons from pip and github rep (setup.py) but all of it not works

Please copy and paste the text and not a screenshot.
The text will help us debug the issue. 
Maybe redirect the output to a file and attach that.

[gcc.txt](https://github.com/SCons/scons/files/7101547/gcc.txt)
This archive contained scons script, simple C++ program and scons output txt: [test.zip](https://github.com/SCons/scons/files/7101665/test.zip)


What locale/language is your computer set up with?
If you're up for trying a patch to your SCons installation, I'd be curious the effect of this (only changes one line):
```python
diff --git a/SCons/Tool/gcc.py b/SCons/Tool/gcc.py
index 6470002f1..7e3859987 100644
--- a/SCons/Tool/gcc.py
+++ b/SCons/Tool/gcc.py
@@ -88,7 +88,8 @@ def detect_version(env, cc):
         line = SCons.Util.to_str(pipe.stdout.readline())
         # Non-GNU compiler's output (like AIX xlc's) may exceed the stdout buffer:
         # So continue with reading to let the child process actually terminate.
-        while SCons.Util.to_str(pipe.stdout.readline()):
+        # We don't need to know the rest of the data, so don't bother decoding.
+        while pipe.stdout.readline():
             pass
 
 
```

As to "why do I get this if I ask for help":  if you use `scons --help`, scons will try to print any locally defined help as well, which means it has to read the sconscript files, which means the compiler and other tool detection gets done.  If you do `scons -H` (capital letter) it will just print the help without any processing and would not trip across this issue.
> 
> 
> What locale/language is your computer set up with?

I use Russian lang: chcp 1251
Also i set 65001 code page but error didn't fix 
Did @mwichmann 's sugguested fix above work?

If not try changing SCons/Util.py's `to_str()` to the following

```
def to_str(s) -> str:
    if s is None:
        return 'None'
    if is_String(s):
        return s
    return str(s, 'utf-8', errors='ignore')
```
Thank u, this works!
Glad that worked!

Reopening. This change is not yet in the code.
We'll close this when we fix it and it will be noted in the release notes.
I'll push the change presently, have it sitting here.  
Can you paste the output of `gcc --version` ?
![Безымянный](https://user-images.githubusercontent.com/64489444/131846980-f363dd50-10aa-4539-aada-3236939094d9.png)
I use TDM on windows. In linux all works. I install SCons from pip and github rep (setup.py) but all of it not works
> ![Безымянный](https://user-images.githubusercontent.com/64489444/131846980-f363dd50-10aa-4539-aada-3236939094d9.png)
> I use TDM on windows. In linux all works. I install SCons from pip and github rep (setup.py) but all of it not works

Please copy and paste the text and not a screenshot.
The text will help us debug the issue. 
Maybe redirect the output to a file and attach that.

[gcc.txt](https://github.com/SCons/scons/files/7101547/gcc.txt)
This archive contained scons script, simple C++ program and scons output txt: [test.zip](https://github.com/SCons/scons/files/7101665/test.zip)


What locale/language is your computer set up with?
If you're up for trying a patch to your SCons installation, I'd be curious the effect of this (only changes one line):
```python
diff --git a/SCons/Tool/gcc.py b/SCons/Tool/gcc.py
index 6470002f1..7e3859987 100644
--- a/SCons/Tool/gcc.py
+++ b/SCons/Tool/gcc.py
@@ -88,7 +88,8 @@ def detect_version(env, cc):
         line = SCons.Util.to_str(pipe.stdout.readline())
         # Non-GNU compiler's output (like AIX xlc's) may exceed the stdout buffer:
         # So continue with reading to let the child process actually terminate.
-        while SCons.Util.to_str(pipe.stdout.readline()):
+        # We don't need to know the rest of the data, so don't bother decoding.
+        while pipe.stdout.readline():
             pass
 
 
```

As to "why do I get this if I ask for help":  if you use `scons --help`, scons will try to print any locally defined help as well, which means it has to read the sconscript files, which means the compiler and other tool detection gets done.  If you do `scons -H` (capital letter) it will just print the help without any processing and would not trip across this issue.
> 
> 
> What locale/language is your computer set up with?

I use Russian lang: chcp 1251
Also i set 65001 code page but error didn't fix 
Did @mwichmann 's sugguested fix above work?

If not try changing SCons/Util.py's `to_str()` to the following

```
def to_str(s) -> str:
    if s is None:
        return 'None'
    if is_String(s):
        return s
    return str(s, 'utf-8', errors='ignore')
```
Thank u, this works!
Glad that worked!

Reopening. This change is not yet in the code.
We'll close this when we fix it and it will be noted in the release notes.
I'll push the change presently, have it sitting here.  

Resolving patch:
diff --git a/SCons/Tool/gcc.py b/SCons/Tool/gcc.py
--- a/SCons/Tool/gcc.py
+++ b/SCons/Tool/gcc.py
@@ -88,7 +88,8 @@ def detect_version(env, cc):
         line = SCons.Util.to_str(pipe.stdout.readline())
         # Non-GNU compiler's output (like AIX xlc's) may exceed the stdout buffer:
         # So continue with reading to let the child process actually terminate.
-        while SCons.Util.to_str(pipe.stdout.readline()):
+        # We don't need to know the rest of the data, so don't bother decoding.
+        while pipe.stdout.readline():
             pass
 
 


Test patch:
diff --git a/test/CC/gcc-non-utf8-fixture/.exclude_tests b/test/CC/gcc-non-utf8-fixture/.exclude_tests
new file mode 100644
--- /dev/null
+++ b/test/CC/gcc-non-utf8-fixture/.exclude_tests
@@ -0,0 +1,2 @@
+data
+gcc-non-utf8.py
diff --git a/test/CC/gcc-non-utf8-fixture/data b/test/CC/gcc-non-utf8-fixture/data
new file mode 100644
--- /dev/null
+++ b/test/CC/gcc-non-utf8-fixture/data
@@ -0,0 +1,10 @@
+gcc (tdm-1) 9.2.0
+Copyright (C) 2019 Free Software Foundation, Inc.
+��� �������� ���������������� ����������� �����������. ������� �����������
+��������� � �������� �������.
+
+  ��� �������� �����-���� �������, ������� 
+������������ �������� � ������������ ��� �����-���� �����.
+
+
+
diff --git a/test/CC/gcc-non-utf8-fixture/gcc-non-utf8.py b/test/CC/gcc-non-utf8-fixture/gcc-non-utf8.py
new file mode 100644
--- /dev/null
+++ b/test/CC/gcc-non-utf8-fixture/gcc-non-utf8.py
@@ -0,0 +1,8 @@
+import sys
+
+print(f"In {sys.argv[0]}")
+
+if __name__ == '__main__':
+    if '--version' in sys.argv:
+        with open("data", "rb") as f:
+            sys.stdout.buffer.write(f.read())
diff --git a/test/CC/gcc-version.py b/test/CC/gcc-version.py
new file mode 100644
--- /dev/null
+++ b/test/CC/gcc-version.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+#
+# MIT License
+#
+# Copyright The SCons Foundation
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+"""
+Test that if gcc returns non-UTF8 text, the version check doesn't fall over.
+"""
+
+import sys
+
+import TestSCons
+
+_python_ = TestSCons._python_
+_exe = TestSCons._exe
+
+test = TestSCons.TestSCons()
+
+test.dir_fixture('gcc-non-utf8-fixture')
+
+test.write(
+    'SConstruct',
+    """
+import sys
+
+DefaultEnvironment(tools=[])
+env = Environment(tools=[], CC="%(_python_)s gcc-non-utf8.py")
+try:
+    env.Tool('gcc')
+except UnicodeDecodeError:
+    print("Failed decoding gcc version message.", file=sys.stderr)
+    Exit(1)
+"""
+    % locals(),
+)
+
+test.run(arguments='.')
+
+test.pass_test()
+
+# Local Variables:
+# tab-width:4
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=4 shiftwidth=4: