Skip to content

Commit a8cb798

Browse files
committed
ImageConfigParser: Don't sort merged values
The order of configuration values may be significant, so the parser should not sort them. To do that, use a single counter for add and del variants and emit the values that have positive counts without sorting. This relies on the Python dict stable order, which has been the case since Python 3.6.
1 parent ff6c562 commit a8cb798

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

lib/eib.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -242,25 +242,23 @@ def _merge_option(self, section_pattern, option):
242242
opt)
243243
yield (section, opt)
244244
else:
245-
add_vals = Counter()
245+
values = Counter()
246246
for opt in add_opts:
247247
logger.debug('Adding %s %s values from %s', section,
248248
option, opt)
249-
add_vals.update(sect[opt].split())
249+
values.update(sect[opt].split())
250250
yield (section, opt)
251251

252-
del_vals = Counter()
253252
for opt in del_opts:
254253
logger.debug('Removing %s %s values from %s', section,
255254
option, opt)
256-
del_vals.update(sect[opt].split())
255+
values.subtract(sect[opt].split())
257256
yield (section, opt)
258257

259-
# Set the option to the difference of the counters.
258+
# Set the option to the keys that have positive values.
260259
# Merge the values together with newlines like they were
261260
# in the original configuration.
262-
vals = add_vals - del_vals
263-
sect[option] = '\n'.join(sorted(vals.keys()))
261+
sect[option] = '\n'.join(k for k, v in values.items() if v > 0)
264262

265263
def copy(self):
266264
"""Create a new instance from this one"""

tests/eib/test_image_config_parser.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ def test_merged_option(config):
119119

120120
assert set(sect) == {'opt'}
121121

122-
# The values will be sorted and newline separated
123-
assert sect['opt'] == 'baz\nfoo'
122+
# The values will be newline separated in the order they appeared.
123+
assert sect['opt'] == 'foo\nbaz'
124124

125125
# Now that the merged option exists, it will override any further
126126
# add/del.
@@ -129,7 +129,7 @@ def test_merged_option(config):
129129
config.merge()
130130

131131
assert set(sect) == {'opt'}
132-
assert sect['opt'] == 'baz\nfoo'
132+
assert sect['opt'] == 'foo\nbaz'
133133

134134

135135
def test_merged_option_interpolation(config):
@@ -277,7 +277,7 @@ def test_merged_files(tmp_path, config):
277277
config.merge()
278278

279279
assert set(config['sect']) == {'opt'}
280-
assert config['sect']['opt'] == 'baz\nfoo'
280+
assert config['sect']['opt'] == 'foo\nbaz'
281281
assert set(config['sect-a']) == {'opt'}
282282
assert config['sect-a']['opt'] == ''
283283
assert set(config['sect-b']) == {'opt'}

0 commit comments

Comments
 (0)