Skip to content

Support syntax for passing struct in CPython #1814

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

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ RUN(NAME structs_01 LABELS cpython llvm c)
RUN(NAME structs_02 LABELS cpython llvm c)
RUN(NAME structs_03 LABELS llvm c)
RUN(NAME structs_04 LABELS cpython llvm c)
RUN(NAME structs_05 LABELS llvm c)
RUN(NAME structs_05 LABELS cpython llvm c)
RUN(NAME structs_06 LABELS cpython llvm c)
RUN(NAME structs_07 LABELS llvm c
EXTRAFILES structs_07b.c)
Expand All @@ -480,6 +480,7 @@ RUN(NAME structs_21 LABELS cpython llvm c)
RUN(NAME structs_22 LABELS cpython llvm c)
RUN(NAME structs_23 LABELS cpython llvm c)
RUN(NAME structs_24 LABELS cpython llvm c)
RUN(NAME structs_25 LABELS cpython llvm c)
RUN(NAME sizeof_01 LABELS llvm c
EXTRAFILES sizeof_01b.c)
RUN(NAME sizeof_02 LABELS cpython llvm c)
Expand Down
1 change: 0 additions & 1 deletion integration_tests/structs_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def update_2(s: A[:]):
s[1].c = i8(3)

def g():
# TODO: Enable cpython in integration_tests.
y: A[2] = empty([2], dtype=A)
y[0] = A(1.1, 1, i64(1), f32(1.1), i16(1), i8(1), True)
y[1] = A(2.2, 2, i64(2), f32(2.2), i16(2), i8(2), True)
Expand Down
20 changes: 20 additions & 0 deletions integration_tests/structs_25.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from lpython import i32, dataclass
from numpy import empty

@dataclass
class Foo:
x: i32
y: i32

def init(foos: Foo[:]) -> None:
foos[0] = Foo(5, 21)

def main0() -> None:
foos: Foo[1] = empty(1, dtype=Foo)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would just support the Array[] syntax for arrays of structs, like this:

Suggested change
foos: Foo[1] = empty(1, dtype=Foo)
foos: Array[Foo, 1] = empty(1, dtype=Foo)

Then it should be possible to implement this cleanly in lpython.py as well as LPython.

init(foos)
print("foos[0].x =", foos[0].x)

assert foos[0].x == 5
assert foos[0].y == 21

main0()
1 change: 1 addition & 0 deletions src/runtime/lpython/lpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __call__(self, arg):
return self._convert(arg)

def dataclass(arg):
arg.__class_getitem__ = lambda self: None
return py_dataclass(arg)

def is_dataclass(obj):
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_05-fa98307.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"basename": "asr-structs_05-fa98307",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/structs_05.py",
"infile_hash": "0ca482232f99c40614dc5b994fa8c9f4865fbe72f5a133b02914b5ad",
"infile_hash": "94d06fb2844e1060d5df5291c3ccc4bd0e2bc8a8a59651814f0c4f0e",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_05-fa98307.stdout",
Expand Down