Skip to content

Commit 07e262f

Browse files
committed
Revert "fix(shell): decode Windows pipe output with system codepage instead of UTF-8"
This reverts commit 3db6d1c.
1 parent 3db6d1c commit 07e262f

2 files changed

Lines changed: 5 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4545

4646
### Fixed
4747

48-
* shell.py: fix `UnicodeDecodeError` on Windows when command output contains non-ASCII characters (e.g. usernames with umlauts) by decoding with the system's ANSI codepage instead of assuming UTF-8
4948
* base.py: `cu()` now also escapes HTML characters in the error message, not just in the traceback
5049
* base.py: `cu()` now detects active exceptions via `sys.exc_info()` instead of string-matching the traceback
5150
* base.py: `get_state()` no longer calls `sys.exit()` on malformed range specs, returns UNKNOWN instead

shell.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
"""
1313

1414
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
15-
__version__ = '2026040801'
15+
__version__ = '2026032101'
1616

1717

18-
import locale
1918
import os
2019
import re
2120
import shlex
@@ -144,19 +143,9 @@ def shell_exec(cmd, env=None, shell=False, stdin='', cwd=None, timeout=None, lc_
144143
env = {**os.environ.copy(), **(env or {})}
145144
env['LC_ALL'] = lc_all
146145

147-
# On Windows, subprocess pipes deliver output in the system's ANSI codepage
148-
# (e.g. CP1252, CP437), NOT in UTF-8, even if "chcp 65001" was run beforehand.
149-
# This is because pipes are not console devices (see PEP 528 and Python docs
150-
# on sys.stdout). We therefore decode with the system's preferred encoding
151-
# and fall back to 'replace' to avoid UnicodeDecodeError on unexpected bytes.
152-
# See: https://github.com/Linuxfabrik/monitoring-plugins/issues/681
153146
if os.name == 'nt':
154-
_encoding = locale.getpreferredencoding(False)
155-
_errors = 'replace'
147+
cmd = f'chcp 65001 && {cmd}'
156148
shell = True
157-
else:
158-
_encoding = 'utf-8'
159-
_errors = 'surrogateescape'
160149

161150
if shell or stdin:
162151
try:
@@ -182,8 +171,8 @@ def shell_exec(cmd, env=None, shell=False, stdin='', cwd=None, timeout=None, lc_
182171
p.communicate()
183172
return False, f'Timeout after {timeout} seconds.'
184173
retc = p.returncode
185-
stdout = txt.to_text(stdout, encoding=_encoding, errors=_errors)
186-
stderr = txt.to_text(stderr, encoding=_encoding, errors=_errors)
174+
stdout = txt.to_text(stdout).replace('Active code page: 65001\r\n', '')
175+
stderr = txt.to_text(stderr)
187176
return True, (stdout, stderr, retc)
188177

189178
cmds = cmd.split('|')
@@ -210,8 +199,4 @@ def shell_exec(cmd, env=None, shell=False, stdin='', cwd=None, timeout=None, lc_
210199
p.communicate()
211200
return False, f'Timeout after {timeout} seconds.'
212201

213-
return True, (
214-
txt.to_text(stdout, encoding=_encoding, errors=_errors),
215-
txt.to_text(stderr, encoding=_encoding, errors=_errors),
216-
p.returncode,
217-
)
202+
return True, (txt.to_text(stdout), txt.to_text(stderr), p.returncode)

0 commit comments

Comments
 (0)