Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/bitstruct/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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++;
Expand Down Expand Up @@ -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);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/test_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<', '<u16', 'u8<u16', 'u8 <', 'p4<u4']:
with self.assertRaises(NotImplementedError):
pack(fmt, 1, 2)

with self.assertRaises(NotImplementedError):
unpack(fmt, b'\x00\x01\x02')

self.assertEqual(pack('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.

Expand Down