Skip to content

Commit b473181

Browse files
committed
Add a warning for windows users wrt stdout piping
Fix #905
1 parent 0d1d25b commit b473181

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

minet/cli/run.py

-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import multiprocessing
1616
import casanova
1717
import shlex
18-
import platform
1918
from casanova.exceptions import MissingColumnError
2019
from contextlib import ExitStack
2120

@@ -35,7 +34,6 @@
3534
from minet.cli.argparse import resolve_arg_dependencies, build_parser, get_subparser
3635
from minet.cli.exceptions import NotResumableError, InvalidArgumentsError, FatalError
3736

38-
WINDOWS = "windows" in platform.system().lower()
3937

4038
GLOBAL_SETUP_IS_DONE = False
4139

@@ -48,11 +46,6 @@ def global_setup() -> None:
4846

4947
GLOBAL_SETUP_IS_DONE = True
5048

51-
# Issue #497, utf-8 encoding for windows stdout
52-
if WINDOWS:
53-
sys.__stdout__.reconfigure(encoding="utf-8")
54-
sys.__stderr__.reconfigure(encoding="utf-8")
55-
5649
# Freezing multiprocessing support for pyinstaller etc.
5750
multiprocessing.freeze_support()
5851

minet/cli/utils.py

+21
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,28 @@ def emit(self, record):
139139
def acquire_cross_platform_stdout():
140140
# As per #254: stdout need to be wrapped so that windows get a correct csv
141141
# stream output
142+
# As per #905: powershell sometimes writes a UTF-16-LE BOM in the stdout
143+
# before one has any chance to reconfigure the stream as UTF-8
144+
# Reference: https://stackoverflow.com/questions/68487529/how-to-ensure-python-prints-utf-8-and-not-utf-16-le-when-piped-in-powershell
142145
if "windows" in platform.system().lower():
146+
console.vprint(
147+
[
148+
"You piped the result of your command into stdout.",
149+
'Stdout piping (">") is known to be unreliable on Windows shells (cmd.exe, PowerShell)',
150+
"and can sometimes result in corrupted data.",
151+
"",
152+
"Are you sure you know what you are doing?",
153+
"If you are not sure, please use the --output flag instead.",
154+
"",
155+
],
156+
style="warning",
157+
)
158+
159+
# As per #497: we reconfigure the stdout to be UTF-8 on
160+
# windows
161+
sys.__stdout__.reconfigure(encoding="utf-8")
162+
sys.__stderr__.reconfigure(encoding="utf-8")
163+
143164
return open(
144165
sys.__stdout__.fileno(),
145166
mode=sys.__stdout__.mode,

0 commit comments

Comments
 (0)