# HG changeset patch # User Thomas De Schampheleire # Date 2020-09-10 20:17:18 # Node ID c91f5f36fb2b2761adb810fbddd41773b07fee55 # Parent eca0cb56a822690c58e6237e9787cd8cfa77fddc 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 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. diff --git a/kallithea/bin/base.py b/kallithea/bin/base.py --- a/kallithea/bin/base.py +++ b/kallithea/bin/base.py @@ -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')