Merge ~kissiel/checkbox-support:snap_utils into checkbox-support:master

Proposed by Maciej Kisielewski
Status: Merged
Approved by: Sylvain Pineau
Approved revision: 3b54b23b43bcffcad2a61a43f6e8483499f472fe
Merged at revision: 03f2d1d8b88b16dcdb6e9de628d1ae88e18254d2
Proposed branch: ~kissiel/checkbox-support:snap_utils
Merge into: checkbox-support:master
Diff against target: 13 lines (+1/-1)
1 file modified
checkbox_support/snap_utils/config.py (+1/-1)
Reviewer Review Type Date Requested Status
Sylvain Pineau (community) Approve
Review via email: mp+333875@code.launchpad.net

Description of the change

snap_utils: ensure values are stringified

if the value stored by the snapd is a string with only numbers in it, later on it gets printed in the json as number making python instantiate an Int from it.
Python's configparser expects only string, so this patch ensures the values are always treated as strings.

To test it, try setting any value to a number, and afterwards setting anything, like so:
$ sudo checkbox-snappy.configure FOO=123
$ sudo checkbox-snappy.configure FOO=bar
Traceback (most recent call last):
  File "/snap/checkbox-snappy/x1/bin/configure", line 30, in <module>
    main()
  File "/snap/checkbox-snappy/x1/bin/configure", line 27, in main
    update_configuration(vars_to_set)
  File "/snap/checkbox-snappy/x1/lib/python3.5/site-packages/checkbox_support/snap_utils/config.py", line 153, in update_configuration
    get_snapctl_config(list(get_configuration_set().keys())))
  File "/snap/checkbox-snappy/x1/lib/python3.5/site-packages/checkbox_support/snap_utils/config.py", line 115, in write_checkbox_conf
    config.set('environment', key, val)
  File "/snap/checkbox-snappy/x1/usr/lib/python3.5/configparser.py", line 1189, in set
    self._validate_value_types(option=option, value=value)
  File "/snap/checkbox-snappy/x1/usr/lib/python3.5/configparser.py", line 1174, in _validate_value_types
    raise TypeError("option values must be strings")
TypeError: option values must be strings

With this patch there should be no crash.

To post a comment you must log in.
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

Good catch, +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/checkbox_support/snap_utils/config.py b/checkbox_support/snap_utils/config.py
2index eb2b1aa..aaa7499 100644
3--- a/checkbox_support/snap_utils/config.py
4+++ b/checkbox_support/snap_utils/config.py
5@@ -109,7 +109,7 @@ def write_checkbox_conf(configuration):
6 config.optionxform = str
7 config.add_section('environment')
8 for key in sorted(configuration.keys()):
9- val = configuration[key]
10+ val = str(configuration[key])
11 # unmangle the key
12 key = key.replace('-', '_').upper()
13 config.set('environment', key, val)

Subscribers

People subscribed via source and target branches