diff options
author | Nick Babcock <nbabcock19@hotmail.com> | 2015-07-10 17:25:50 -0400 |
---|---|---|
committer | Nick Babcock <nbabcock19@hotmail.com> | 2015-07-10 17:25:50 -0400 |
commit | a9e5e7256c9fb27126813c9430be1dab5f27f7ba (patch) | |
tree | 488eb7357fd5e781395161b7aa6be5aabea6a8ed /simplejson/tool.py | |
parent | 49724ee06f038d27ab3d4adbe4ed403692799aa1 (diff) | |
parent | 0bcdf20cc525c1343b796cb8f247ea5213c6557e (diff) | |
download | simplejson-a9e5e7256c9fb27126813c9430be1dab5f27f7ba.tar.gz |
Merge branch 'master' into iterable_as_array-gh1
Conflicts:
CHANGES.txt
conf.py
index.rst
setup.py
simplejson/__init__.py
simplejson/_speedups.c
simplejson/encoder.py
simplejson/tests/test_tuple.py
Diffstat (limited to 'simplejson/tool.py')
-rw-r--r-- | simplejson/tool.py | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/simplejson/tool.py b/simplejson/tool.py index 73370db..062e8e2 100644 --- a/simplejson/tool.py +++ b/simplejson/tool.py @@ -10,6 +10,7 @@ Usage:: Expecting property name: line 1 column 2 (char 2) """ +from __future__ import with_statement import sys import simplejson as json @@ -18,21 +19,23 @@ def main(): infile = sys.stdin outfile = sys.stdout elif len(sys.argv) == 2: - infile = open(sys.argv[1], 'rb') + infile = open(sys.argv[1], 'r') outfile = sys.stdout elif len(sys.argv) == 3: - infile = open(sys.argv[1], 'rb') - outfile = open(sys.argv[2], 'wb') + infile = open(sys.argv[1], 'r') + outfile = open(sys.argv[2], 'w') else: raise SystemExit(sys.argv[0] + " [infile [outfile]]") - try: - obj = json.load(infile, - object_pairs_hook=json.OrderedDict, - use_decimal=True) - except ValueError, e: - raise SystemExit(e) - json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True) - outfile.write('\n') + with infile: + try: + obj = json.load(infile, + object_pairs_hook=json.OrderedDict, + use_decimal=True) + except ValueError: + raise SystemExit(sys.exc_info()[1]) + with outfile: + json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True) + outfile.write('\n') if __name__ == '__main__': |