Permalink
Browse files

fix TextualAdapter to build 0 and negative numbers

  • Loading branch information...
1 parent dad8eca commit 9f4a10bfa50dc690d2ac8bb181974503038433ca Artem Zankovich committed Mar 19, 2012
Showing with 10 additions and 4 deletions.
  1. +4 −0 construct/tests/unit.py
  2. +6 −4 construct/text/common.py
View
4 construct/tests/unit.py 100644 → 100755
@@ -220,8 +220,12 @@
[Identifier("identifier").build, "2c8", None, ValidationError],
[TextualIntAdapter(Field("textintadapter", 3)).parse, "234", 234, None],
+ [TextualIntAdapter(Field("textintadapter", 3)).parse, "-34", -34, None],
+ [TextualIntAdapter(Field("textintadapter", 1)).parse, "0", 0, None],
[TextualIntAdapter(Field("textintadapter", 3), radix = 16).parse, "234", 0x234, None],
[TextualIntAdapter(Field("textintadapter", 3)).build, 234, "234", None],
+ [TextualIntAdapter(Field("textintadapter", 3)).build, -34, "-34", None],
+ [TextualIntAdapter(Field("textintadapter", 1)).build, 0, "0", None],
[TextualIntAdapter(Field("textintadapter", 3), radix = 16).build, 0x234, "234", None],
# [TextualIntAdapter(Field("textintadapter", 3)).build, 23, "023", None],
View
10 construct/text/common.py 100644 → 100755
@@ -202,16 +202,18 @@ def __init__(self, subcon, radix = 10, digits = "0123456789abcdef"):
self.digits = digits
def _encode(self, obj, context):
chars = []
+ n = obj
if obj < 0:
- chars.append("-")
- n = -obj
- else:
- n = obj
+ n = -n
+ elif obj == 0:
+ chars.append("0")
r = self.radix
digs = self.digits
while n > 0:
n, d = divmod(n, r)
chars.append(digs[d])
+ if obj < 0:
+ chars.append("-")
# obj2 = "".join(reversed(chars))
# filler = digs[0] * (self._sizeof(context) - len(obj2))
# return filler + obj2

2 comments on commit 9f4a10b

Hey -- can you resubmit the pull request, this time to https://github.com/construct/construct/ ?
Thanks

sorry, it's invalid for construct 2.1.0 -- i'm dropping the text package (it was experimental and it's highly inefficient)

Please sign in to comment.