-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: show how to create and pass a V string to a V function, and…
… then get back a V string in Python
- Loading branch information
Showing
2 changed files
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
from ctypes import * | ||
import math, os | ||
|
||
## Load the V shared library: | ||
so_file="./test.so" | ||
if os.name=="nt": | ||
so_file="./test.dll" | ||
lib = CDLL(so_file) | ||
|
||
## Pass an integer to a V function, and receiving back an integer: | ||
print("lib.square(10) result is", lib.square(10)) | ||
assert lib.square(10) == 100, "Cannot validate V square()." | ||
|
||
## Pass a floating point number to a V function: | ||
lib.sqrt_of_sum_of_squares.restype = c_double | ||
assert lib.sqrt_of_sum_of_squares(c_double(1.1), c_double(2.2)) == math.sqrt(1.1*1.1 + 2.2*2.2), "Cannot validate V sqrt_of_sum_of_squares()." | ||
|
||
## Passing a V string to a V function, and receiving back a V string: | ||
class VString(Structure): | ||
_fields_ = [("str", c_char_p), ("len", c_int)] | ||
|
||
lib.process_v_string.argtypes = [VString] | ||
lib.process_v_string.restype = VString | ||
|
||
assert lib.process_v_string(VString(b'World', 5)).str == b'v World v' | ||
print('Hello', str(lib.process_v_string(VString(b'World', 5)).str, 'utf-8')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters