[LNT] r373615 - [LNT] Python 3 support: fix convert to JSON
Thomas Preud'homme via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 3 08:18:06 PDT 2019
Author: thopre
Date: Thu Oct 3 08:18:06 2019
New Revision: 373615
URL: http://llvm.org/viewvc/llvm-project?rev=373615&view=rev
Log:
[LNT] Python 3 support: fix convert to JSON
Plistlib and json modules have conflicting requirements for their dump
methods: Plistlib expects its file handle to be a writable binary file
object while json expects it to be a writable text file object.
This commit solves this issue by dumping the JSON into a temporary
string which is encoded to UTF-8 (JSON format RFC 7159 requires UTF-8,
UTF-16 or UTF-32 encoding and UTF-8 is the recommended default) before
being output. This allows to keep delegating file opening to Click and
avoid a refactoring to abstract the opening mode between supported
formats.
Reviewers: cmatthews, hubert.reinterpretcast, kristof.beyls
Reviewed By: hubert.reinterpretcast
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D68223
Modified:
lnt/trunk/lnt/formats/JSONFormat.py
Modified: lnt/trunk/lnt/formats/JSONFormat.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/formats/JSONFormat.py?rev=373615&r1=373614&r2=373615&view=diff
==============================================================================
--- lnt/trunk/lnt/formats/JSONFormat.py (original)
+++ lnt/trunk/lnt/formats/JSONFormat.py Thu Oct 3 08:18:06 2019
@@ -19,9 +19,17 @@ def _load_format(path_or_file):
return json.load(path_or_file)
+def _dump_format(obj, fp):
+ # The json module produces str objects but fp is opened in binary mode
+ # (since Plistlib only dump to binary mode files) so we first dump into
+ # a string a convert to UTF-8 before outputing.
+ json_str = json.dumps(obj)
+ fp.write(json_str.encode())
+
+
format = {
'name': 'json',
'predicate': _matches_format,
'read': _load_format,
- 'write': json.dump,
+ 'write': _dump_format,
}
More information about the llvm-commits
mailing list