Skip to content

Commit

Permalink
Issue 7: Correctly handles blank and comment only files (#8)
Browse files Browse the repository at this point in the history
* Issue 7: Correctly handles blanks and comment only files

* Update version numbers
  • Loading branch information
scuml authored and laktak committed Jun 9, 2017
1 parent 36b92ab commit b3e3a04
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 23 deletions.
2 changes: 2 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# History

- v2.0.6
- add support for blank and comment only files
- v2.0.5
- fix stringify for strings staring with a punctuator char
- v2.0.2
Expand Down
2 changes: 1 addition & 1 deletion hjson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"""
from __future__ import absolute_import
__version__ = "2.0.5"
__version__ = "2.0.6"
__all__ = [
'dump', 'dumps', 'load', 'loads',
'dumpJSON', 'dumpsJSON',
Expand Down
7 changes: 5 additions & 2 deletions hjson/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,13 @@ def raw_decode(self, s, idx=0, _PY3=PY3):
elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
idx += 3

start_index = idx
ch, idx = getNext(s, idx)

# If blank or comment only file, return dict
if start_index == 0 and ch == '':
return {}, 0

if ch == '{' or ch == '[':
return self.scan_once(s, idx)
else:
Expand All @@ -556,5 +561,3 @@ def raw_decode(self, s, idx=0, _PY3=PY3):
return self.scan_once(s, idx)
except:
raise e


1 change: 1 addition & 0 deletions hjson/tests/assets/blank_file_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Empty file.
1 change: 1 addition & 0 deletions hjson/tests/assets/comments_only_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions hjson/tests/assets/comments_only_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions hjson/tests/assets/comments_only_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// comments only file
1 change: 0 additions & 1 deletion hjson/tests/test_fail.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def test_array_decoder_issue46(self):

def test_truncated_input(self):
test_cases = [
('', 'Bad key name (eof)', 0),
('[', "End of input while parsing an array", 1),
# ('[42', "Expecting ',' delimiter", 3),
('[42,', 'Expecting value', 4),
Expand Down
37 changes: 19 additions & 18 deletions hjson/tests/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ class TestTool(unittest.TestCase):

expect = textwrap.dedent("""\
[
[
"blorpie"
],
[
"whoops"
],
[],
"d-shtaeou",
"d-nthiouh",
"i-vhbjkhnth",
{
"nifty": 87
},
{
"field": "yes",
"morefield": false
}
[
blorpie
]
[
whoops
]
[]
d-shtaeou
d-nthiouh
i-vhbjkhnth
{
nifty: 87
}
{
morefield: false
field: yes
}
]
""")

Expand Down Expand Up @@ -78,7 +78,8 @@ def test_infile_stdout(self):
self.runTool(args=[infile.name]),
self.expect.encode())

def test_infile_outfile(self):
def x_test_infile_outfile(self):
"""Not currently an option in tool"""
with tempfile.NamedTemporaryFile() as infile:
infile.write(self.data.encode())
infile.flush()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
DistutilsPlatformError

IS_PYPY = hasattr(sys, 'pypy_translation_info')
VERSION = "2.0.5"
VERSION = "2.0.6"
DESCRIPTION = "JSON for Humans. A configuration file format with relaxed syntax, fewer mistakes and more comments."

with open('README.rst', 'r') as f:
Expand Down

0 comments on commit b3e3a04

Please sign in to comment.