Skip to content

Commit

Permalink
Added decoding for True, False
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Vasiliev committed Feb 24, 2010
1 parent a83a67b commit 305b299
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/erlport/erlterms.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from array import array
from zlib import decompress, compress


class IncompleteData(ValueError):
"""Need more data."""

Expand Down Expand Up @@ -159,7 +160,13 @@ def decode_term(string):
tail = tail[2:]
if len(tail) < length:
raise IncompleteData("incomplete data: %r" % string)
return Atom(tail[:length]), tail[length:]
name = tail[:length]
tail = tail[length:]
if name == "true":
return True, tail
elif name == "false":
return False, tail
return Atom(name), tail
elif tag == 104 or tag == 105:
# SMALL_TUPLE_EXT, LARGE_TUPLE_EXT
if tag == 104:
Expand Down
13 changes: 13 additions & 0 deletions src/erlport/tests/erlterms.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Erlang terms
============

Encoding
--------

Function encode() can encode Python types to Erlang external term format:

>>> from erlport.erlterms import encode, Atom, String, BitBinary
Expand Down Expand Up @@ -120,6 +123,10 @@ dict encoded as (Python) ordered proplists:
>>> encode(dict(a=1,b=2,c=3)) == encode([('a', 1), ('b', 2), ('c', 3)])
True


Decoding
--------

Function decode() can decode Erlang external term format to Python types:

>>> from erlport.erlterms import decode
Expand Down Expand Up @@ -239,3 +246,9 @@ Compressed terms:
>>> decode('\x83P\x00\x00\x00\x12x\x9c\xcbf\xe0\xaf@\x05\x00@\xc8\x07\x83')
([120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120], '')

Atoms true and false decoded as True and False:

>>> decode('\x83d\x00\x04true')
(True, '')
>>> decode('\x83d\x00\x05false')
(False, '')

0 comments on commit 305b299

Please sign in to comment.