-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TEST: Add cpython_interop initial example for LLVM
- Loading branch information
1 parent
5693585
commit f23a8d8
Showing
3 changed files
with
84 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
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from lpython import ccall, Pointer, i32, empty_c_void_p, CPtr, pointer | ||
|
||
|
||
@ccall(header="Python.h") | ||
def Py_Initialize(): | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def Py_DecodeLocale(s: str, p: CPtr) -> CPtr: | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def PySys_SetArgv(n: i32, args: Pointer[CPtr]): | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def Py_FinalizeEx() -> i32: | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def PyUnicode_FromString(s: str) -> CPtr: | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def PyImport_Import(name: CPtr) -> CPtr: | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def _Py_DecRef(name: CPtr): | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def PyObject_GetAttrString(m: CPtr, s: str) -> CPtr: | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def PyTuple_New(n: i32) -> CPtr: | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def PyObject_CallObject(a: CPtr, b: CPtr) -> CPtr: | ||
pass | ||
|
||
@ccall(header="Python.h") | ||
def PyLong_AsLongLong(a: CPtr) -> i32: | ||
pass | ||
|
||
def my_f(): | ||
pName: CPtr; pModule: CPtr; pFunc: CPtr; pArgs: CPtr; pValue: CPtr | ||
|
||
pName = PyUnicode_FromString("bindpy_05_module") | ||
assert pName != empty_c_void_p(), "Failed to convert to unicode string bindpy_05_module\n" | ||
|
||
pModule = PyImport_Import(pName) | ||
_Py_DecRef(pName) | ||
assert pModule != empty_c_void_p(), "Failed to load python module bindpy_05_module\n" | ||
|
||
pFunc = PyObject_GetAttrString(pModule, "my_f") | ||
assert pFunc != empty_c_void_p(), "Cannot find function my_f\n" | ||
|
||
pArgs = PyTuple_New(0) | ||
pValue = PyObject_CallObject(pFunc, pArgs) | ||
_Py_DecRef(pArgs) | ||
assert pValue != empty_c_void_p(), "Call to my_f failed\n" | ||
|
||
ans: i32 = PyLong_AsLongLong(pValue) | ||
print("Ans is", ans) | ||
assert ans == 5 | ||
|
||
|
||
def main0(): | ||
Py_Initialize() | ||
argv1: CPtr = Py_DecodeLocale("", empty_c_void_p()) | ||
PySys_SetArgv(1, pointer(argv1)) | ||
|
||
my_f() | ||
|
||
assert(Py_FinalizeEx() >= 0), "BindPython: Unknown Error in FinalizeEx()\n" | ||
|
||
main0() |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def my_f(): | ||
print("hello from python") | ||
return 5 |