From 47b7277ef94d6196aebe7cfc3026d3283cabd4a8 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse Date: Tue, 25 Oct 2016 14:38:22 +0200 Subject: [PATCH] Fix UTF-8 issue with format (#380) --- alibuild_helpers/utilities.py | 2 ++ tests/test_utilities.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/alibuild_helpers/utilities.py b/alibuild_helpers/utilities.py index dbd47446..21e5c518 100644 --- a/alibuild_helpers/utilities.py +++ b/alibuild_helpers/utilities.py @@ -23,6 +23,8 @@ def validateSpec(spec): raise SpecError("Missing package field in header.") def format(s, **kwds): + if type(s) == bytes: + s = s.decode() return s % kwds def doDetectArch(hasOsRelease, osReleaseLines, platformTuple, platformSystem, platformProcessor): diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 66c53665..21a3cb18 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -2,6 +2,7 @@ import platform from alibuild_helpers.utilities import doDetectArch from alibuild_helpers.utilities import Hasher +from alibuild_helpers.utilities import format UBUNTU_1510_OS_RELEASE = """ NAME="Ubuntu" @@ -129,6 +130,11 @@ def test_UTF8_Hasher(self): self.assertEqual(h3.hexdigest(), "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33") self.assertNotEqual(h1.hexdigest(), h2.hexdigest()) + def test_format(self): + self.assertEqual(format("%(foo)s", foo="foo"), "foo") + self.assertEqual(format(b"%(foo)s", foo="foo"), "foo") + self.assertRaises(KeyError, format, "%(foo)s", bar="foo") + if __name__ == '__main__': unittest.main()