Skip to content

Commit 8109fb4

Browse files
committed
Support the text argument to our subprocess helpers in order to ease
conversion of the default utf8-encoded bytes output to native strings. Follow-up to issue #288.
1 parent 6511bfd commit 8109fb4

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

mig/shared/safeeval.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# safeeval - Safe evaluation of expressions and commands
7-
# Copyright (C) 2003-2023 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH
88
#
99
# This file is part of MiG.
1010
#
@@ -290,7 +290,7 @@ def math_expr_eval(expr):
290290

291291

292292
def subprocess_check_output(command, stdin=None, stdout=None, stderr=None,
293-
env=None, cwd=None,
293+
env=None, text=None, cwd=None,
294294
only_sanitized_variables=False):
295295
"""Safe execution of command with output returned as byte string.
296296
The optional only_sanitized_variables option is used to override the
@@ -299,25 +299,31 @@ def subprocess_check_output(command, stdin=None, stdout=None, stderr=None,
299299
command comes from user-provided variables or file names that may contain
300300
control characters.
301301
"""
302-
return subprocess.check_output(command, stdin=stdin, env=env, cwd=cwd,
302+
# NOTE: python3.7 added text arg previously known as universal_newlines
303+
# TODO: rename universal_newlines to text once we drop python3.6 support
304+
return subprocess.check_output(command, stdin=stdin, env=env,
305+
universal_newlines=text, cwd=cwd,
303306
shell=only_sanitized_variables)
304307

305308

306309
def subprocess_call(command, stdin=None, stdout=None, stderr=None, env=None,
307-
cwd=None, only_sanitized_variables=False):
310+
text=None, cwd=None, only_sanitized_variables=False):
308311
"""Safe execution of command.
309312
The optional only_sanitized_variables option is used to override the
310313
default execution without shell interpretation of control characters.
311314
Please be really careful when using it especially if any parts of your
312315
command comes from user-provided variables or file names that may contain
313316
control characters.
314317
"""
318+
# NOTE: python3.7 added text arg previously known as universal_newlines
319+
# TODO: rename universal_newlines to text once we drop python3.6 support
315320
return subprocess.call(command, stdin=stdin, stdout=stdout, stderr=stderr,
316-
env=env, cwd=cwd, shell=only_sanitized_variables)
321+
env=env, universal_newlines=text, cwd=cwd,
322+
shell=only_sanitized_variables)
317323

318324

319325
def subprocess_popen(command, stdin=None, stdout=None, stderr=None, env=None,
320-
cwd=None, only_sanitized_variables=False):
326+
text=None, cwd=None, only_sanitized_variables=False):
321327
"""Safe execution of command with full process control.
322328
The optional only_sanitized_variables option is used to override the
323329
default execution without shell interpretation of control characters.
@@ -326,8 +332,11 @@ def subprocess_popen(command, stdin=None, stdout=None, stderr=None, env=None,
326332
control characters.
327333
Returns a subprocess Popen object with wait method, returncode and so on.
328334
"""
335+
# NOTE: python3.7 added text arg previously known as universal_newlines
336+
# TODO: rename universal_newlines to text once we drop python3.6 support
329337
return subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr,
330-
env=env, cwd=cwd, shell=only_sanitized_variables)
338+
env=env, universal_newlines=text, cwd=cwd,
339+
shell=only_sanitized_variables)
331340

332341

333342
def subprocess_list2cmdline(command):

0 commit comments

Comments
 (0)