This is an archive of the discontinued LLVM Phabricator instance.

[LNT] Python 3 support: fix convert to JSON
ClosedPublic

Authored by thopre on Sep 30 2019, 6:56 AM.

Details

Summary

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.

Diff Detail

Repository
rL LLVM

Event Timeline

thopre created this revision.Sep 30 2019, 6:56 AM

LGTM.

lnt/formats/JSONFormat.py
27 ↗(On Diff #222419)

According to https://docs.python.org/3/library/json.html, the default argument for json.dumps's ensure_ascii parameter is true. I guess this means both Python 2 and Python 3 will behave the same here with encode(), so we're okay to just use that.

This revision is now accepted and ready to land.Oct 3 2019, 8:00 AM
This revision was automatically updated to reflect the committed changes.
thopre marked an inline comment as done.
Herald added a project: Restricted Project. · View Herald TranscriptOct 3 2019, 8:17 AM