Skip to content

Commit 57def08

Browse files
committed
fix: export c_char, c_uchar, c_short, c_ushort too
1 parent 59f970e commit 57def08

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

docs/basics/types.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ libdestruct provides Python equivalents for common C primitive types. All types
66

77
| libdestruct | C equivalent | Size (bytes) | Signed |
88
|---|---|---|---|
9+
| `c_char` | `char` / `int8_t` | 1 | Yes |
10+
| `c_uchar` | `unsigned char` / `uint8_t` | 1 | No |
11+
| `c_short` | `short` / `int16_t` | 2 | Yes |
12+
| `c_ushort` | `unsigned short` / `uint16_t` | 2 | No |
913
| `c_int` | `int` / `int32_t` | 4 | Yes |
1014
| `c_uint` | `unsigned int` / `uint32_t` | 4 | No |
1115
| `c_long` | `long` / `int64_t` | 8 | Yes |
1216
| `c_ulong` | `unsigned long` / `uint64_t` | 8 | No |
13-
| `c_char` | `char` | 1 ||
1417
| `c_float` | `float` | 4 ||
1518
| `c_double` | `double` | 8 ||
1619
| `c_str` | `char[]` | variable ||

libdestruct/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
pass
1313

1414
from libdestruct.backing.resolver import Resolver
15-
from libdestruct.c import c_double, c_float, c_int, c_long, c_str, c_uint, c_ulong
15+
from libdestruct.c import c_char, c_double, c_float, c_int, c_long, c_short, c_str, c_uchar, c_uint, c_ulong, c_ushort
1616
from libdestruct.common.array import array, array_of
1717
from libdestruct.common.attributes import offset
1818
from libdestruct.common.bitfield import bitfield_of
@@ -27,13 +27,17 @@
2727
"array",
2828
"array_of",
2929
"bitfield_of",
30+
"c_char",
3031
"c_double",
3132
"c_float",
3233
"c_int",
3334
"c_long",
35+
"c_short",
3436
"c_str",
37+
"c_uchar",
3538
"c_uint",
3639
"c_ulong",
40+
"c_ushort",
3741
"enum",
3842
"enum_of",
3943
"inflate",

test/scripts/types_unit_test.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import struct as pystruct
1010
import unittest
1111

12-
from libdestruct import c_int, c_long, c_str, c_uint, c_float, c_double, inflater, struct, ptr, ptr_to_self, size_of, array_of
12+
from libdestruct import (
13+
c_char, c_double, c_float, c_int, c_long, c_short,
14+
c_str, c_uchar, c_uint, c_ulong, c_ushort,
15+
inflater, struct, ptr, ptr_to_self, size_of, array_of,
16+
)
1317
from libdestruct.backing.memory_resolver import MemoryResolver
1418

1519

@@ -26,6 +30,26 @@ def test_c_long_from_bytes(self):
2630
obj = c_long.from_bytes(data)
2731
self.assertEqual(obj.value, 123456789)
2832

33+
def test_c_char_from_bytes(self):
34+
data = (65).to_bytes(1, "little")
35+
obj = c_char.from_bytes(data)
36+
self.assertEqual(obj.value, 65)
37+
38+
def test_c_uchar_from_bytes(self):
39+
data = (200).to_bytes(1, "little")
40+
obj = c_uchar.from_bytes(data)
41+
self.assertEqual(obj.value, 200)
42+
43+
def test_c_short_from_bytes(self):
44+
data = (-1234).to_bytes(2, "little", signed=True)
45+
obj = c_short.from_bytes(data)
46+
self.assertEqual(obj.value, -1234)
47+
48+
def test_c_ushort_from_bytes(self):
49+
data = (60000).to_bytes(2, "little")
50+
obj = c_ushort.from_bytes(data)
51+
self.assertEqual(obj.value, 60000)
52+
2953
def test_c_uint_from_bytes(self):
3054
data = (0xDEADBEEF).to_bytes(4, "little")
3155
obj = c_uint.from_bytes(data)

0 commit comments

Comments
 (0)