Skip to content

Commit 38e45ef

Browse files
committed
#323 colon in value cause yaml.load() throw error
Instead of updating/modifying the yaml parser, one of the workaround solutions is to put a pipe, '|' to indicate the whole value as a string. https://stackoverflow.com/questions/11301650/how-to-escape-indicator-characters-i-e-or-in-yaml/22483116#22483116 Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 60e0c31 commit 38e45ef

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/attributecode/model.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def serialize(self):
163163
value = value.splitlines(True)
164164
# multi-line
165165
if len(value) > 1:
166-
# This code is used to read the YAML's multi-line format in
166+
# This code is used to read the YAML's multi-line format saneyaml.loadin
167167
# ABOUT files
168168
# (Test: test_loads_dumps_is_idempotent)
169169
if value[0].strip() == u'|' or value[0].strip() == u'>':
@@ -175,7 +175,16 @@ def serialize(self):
175175
# insert 4 spaces for newline values
176176
value = u' '.join(value)
177177
else:
178-
value = u''.join(value)
178+
# See https://github.com/nexB/aboutcode-toolkit/issues/323
179+
# The yaml.load() will throw error if the parsed value
180+
# contains ': ' character. A work around is to put a pipe, '|'
181+
# to indicate the whole value as a string
182+
if value and ': ' in value[0]:
183+
value.insert(0, u'|\n')
184+
# insert 4 spaces for newline values
185+
value = u' '.join(value)
186+
else:
187+
value = u''.join(value)
179188

180189
serialized = u'%(name)s:' % locals()
181190
if value:

tests/test_model.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,21 @@ def test_write_output_json(self):
10681068
expected = get_test_loc('load/expected.json')
10691069
check_json(expected, result)
10701070

1071+
def test_colon_in_value(self):
1072+
test = u'''about_resource: .
1073+
name: AboutCode
1074+
version: v: 0.11.0
1075+
'''
1076+
expected = u'''about_resource: .
1077+
name: AboutCode
1078+
version: |
1079+
v: 0.11.0
1080+
'''
1081+
a = model.About()
1082+
base_dir = 'some_dir'
1083+
a.loads(test, base_dir)
1084+
dumped = a.dumps(with_absent=False, with_empty=False)
1085+
assert dumped == expected
10711086

10721087
class CollectorTest(unittest.TestCase):
10731088

0 commit comments

Comments
 (0)