diff options
author | Peter Johnson <peter@tortall.net> | 2007-01-20 19:37:02 +0000 |
---|---|---|
committer | Peter Johnson <peter@tortall.net> | 2007-01-20 19:37:02 +0000 |
commit | 9dd8df8fb683038deaaba9959872c9ba6ae66fb2 (patch) | |
tree | dd5a94c9c4a5c954b70f484b7a80635fc8f9b329 /tools | |
parent | d2fba0ea6eb349db3e3d5f0bced3ff72aa18abc7 (diff) | |
download | yasm-9dd8df8fb683038deaaba9959872c9ba6ae66fb2.tar.gz |
intnum.c: Better internal error checking on intnum creation input strings.
svn path=/trunk/yasm/; revision=1739
Diffstat (limited to 'tools')
-rw-r--r-- | tools/python-yasm/intnum.pxi | 23 | ||||
-rw-r--r-- | tools/python-yasm/tests/test_intnum.py | 12 |
2 files changed, 27 insertions, 8 deletions
diff --git a/tools/python-yasm/intnum.pxi b/tools/python-yasm/intnum.pxi index 42db35eb..7f77af95 100644 --- a/tools/python-yasm/intnum.pxi +++ b/tools/python-yasm/intnum.pxi @@ -109,18 +109,25 @@ cdef class IntNum: self.intn = <yasm_intnum *>__get_voidp(value, IntNum) return - val = None if isinstance(value, str): - val = long(value, base) + if base == 2: + self.intn = yasm_intnum_create_bin(value) + elif base == 8: + self.intn = yasm_intnum_create_oct(value) + elif base == 10 or base is None: + self.intn = yasm_intnum_create_dec(value) + elif base == 16: + self.intn = yasm_intnum_create_hex(value) + elif base == "nasm": + self.intn = yasm_intnum_create_charconst_nasm(value) + else: + raise ValueError("base must be 2, 8, 10, 16, or \"nasm\"") elif isinstance(value, (int, long)): - val = long(value) - - if val is None: + _PyLong_AsByteArray(long(value), buf, 16, 1, 1) + self.intn = yasm_intnum_create_sized(buf, 1, 16, 0) + else: raise ValueError - _PyLong_AsByteArray(val, buf, 16, 1, 1) - self.intn = yasm_intnum_create_sized(buf, 1, 16, 0) - def __dealloc__(self): if self.intn != NULL: yasm_intnum_destroy(self.intn) diff --git a/tools/python-yasm/tests/test_intnum.py b/tools/python-yasm/tests/test_intnum.py index 1c157f6a..65e09efc 100644 --- a/tools/python-yasm/tests/test_intnum.py +++ b/tools/python-yasm/tests/test_intnum.py @@ -16,11 +16,23 @@ class TIntNum(TestCase): def test_to_from(self): for i in self.legal_values: self.assertEquals(i, int(IntNum(i))) + self.assertEquals(i, long(IntNum(i))) def test_overflow(self): for i in self.overflow_values: self.assertRaises(OverflowError, IntNum, i) + str_values = [ + "0", "00000", "1234", "87654321", "010101010", "FADCBEEF" + ] + base_values = [2, 8, 10, 12, 16, None, "nasm", "foo"] + + def test_from_str(self): + pass + + def test_from_str_base(self): + pass + def test_exceptions(self): self.assertRaises(ZeroDivisionError, IntNum(1).__div__, 0) |