From 7aafe91f2de6f8babc3798f1d5d378c5f5c56e64 Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Fri, 17 Aug 2012 21:24:16 +0200 Subject: [PATCH] [molecule] restore Python 3.x compatibility --- molecule/compat.py | 6 ++++++ molecule/specs/skel.py | 8 ++++++-- tests/utils.py | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/molecule/compat.py b/molecule/compat.py index 094eb91..779ca29 100644 --- a/molecule/compat.py +++ b/molecule/compat.py @@ -170,3 +170,9 @@ def isnumber(obj): return isinstance(obj, int) else: return isinstance(obj, (int, long,)) + +def is_python3(): + """ + Return True, if the interpreter is Python 3.x + """ + return sys.hexversion >= 0x3000000 diff --git a/molecule/specs/skel.py b/molecule/specs/skel.py index a3e382c..9770374 100644 --- a/molecule/specs/skel.py +++ b/molecule/specs/skel.py @@ -19,7 +19,8 @@ import os import shlex -from molecule.compat import convert_to_unicode, convert_to_rawstring +from molecule.compat import convert_to_unicode, convert_to_rawstring, \ + is_python3 import molecule.utils class GenericSpecFunctions(object): @@ -56,8 +57,11 @@ class GenericSpecFunctions(object): return convert_to_unicode(x).strip().split() def ve_command_splitter(self, x): + x_str = x + if not is_python3(): + x_str = convert_to_rawstring(x) return [convert_to_unicode(y) for y in \ - shlex.split(convert_to_rawstring(x))] + shlex.split(x_str)] def ve_integer_converter(self, x): return int(x) diff --git a/tests/utils.py b/tests/utils.py index 08d1ab1..adbe91a 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,7 +6,7 @@ sys.path.insert(0,'..') import unittest import tempfile -from molecule.compat import get_stringtype +from molecule.compat import get_stringtype, convert_to_rawstring from molecule.utils import md5sum, copy_dir, get_random_number, \ remove_path_sandbox, remove_path, mkdtemp, empty_dir, \ exec_cmd_get_status_output, exec_cmd, is_exec_available, \ @@ -28,7 +28,7 @@ class UtilsTest(unittest.TestCase): def test_md5sum(self): tmp_fd, tmp_path = tempfile.mkstemp(dir=os.getcwd()) - os.write(tmp_fd, "hello") + os.write(tmp_fd, convert_to_rawstring("hello")) os.close(tmp_fd) result = md5sum(tmp_path) self.assertEqual(result, "5d41402abc4b2a76b9719d911017c592")