Skip to content

Commit

Permalink
TEST: Add cpython_interop initial example for LLVM
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaikh-Ubaid committed Jul 25, 2023
1 parent 5693585 commit f23a8d8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ RUN(NAME bindpy_01 LABELS cpython c_py ENABLE_CPYTHON NOFAST COPY_TO_B
RUN(NAME bindpy_02 LABELS cpython c_py LINK_NUMPY COPY_TO_BIN bindpy_02_module.py)
RUN(NAME bindpy_03 LABELS cpython c_py LINK_NUMPY NOFAST COPY_TO_BIN bindpy_03_module.py)
RUN(NAME bindpy_04 LABELS cpython c_py LINK_NUMPY NOFAST COPY_TO_BIN bindpy_04_module.py)
RUN(NAME bindpy_05 LABELS llvm_py c_py ENABLE_CPYTHON COPY_TO_BIN bindpy_05_module.py)
RUN(NAME test_generics_01 LABELS cpython llvm c NOFAST)
RUN(NAME test_cmath LABELS cpython llvm c NOFAST)
RUN(NAME test_complex_01 LABELS cpython llvm c wasm wasm_x64)
Expand Down
80 changes: 80 additions & 0 deletions integration_tests/bindpy_05.py
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()
3 changes: 3 additions & 0 deletions integration_tests/bindpy_05_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def my_f():
print("hello from python")
return 5

0 comments on commit f23a8d8

Please sign in to comment.