Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BindPython ASR Pass: aggregate type conversions #2803

Merged
merged 10 commits into from
Aug 18, 2024
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 llvm_py EXTRA_ARGS --enable-cpython COPY_TO_BIN bindpy_06_module.py REQ_PY_VER 3.10)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add NOFAST to this. Also, I think we should be testing this with cpython as well to ensure what we support also works with CPython. We should not support something that is not supported with CPython.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test bindpy_05.py with CPython.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bindpy_05.py with cpython fails with:

Traceback (most recent call last):
  File "/home/vipul/Workspace/python/lpython/integration_tests/bindpy_05.py", line 3, in <module>
    @ccall(header="Python.h")
     ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vipul/Workspace/python/lpython/src/runtime/lpython/lpython.py", line 482, in wrap
    func = CTypes(func, c_shared_lib, c_shared_lib_path)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vipul/Workspace/python/lpython/src/runtime/lpython/lpython.py", line 372, in __init__
    self.cf = self.library[self.name]
              ~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/lib64/python3.12/ctypes/__init__.py", line 397, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: /home/vipul/Workspace/python/lpython/src/runtime/lpython/../liblpython_runtime.so: undefined symbol: Py_Initialize

I have added cpython for bindpy_06 and NOFAST flag.

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
Loading