examples: show how to call a simple v module from python (#13105)

pull/13113/head
Saptak Bhoumik 2022-01-09 23:23:36 +05:30 committed by GitHub
parent 535317eba3
commit 5e85d4cb39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 1 deletions

View File

@ -176,6 +176,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
} }
if testing.github_job != 'ubuntu-tcc' { if testing.github_job != 'ubuntu-tcc' {
skip_files << 'examples/c_interop_wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases skip_files << 'examples/c_interop_wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases
skip_files << 'examples/call_v_from_python/test.v' // the example only makes sense to be compiled, when python is installed
// the ttf_test.v is not interactive, but needs X11 headers to be installed, which is done only on ubuntu-tcc for now // the ttf_test.v is not interactive, but needs X11 headers to be installed, which is done only on ubuntu-tcc for now
skip_files << 'vlib/x/ttf/ttf_test.v' skip_files << 'vlib/x/ttf/ttf_test.v'
skip_files << 'vlib/vweb/vweb_app_test.v' // imports the `sqlite` module, which in turn includes sqlite3.h skip_files << 'vlib/vweb/vweb_app_test.v' // imports the `sqlite` module, which in turn includes sqlite3.h
@ -456,7 +457,7 @@ pub fn prepare_test_session(zargs string, folder string, oskipped []string, main
} }
} }
c := os.read_file(f) or { panic(err) } c := os.read_file(f) or { panic(err) }
maxc := if c.len > 300 { 300 } else { c.len } maxc := if c.len > 500 { 500 } else { c.len }
start := c[0..maxc] start := c[0..maxc]
if start.contains('module ') && !start.contains('module main') { if start.contains('module ') && !start.contains('module main') {
skipped_f := f.replace(os.join_path_single(parent_dir, ''), '') skipped_f := f.replace(os.join_path_single(parent_dir, ''), '')

View File

@ -0,0 +1,5 @@
A simple example to show how to call a function written in v from python
Step 1: Compile the v code to a shared library using ``v -shared -prod test.v``
Step 2: Run the python file using ``python3 test.py``

View File

@ -0,0 +1,4 @@
from ctypes import *
so_file="./test.so"
my_functions = CDLL(so_file)
print(my_functions.square(10))

View File

@ -0,0 +1,6 @@
module test
[export: 'square']
fn square(i int) int {
return i * i
}