Skip to content

Commit ba2dff6

Browse files
BindPython ASR Pass: aggregate type conversions (#2803)
* 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
1 parent 61a27d5 commit ba2dff6

File tree

4 files changed

+699
-29
lines changed

4 files changed

+699
-29
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ RUN(NAME bindpy_02 LABELS cpython c_py EXTRA_ARGS --link-numpy COPY_TO
666666
RUN(NAME bindpy_03 LABELS cpython c_py EXTRA_ARGS --link-numpy NOFAST COPY_TO_BIN bindpy_03_module.py)
667667
RUN(NAME bindpy_04 LABELS cpython c_py EXTRA_ARGS --link-numpy NOFAST COPY_TO_BIN bindpy_04_module.py)
668668
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)
669+
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)
669670
RUN(NAME test_generics_01 LABELS cpython llvm llvm_jit c NOFAST)
670671
RUN(NAME test_cmath LABELS cpython llvm llvm_jit c NOFAST)
671672
RUN(NAME test_complex_01 LABELS cpython llvm llvm_jit c wasm wasm_x64)

integration_tests/bindpy_06.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from lpython import i32, f64, pythoncall, Const
2+
from numpy import empty, int32, float64
3+
4+
5+
@pythoncall(module = "bindpy_06_module")
6+
def get_cpython_version() -> str:
7+
pass
8+
9+
10+
@pythoncall(module = "bindpy_06_module")
11+
def get_modified_dict(d: dict[str, i32]) -> dict[str, i32]:
12+
pass
13+
14+
15+
@pythoncall(module = "bindpy_06_module")
16+
def get_modified_list(d: list[str]) -> list[str]:
17+
pass
18+
19+
@pythoncall(module = "bindpy_06_module")
20+
def get_modified_tuple(t: tuple[i32, i32]) -> tuple[i32, i32, i32]:
21+
pass
22+
23+
24+
@pythoncall(module = "bindpy_06_module")
25+
def get_modified_set(s: set[i32]) -> set[i32]:
26+
pass
27+
28+
29+
def test_list():
30+
l: list[str] = ["LPython"]
31+
lr: list[str] = get_modified_list(l)
32+
assert len(lr) == 2
33+
assert lr[0] == "LPython"
34+
assert lr[1] == "LFortran"
35+
36+
37+
def test_tuple():
38+
t: tuple[i32, i32] = (2, 4)
39+
tr: tuple[i32, i32, i32] = get_modified_tuple(t)
40+
assert tr[0] == t[0]
41+
assert tr[1] == t[1]
42+
assert tr[2] == t[0] + t[1]
43+
44+
45+
def test_set():
46+
s: set[i32] = {1, 2, 3}
47+
sr: set[i32] = get_modified_set(s)
48+
assert len(sr) == 4
49+
assert 1 in sr
50+
assert 2 in sr
51+
assert 3 in sr
52+
assert 100 in sr
53+
54+
55+
def test_dict():
56+
d: dict[str, i32] = {
57+
"LPython": 50
58+
}
59+
dr: dict[str, i32] = get_modified_dict(d)
60+
assert len(dr) == 2
61+
assert dr["LPython"] == 50
62+
assert dr["LFortran"] == 100
63+
64+
65+
def main0():
66+
test_list()
67+
test_tuple()
68+
test_set()
69+
test_dict()
70+
71+
72+
main0()

integration_tests/bindpy_06_module.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import platform
2+
3+
4+
def get_cpython_version():
5+
return platform.python_version()
6+
7+
8+
def get_modified_dict(d):
9+
d["LFortran"] = 100
10+
return d
11+
12+
13+
def get_modified_list(l):
14+
l.append("LFortran")
15+
return l
16+
17+
18+
def get_modified_tuple(t):
19+
return (t[0], t[1], t[0] + t[1])
20+
21+
22+
def get_modified_set(s):
23+
s.add(100)
24+
return s

0 commit comments

Comments
 (0)