From 408d405704d8826dd164d828670fa37bf6ca8027 Mon Sep 17 00:00:00 2001 From: Oz Tiram Date: Tue, 9 Apr 2019 22:51:54 +0200 Subject: [PATCH] Handle StringIO for Python2 properly --- kubernetes/utils/create_from_yaml.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/kubernetes/utils/create_from_yaml.py b/kubernetes/utils/create_from_yaml.py index cf157da58..9cfec1ab6 100644 --- a/kubernetes/utils/create_from_yaml.py +++ b/kubernetes/utils/create_from_yaml.py @@ -11,8 +11,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import io import re +import sys + +if sys.version_info.major > 2: + from StringIO import StringIO +else: + from io import StringIO from os import path @@ -56,7 +61,11 @@ def create_from_yaml( """ if path.exists(yaml_file): with open(path.abspath(yaml_file)) as f: - yaml_file = io.StringIO(f.read()) + content = f.read() + try: + yaml_file = StringIO(content) + except TypeError: + yaml_file = StringIO(content.decode('utf-8')) yml_document_all = yaml.safe_load_all(yaml_file) # Load all documents from a single YAML file