Skip to content

Commit 47b7277

Browse files
authored
Fix UTF-8 issue with format (#380)
1 parent e3c8c61 commit 47b7277

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

alibuild_helpers/utilities.py

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def validateSpec(spec):
2323
raise SpecError("Missing package field in header.")
2424

2525
def format(s, **kwds):
26+
if type(s) == bytes:
27+
s = s.decode()
2628
return s % kwds
2729

2830
def doDetectArch(hasOsRelease, osReleaseLines, platformTuple, platformSystem, platformProcessor):

tests/test_utilities.py

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import platform
33
from alibuild_helpers.utilities import doDetectArch
44
from alibuild_helpers.utilities import Hasher
5+
from alibuild_helpers.utilities import format
56

67
UBUNTU_1510_OS_RELEASE = """
78
NAME="Ubuntu"
@@ -129,6 +130,11 @@ def test_UTF8_Hasher(self):
129130
self.assertEqual(h3.hexdigest(), "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
130131
self.assertNotEqual(h1.hexdigest(), h2.hexdigest())
131132

133+
def test_format(self):
134+
self.assertEqual(format("%(foo)s", foo="foo"), "foo")
135+
self.assertEqual(format(b"%(foo)s", foo="foo"), "foo")
136+
self.assertRaises(KeyError, format, "%(foo)s", bar="foo")
137+
132138
if __name__ == '__main__':
133139
unittest.main()
134140

0 commit comments

Comments
 (0)