Bugfix: write_json_to_file: use json.dump with files 51/27651/3
authorRoss Brattain <ross.b.brattain@intel.com>
Fri, 27 Jan 2017 00:00:38 +0000 (16:00 -0800)
committerRoss Brattain <ross.b.brattain@intel.com>
Wed, 8 Feb 2017 06:25:39 +0000 (06:25 +0000)
In python3 file objects automatically handle encoding to utf-8 and expect
string, not bytes, so use regular json.dump() with the file object

We should only use dump_as_bytes to replace json.dumps(), not
to replace json.dump()

This fixes Python3 issue:

Traceback (most recent call last):
  File "yardstick/main.py", line 52, in <module>
    main()
  File "yardstick/main.py", line 49, in main
    YardstickCLI().main(sys.argv[1:])
  File "yardstick/yardstick/cmd/cli.py", line 167, in main
    self._dispath_func_notask()
  File "yardstick/yardstick/cmd/cli.py", line 145, in _dispath_func_notask
    func(CONF.category)
  File "yardstick/yardstick/cmd/commands/task.py", line 48, in do_start
    self._init_result_file()
  File "yardstick/yardstick/cmd/commands/task.py", line 57, in _init_result_file
    write_json_to_file(consts.DEFAULT_OUTPUT_FILE, data)
  File "yardstick/yardstick/common/utils.py", line 152, in write_json_to_file
    write_file(path, jsonutils.dump_as_bytes(data), mode)
  File "yardstick/yardstick/common/utils.py", line 157, in write_file
    f.write(data)
TypeError: write() argument must be str, not bytes

Change-Id: I573419be25d8fa1f015e1507730ba66c05f86686
Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
yardstick/common/utils.py

index 473bbf5..174ac0a 100644 (file)
@@ -149,7 +149,8 @@ def get_neutron_client():
 
 
 def write_json_to_file(path, data, mode='w'):
-    write_file(path, jsonutils.dump_as_bytes(data), mode)
+    with open(path, mode) as f:
+        jsonutils.dump(data, f)
 
 
 def write_file(path, data, mode='w'):