From 38cc5ead11d98b7f28ddf97adf7f0b09e969fc6e Mon Sep 17 00:00:00 2001 From: Bart Oldeman Date: Thu, 14 Mar 2024 13:32:14 -0400 Subject: [PATCH] Use locale.getpreferredencoding(False) instead of explicit 'utf-8' This fixes Python 3.6 with LC_ALL=C. For Python 3.7+ we always get 'utf-8': https://docs.python.org/3/library/os.html#utf8-mode --- easybuild/tools/run.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/easybuild/tools/run.py b/easybuild/tools/run.py index 2592e198f1..ce2081d58d 100644 --- a/easybuild/tools/run.py +++ b/easybuild/tools/run.py @@ -38,6 +38,7 @@ import contextlib import functools import inspect +import locale import os import re import signal @@ -350,8 +351,11 @@ def to_cmd_str(cmd): (stdout, stderr) = proc.communicate(input=stdin) # return output as a regular string rather than a byte sequence (and non-UTF-8 characters get stripped out) - output = stdout.decode('utf-8', 'ignore') - stderr = stderr.decode('utf-8', 'ignore') if split_stderr else None + # getpreferredencoding normally gives 'utf-8' but can be ASCII (ANSI_X3.4-1968) + # for Python 3.6 and older with LC_ALL=C + encoding = locale.getpreferredencoding(False) + output = stdout.decode(encoding, 'ignore') + stderr = stderr.decode(encoding, 'ignore') if split_stderr else None # store command output to temporary file(s) if output_file: