Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix issue #2, add check for invalid tokens

  • Loading branch information...
commit 5409355f7a81444466b7aa77c84c097f778146b8 1 parent 7a12d2c
@sunng87 authored
Showing with 22 additions and 8 deletions.
  1. +18 −6 clj.py
  2. +4 −2 tests/clj-test.py
View
24 clj.py
@@ -49,8 +49,9 @@ def number(v):
else:
return int(v)
-_STOP_CHARS = [" ", ",", "\n", "\r"]
+_STOP_CHARS = [" ", ",", "\n", "\r", "\t"]
_COLL_OPEN_CHARS = ["#", "[", "{"]
+_COLL_CLOSE_CHARS = ["]", "}"]
_EXTRA_NUM_CHARS = ["-", "+", ".", "e", "E"]
class CljDecoder(object):
@@ -124,10 +125,16 @@ def __read_token(self):
if t == "boolean":
if c == 't':
- e = fd.read(4)[-1]
+ chars = fd.read(4)
+ if chars[:3] != 'rue':
+ raise ValueError('Expect true, got t'+chars[:3])
+ e = chars[-1]
v = True
else:
- e = fd.read(5)[-1]
+ chars = fd.read(5)
+ if chars[:4] != 'alse':
+ raise ValueError('Expect false, got f'+chars[:4])
+ e = chars[-1]
v = False
elif t == "char":
@@ -140,7 +147,10 @@ def __read_token(self):
v = ''.join(buf[:-1])
elif t == "nil":
- e = fd.read(3)[-1]
+ chars = fd.read(3)
+ if chars[:2] != 'il':
+ raise ValueError('Expect nil, got n'+chars[:2])
+ e = chars[-1]
v = None
elif t == "number":
@@ -179,6 +189,8 @@ def __read_token(self):
#v = u''.join(buf).decode('unicode-escape')
v = ''.join(buf).decode('string-escape')
else:
+ if c not in _COLL_CLOSE_CHARS:
+ raise ValueError('Unexpected char: '+c)
r = False
e = c
@@ -215,7 +227,7 @@ def encode(self):
def get_type(self,t):
if t is None:
return ("None", False)
- elif isinstance(t, str) or isinstance(t, unicode):
+ elif isinstance(t, str):
return ("string", False)
elif isinstance(t, bool):
return ("boolean", False)
@@ -262,7 +274,7 @@ def __do_encode(self, d):
if t == "number":
fd.write(str(d))
elif t == "string":
- s = d.encode("unicode-escape").replace('"', '\\"')
+ s = d.encode("string-escape").replace('"', '\\"')
fd.write('"'+s+'"')
elif t == "boolean":
if d:
View
6 tests/clj-test.py
@@ -50,8 +50,10 @@ def test_all_data(self):
self.assertEqual(k, clj.dumps(v))
def test_misformed_data(self):
- data = "[1 2 3"
- self.assertRaises(ValueError, clj.loads, data)
+ data = ["[1 2 3", "til", "falSe", "nik", "@EE", "[@nil tee]"]
+ for d in data:
+ self.assertRaises(ValueError, clj.loads, d)
+
if __name__ == '__main__':
Please sign in to comment.
Something went wrong with that request. Please try again.