Skip to content

Commit 809da78

Browse files
committed
added struct dict example
1 parent 8bad885 commit 809da78

File tree

4 files changed

+85
-8
lines changed

4 files changed

+85
-8
lines changed

allocate.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
cffi = FFI()
44
cffi.cdef(
55
"""
6-
char* allocByteArrayZ(long long size);
7-
void printZ(char* messageZ);
6+
char* allocByteArrayZ(unsigned long long size);
7+
void printZ(char* message);
88
void freeByteArrayZ(char* array);
99
bool detectLeaks();
1010
"""
@@ -14,11 +14,11 @@
1414

1515
allocate = cffi.dlopen(os.path.abspath("allocate.dll"))
1616

17-
messageZ = allocate.allocByteArrayZ(5)
18-
messageZ[0:5] = b"hello"
17+
message = allocate.allocByteArrayZ(5)
18+
message[0:5] = b"hello"
1919

20-
allocate.printZ(messageZ)
20+
allocate.printZ(message)
2121

22-
allocate.freeByteArrayZ(messageZ)
22+
allocate.freeByteArrayZ(message)
2323

2424
print("leaked: {leaked}".format(leaked=allocate.detectLeaks()))

hello-world.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import os
1111

12-
helloWorld = cffi.dlopen(os.path.abspath("hello-world.dll"))
12+
hello_world = cffi.dlopen(os.path.abspath("hello-world.dll"))
1313

1414
# pass in null-terminated array
15-
helloWorld.printZ(b"Hello world\x00")
15+
hello_world.printZ(b"Hello world\x00")

struct-dict.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from cffi import FFI
2+
3+
cffi = FFI()
4+
cffi.cdef(
5+
"""
6+
typedef struct {
7+
bool boolean;
8+
char byte;
9+
char array[6];
10+
char* stringZ;
11+
} SomeStruct;
12+
13+
SomeStruct getStruct();
14+
void setStruct(SomeStruct some_struct);
15+
"""
16+
)
17+
18+
import os
19+
20+
struct_dict = cffi.dlopen(os.path.abspath("struct-dict.dll"))
21+
22+
23+
def toDict(struct):
24+
return dict(
25+
boolean=struct.boolean,
26+
byte=struct.byte[0],
27+
array=list(cffi.buffer(struct.array)[:]),
28+
stringZ=cffi.string(struct.stringZ).decode("utf-8"),
29+
)
30+
31+
32+
print(toDict(struct_dict.getStruct()))
33+
34+
35+
def toStruct(some_dict):
36+
some_struct = cffi.new("SomeStruct *")
37+
some_struct.boolean = some_dict["boolean"]
38+
some_struct.byte = some_dict["byte"].to_bytes(length=1, byteorder="big")
39+
some_struct.array = cffi.from_buffer("char[6]", bytearray(some_dict["array"]))
40+
some_struct.stringZ = cffi.from_buffer(
41+
"char[]", some_dict["stringZ"].encode("utf-8")
42+
)
43+
return some_struct[0]
44+
45+
46+
struct_dict.setStruct(
47+
toStruct(
48+
dict(
49+
boolean=True,
50+
byte=42,
51+
array=[5, 4, 3, 2, 1, 0],
52+
stringZ="hello from python",
53+
)
54+
)
55+
)

struct-dict.zig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const std = @import("std");
2+
3+
const SomeStruct = extern struct {
4+
boolean: bool,
5+
byte: u8,
6+
array: [6]u8,
7+
stringZ: [*:0]const u8,
8+
};
9+
10+
export fn getStruct() callconv(.C) SomeStruct {
11+
return .{
12+
.boolean = true,
13+
.byte = 42,
14+
.array = .{ 5, 4, 3, 2, 1, 0 },
15+
.stringZ = "hello from zig",
16+
};
17+
}
18+
19+
export fn setStruct(some_struct: SomeStruct) callconv(.C) void {
20+
std.debug.print("{}\n", .{some_struct});
21+
std.debug.print("{s}\n", .{some_struct.stringZ});
22+
}

0 commit comments

Comments
 (0)