diff --git a/src/bitstruct/c.c b/src/bitstruct/c.c index c300e01..8d6f8ad 100644 --- a/src/bitstruct/c.c +++ b/src/bitstruct/c.c @@ -844,6 +844,22 @@ static int count_number_of_fields(const char *format_p, return (count); } +static int skip_order(const char **format_p_pp) +{ + if (**format_p_pp == '<') { + PyErr_SetString(PyExc_NotImplementedError, + "Bit endianness and byte order are not supported."); + + return (-1); + } + + if (**format_p_pp == '>') { + (*format_p_pp)++; + } + + return (0); +} + const char *parse_field(const char *format_p, int *kind_p, int *number_of_bits_p) @@ -852,6 +868,10 @@ const char *parse_field(const char *format_p, format_p++; } + if (skip_order(&format_p) != 0) { + return (NULL); + } + *kind_p = *format_p; *number_of_bits_p = 0; format_p++; @@ -935,6 +955,16 @@ static struct info_t *parse_format(PyObject *format_obj_p) info_p->number_of_bits += number_of_bits; } + while (isspace(*format_p)) { + format_p++; + } + + if (skip_order(&format_p) != 0) { + PyMem_RawFree(info_p); + + return (NULL); + } + return (info_p); } diff --git a/tests/test_c.py b/tests/test_c.py index 2aa58ff..476f954 100644 --- a/tests/test_c.py +++ b/tests/test_c.py @@ -26,6 +26,25 @@ def is_cpython_3(): class CTest(unittest.TestCase): + def test_byte_order(self): + """Byte and bit order are not supported; '>' is the default. + + """ + + if not is_cpython_3(): + return + + for fmt in ['u16<', '', 1), b'\x00\x01') + self.assertEqual(pack('>u8>u8', 1, 2), b'\x01\x02') + self.assertEqual(unpack('u8>u8>', b'\x01\x02'), (1, 2)) + def test_pack(self): """Pack values.