I’m trying to call Python’s C APIs in OCaml efficiently.
external pyinit : unit -> unit = "Py_Initialize" external errprint : unit -> unit = "PyErr_Print" external compile : string -> string -> int -> pyo = "Py_CompileString" let main() = let _ = pyinit() in let _ = compile "print(1)" "a.py" 257 in errprint() sh> dynjit dune exec myproj File "a.py", line 1 print(1) ^ SyntaxError: invalid syntax
This is really a strange error, everything is fine but it doesn’t work.
I’ve tried invoking the C function
Py_CompileString in Python with python’s ctypes, it works.
Also I’ve tried this in another programming language with its dynamic link interfaces, things also work.
However the equivalent code in OCaml causes this very strange error. The c function arguments must be handled correctly otherwise I cannot see “print(1)” or “a.py” in the error report.
If I incorrectly put the whitespaces in the head of the code,
err_print can also report what the syntax error is.
So strange… Any suggestions?