Permalink
Browse files

add separate type tag for floats (non-standard, maybe Zed won't like...)

  • Loading branch information...
1 parent 7951a3a commit 02e6b1a7e6b21731334efba8fcee3f63943b3f63 @rfk committed Apr 11, 2011
Showing with 24 additions and 25 deletions.
  1. +20 −22 tnetstring/__init__.py
  2. +1 −0 tnetstring/tests/test_format.py
  3. +3 −3 tnetstring/tns_core.c
@@ -115,7 +115,7 @@ def _rdumpq(q,size,value):
data = repr(value)
ldata = len(data)
span = str(ldata)
- write("#")
+ write("^")
write(data)
write(":")
write(span)
@@ -177,7 +177,7 @@ def _gdumps(value):
yield str(len(data))
yield ":"
yield data
- yield "#"
+ yield "^"
elif isinstance(value,(str,)):
yield str(len(value))
yield ":"
@@ -250,16 +250,15 @@ def load(file):
if type == ",":
return data
if type == "#":
- if "." in data or "e" in data or "E" in data:
- try:
- return float(data)
- except ValueError:
- raise ValueError("not a tnetstring: invalid float literal")
- else:
- try:
- return int(data)
- except ValueError:
- raise ValueError("not a tnetstring: invalid integer literal")
+ try:
+ return int(data)
+ except ValueError:
+ raise ValueError("not a tnetstring: invalid integer literal")
+ if type == "^":
+ try:
+ return float(data)
+ except ValueError:
+ raise ValueError("not a tnetstring: invalid float literal")
if type == "!":
if data == "true":
return True
@@ -311,16 +310,15 @@ def pop(string):
if type == ",":
return (data,remain)
if type == "#":
- if "." in data or "e" in data or "E" in data:
- try:
- return (float(data),remain)
- except ValueError:
- raise ValueError("not a tnetstring: invalid float literal")
- else:
- try:
- return (int(data),remain)
- except ValueError:
- raise ValueError("not a tnetstring: invalid integer literal")
+ try:
+ return (int(data),remain)
+ except ValueError:
+ raise ValueError("not a tnetstring: invalid integer literal")
+ if type == "^":
+ try:
+ return (float(data),remain)
+ except ValueError:
+ raise ValueError("not a tnetstring: invalid float literal")
if type == "!":
if data == "true":
return (True,remain)
@@ -21,6 +21,7 @@
'5:false!': False,
'10:\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,': "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
'24:5:12345#5:67890#5:xxxxx,]': [12345, 67890, 'xxxxx'],
+ '18:3:0.1^3:0.2^3:0.3^]': [0.1, 0.2, 0.3],
'243:238:233:228:223:218:213:208:203:198:193:188:183:178:173:168:163:158:153:148:143:138:133:128:123:118:113:108:103:99:95:91:87:83:79:75:71:67:63:59:55:51:47:43:39:35:31:27:23:19:15:11:hello-there,]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]': [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["hello-there"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
}
@@ -39,12 +39,12 @@ void* tns_parse(const tns_ops *ops, const char *data, size_t len, char **remain)
// Read the length of the value, and verify that it ends in a colon.
check(tns_strtosz(data, len, &vallen, &valstr) != -1,
- "Not a tnetstring: invalid length prefix1.");
+ "Not a tnetstring: invalid length prefix.");
check(*valstr == ':',
- "Not a tnetstring: invalid length prefix2. %s");
+ "Not a tnetstring: invalid length prefix.");
valstr++;
check((valstr+vallen) < (data+len),
- "Not a tnetstring: invalid length prefix3.");
+ "Not a tnetstring: invalid length prefix.");
// Grab the type tag from the end of the value.
type = valstr[vallen];

0 comments on commit 02e6b1a

Please sign in to comment.