Changeset - c91f5f36fb2b
[Not reviewed]
stable
0 1 0
Thomas De Schampheleire - 5 years ago 2020-09-10 20:17:18
thomas.de_schampheleire@nokia.com
api: fix 'kallithea-api --save-config'

Commit eca0cb56a822 attempted to fix a type inconsistency, which caused
failure in the 'kallithea-api' tool when using '--save-config', but this
unfortunately did not fix the problem completely.

Following error still appeared:

Traceback (most recent call last):
File ".../bin/kallithea-api", line 33, in <module>
sys.exit(load_entry_point('Kallithea', 'console_scripts', 'kallithea-api')())
File ".../bin/kallithea_api.py", line 84, in main
'apihost': args.apihost})
File ".../bin/base.py", line 104, in __init__
self.make_config(config)
File ".../bin/base.py", line 132, in make_config
ext_json.dump(config, f, indent=4)
File "/usr/lib/python3.7/json/__init__.py", line 180, in dump
fp.write(chunk)
TypeError: a bytes-like object is required, not 'str'

The json module documentation says:
https://docs.python.org/3.7/library/json.html#basic-usage
"The json module always produces str objects, not bytes objects. Therefore,
fp.write() must support str input."

Therefore, instead of opening the file in binary mode and writing bytes,
open it in text mode and write strings.

For symmetry reasons, we make the same change when _loading_ the config
file, but this code worked regardless.
1 file changed with 3 insertions and 3 deletions:
0 comments (0 inline, 0 general)
kallithea/bin/base.py
Show inline comments
 
@@ -128,9 +128,9 @@ class RcConf(object):
 
        update = False
 
        if os.path.exists(self._conf_name):
 
            update = True
 
        with open(self._conf_name, 'wb') as f:
 
        with open(self._conf_name, 'w') as f:
 
            ext_json.dump(config, f, indent=4)
 
            f.write(b'\n')
 
            f.write('\n')
 

	
 
        if update:
 
            sys.stdout.write('Updated config in %s\n' % self._conf_name)
 
@@ -159,7 +159,7 @@ class RcConf(object):
 
        Loads config from file and returns loaded JSON object
 
        """
 
        try:
 
            with open(self._conf_name, 'rb') as conf:
 
            with open(self._conf_name, 'r') as conf:
 
                return ext_json.load(conf)
 
        except IOError as e:
 
            #sys.stderr.write(str(e) + '\n')
0 comments (0 inline, 0 general)