Hello Python

This commit is contained in:
Julien Palard 2023-11-15 15:49:16 +01:00
parent e2260a534f
commit d63bebe601
Signed by: mdk
GPG Key ID: 0EFC1AC1006886F8
4 changed files with 28 additions and 0 deletions

3
build.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("cargo:rustc-link-search=./");
}

14
hello_python.c Normal file
View File

@ -0,0 +1,14 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int hello_python()
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()), 'from cpython')\n");
if (Py_FinalizeEx() < 0) {
printf("Failure during FinalizeEx");
return -1;
}
return 0;
}

View File

@ -5,6 +5,7 @@ mod errors;
mod exercises;
mod jour1;
mod jour2;
mod python;
mod rpg;
mod starwars;
mod tests;
@ -19,4 +20,5 @@ fn main() {
doc::main();
errors::main();
concurrency::main();
python::main();
}

9
src/python.rs Normal file
View File

@ -0,0 +1,9 @@
pub fn main() {
#[link(name = "hello_python")]
extern "C" {
fn hello_python();
}
unsafe {
hello_python();
}
}