From 5e85d4cb39063f9ae47d3b836557679324b42a63 Mon Sep 17 00:00:00 2001 From: Saptak Bhoumik Date: Sun, 9 Jan 2022 23:23:36 +0530 Subject: [PATCH] examples: show how to call a simple v module from python (#13105) --- cmd/tools/modules/testing/common.v | 3 ++- examples/call_v_from_python/README.md | 5 +++++ examples/call_v_from_python/test.py | 4 ++++ examples/call_v_from_python/test.v | 6 ++++++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 examples/call_v_from_python/README.md create mode 100644 examples/call_v_from_python/test.py create mode 100644 examples/call_v_from_python/test.v diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index a2e9cb533f..046466f491 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -176,6 +176,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession { } 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/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 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 @@ -456,7 +457,7 @@ pub fn prepare_test_session(zargs string, folder string, oskipped []string, main } } 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] if start.contains('module ') && !start.contains('module main') { skipped_f := f.replace(os.join_path_single(parent_dir, ''), '') diff --git a/examples/call_v_from_python/README.md b/examples/call_v_from_python/README.md new file mode 100644 index 0000000000..889d00b360 --- /dev/null +++ b/examples/call_v_from_python/README.md @@ -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`` diff --git a/examples/call_v_from_python/test.py b/examples/call_v_from_python/test.py new file mode 100644 index 0000000000..33b9ba7327 --- /dev/null +++ b/examples/call_v_from_python/test.py @@ -0,0 +1,4 @@ +from ctypes import * +so_file="./test.so" +my_functions = CDLL(so_file) +print(my_functions.square(10)) diff --git a/examples/call_v_from_python/test.v b/examples/call_v_from_python/test.v new file mode 100644 index 0000000000..9d5fbabc5d --- /dev/null +++ b/examples/call_v_from_python/test.v @@ -0,0 +1,6 @@ +module test + +[export: 'square'] +fn square(i int) int { + return i * i +}