From 71c7a6b5836043e9476e98a61aa5dd907e0d4ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonah=20Br=C3=BCchert?= Date: Sat, 20 Apr 2024 00:21:53 +0200 Subject: Utils: Fix bytes / str confusion in Executor.run --- .gitignore | 3 +++ src/lib/Bcfg2/Utils.py | 16 +++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 6436e466b..99d5dbad8 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ testsuite/test.sqlite # sphinx build data man/.doctrees + +# 2to3 +*.bak diff --git a/src/lib/Bcfg2/Utils.py b/src/lib/Bcfg2/Utils.py index faabf9346..8315cb464 100644 --- a/src/lib/Bcfg2/Utils.py +++ b/src/lib/Bcfg2/Utils.py @@ -13,6 +13,8 @@ import subprocess import threading from Bcfg2.Compat import input, any # pylint: disable=W0622 +from typing import Optional + class ClassName(object): """ This very simple descriptor class exists only to get the name @@ -166,9 +168,6 @@ class ExecutorResult(object): raise TypeError("'%s' object does not support item assignment" % self.__class__.__name__) - def __bool__(self): - return self.__bool__() - def __bool__(self): return self.success @@ -200,7 +199,8 @@ class Executor(object): except OSError: pass - def run(self, command, inputdata=None, timeout=None, **kwargs): + def run(self, command: list[str], inputdata: Optional[str] = None, + timeout: Optional[int] = None, **kwargs): """ Run a command, given as a list, optionally giving it the specified input data. All additional keyword arguments are passed through to :class:`subprocess.Popen`. @@ -241,7 +241,9 @@ class Executor(object): if inputdata: for line in inputdata.splitlines(): self.logger.debug('> %s' % line) - (stdout, stderr) = proc.communicate(input=inputdata) + (stdout, stderr) = proc.communicate(input=inputdata.encode()) + else: + (stdout, stderr) = proc.communicate() # py3k fixes if not isinstance(stdout, str): @@ -313,12 +315,12 @@ def hostnames2ranges(hostnames): return ranges -def safe_input(msg): +def safe_input(msg: str) -> str: """ input() that flushes the input buffer before accepting input """ # flush input buffer while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0]) > 0: os.read(sys.stdin.fileno(), 4096) - return eval(input(msg)) + return input(msg) def safe_module_name(prefix, module): -- cgit v1.2.3-1-g7c22