Created
October 16, 2018 04:56
-
-
Save jctanner/1b8a1aaaf0881b898cae1af89785bb20 to your computer and use it in GitHub Desktop.
dumper reproducer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import datetime | |
| import io | |
| import json | |
| import os | |
| import shutil | |
| import six | |
| from ansibullbot._text_compat import to_bytes, to_text | |
| meta = { | |
| u'hello': u'world' | |
| } | |
| mfile = os.path.join( | |
| u'/tmp', | |
| u'meta.json' | |
| ) | |
| if os.path.exists(mfile): | |
| os.remove(mfile) | |
| meta[u'time'] = to_text(datetime.datetime.now().isoformat()) | |
| # Using in-memory buffer here to work around inability to dump | |
| # json to file directly | |
| tmp_inmemory_json = six.StringIO() | |
| json.dump(meta, tmp_inmemory_json, sort_keys=True, indent=2) | |
| with io.open(mfile, 'w', encoding='utf-8') as f: | |
| shutil.copyfileobj(tmp_inmemory_json, f, -1) | |
| tmp_inmemory_json.close() | |
| del tmp_inmemory_json | |
| assert os.path.isfile(mfile) | |
| assert json.load(open(mfile, 'r')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Even better:
I'll send a PR.