Skip to content

Commit

Permalink
BindPython ASR Pass: aggregate type conversions (#2803)
Browse files Browse the repository at this point in the history
* native to cpython tuple conversion
and refactoring

* PythonBind ABI: native to cpython list conversion

* PythonBind ABI: native to cpython set conversion
Backend does not support yet.

* PythonBind ABI: native to cpython dict conversion

* PythonBind ABI: cpython to native list conversion

* PythonBind ABI: cpython to native tuple conversion

* PythonBind ABI: cpython to native set conversion

* PythonBind ABI: cpython to native dict conversion

* PythonBind ABI: testing aggregate type conversions

* NOFAST for bindpy_06 test
  • Loading branch information
Vipul-Cariappa authored Aug 18, 2024
1 parent 61a27d5 commit ba2dff6
Show file tree
Hide file tree
Showing 4 changed files with 699 additions and 29 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ RUN(NAME bindpy_02 LABELS cpython c_py EXTRA_ARGS --link-numpy COPY_TO
RUN(NAME bindpy_03 LABELS cpython c_py EXTRA_ARGS --link-numpy NOFAST COPY_TO_BIN bindpy_03_module.py)
RUN(NAME bindpy_04 LABELS cpython c_py EXTRA_ARGS --link-numpy NOFAST COPY_TO_BIN bindpy_04_module.py)
RUN(NAME bindpy_05 LABELS llvm_py c_py EXTRA_ARGS --enable-cpython COPY_TO_BIN bindpy_05_module.py REQ_PY_VER 3.10)
RUN(NAME bindpy_06 LABELS cpython llvm_py EXTRA_ARGS --enable-cpython NOFAST COPY_TO_BIN bindpy_06_module.py REQ_PY_VER 3.10)
RUN(NAME test_generics_01 LABELS cpython llvm llvm_jit c NOFAST)
RUN(NAME test_cmath LABELS cpython llvm llvm_jit c NOFAST)
RUN(NAME test_complex_01 LABELS cpython llvm llvm_jit c wasm wasm_x64)
Expand Down
72 changes: 72 additions & 0 deletions integration_tests/bindpy_06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from lpython import i32, f64, pythoncall, Const
from numpy import empty, int32, float64


@pythoncall(module = "bindpy_06_module")
def get_cpython_version() -> str:
pass


@pythoncall(module = "bindpy_06_module")
def get_modified_dict(d: dict[str, i32]) -> dict[str, i32]:
pass


@pythoncall(module = "bindpy_06_module")
def get_modified_list(d: list[str]) -> list[str]:
pass

@pythoncall(module = "bindpy_06_module")
def get_modified_tuple(t: tuple[i32, i32]) -> tuple[i32, i32, i32]:
pass


@pythoncall(module = "bindpy_06_module")
def get_modified_set(s: set[i32]) -> set[i32]:
pass


def test_list():
l: list[str] = ["LPython"]
lr: list[str] = get_modified_list(l)
assert len(lr) == 2
assert lr[0] == "LPython"
assert lr[1] == "LFortran"


def test_tuple():
t: tuple[i32, i32] = (2, 4)
tr: tuple[i32, i32, i32] = get_modified_tuple(t)
assert tr[0] == t[0]
assert tr[1] == t[1]
assert tr[2] == t[0] + t[1]


def test_set():
s: set[i32] = {1, 2, 3}
sr: set[i32] = get_modified_set(s)
assert len(sr) == 4
assert 1 in sr
assert 2 in sr
assert 3 in sr
assert 100 in sr


def test_dict():
d: dict[str, i32] = {
"LPython": 50
}
dr: dict[str, i32] = get_modified_dict(d)
assert len(dr) == 2
assert dr["LPython"] == 50
assert dr["LFortran"] == 100


def main0():
test_list()
test_tuple()
test_set()
test_dict()


main0()
24 changes: 24 additions & 0 deletions integration_tests/bindpy_06_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import platform


def get_cpython_version():
return platform.python_version()


def get_modified_dict(d):
d["LFortran"] = 100
return d


def get_modified_list(l):
l.append("LFortran")
return l


def get_modified_tuple(t):
return (t[0], t[1], t[0] + t[1])


def get_modified_set(s):
s.add(100)
return s
Loading

0 comments on commit ba2dff6

Please sign in to comment.