From 3c013007f162f829f5b8fa6245b80b93a14f73f8 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 16 May 2023 19:28:13 +0530 Subject: [PATCH 1/3] Implement __class_getitem__() for dataclass --- src/runtime/lpython/lpython.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/runtime/lpython/lpython.py b/src/runtime/lpython/lpython.py index 103ec16da5..9c9eb79ef5 100644 --- a/src/runtime/lpython/lpython.py +++ b/src/runtime/lpython/lpython.py @@ -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): From b794a6e768834a9f3f941a7cf601e08a45fd57df Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 16 May 2023 19:28:41 +0530 Subject: [PATCH 2/3] TEST: Add struct passing syntax test in cpython --- integration_tests/CMakeLists.txt | 3 ++- integration_tests/structs_05.py | 1 - integration_tests/structs_25.py | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 integration_tests/structs_25.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index e709d60ef9..c918c9ae76 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -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) @@ -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) diff --git a/integration_tests/structs_05.py b/integration_tests/structs_05.py index d738778109..b730a5387b 100644 --- a/integration_tests/structs_05.py +++ b/integration_tests/structs_05.py @@ -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) diff --git a/integration_tests/structs_25.py b/integration_tests/structs_25.py new file mode 100644 index 0000000000..4ed57ea564 --- /dev/null +++ b/integration_tests/structs_25.py @@ -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) + init(foos) + print("foos[0].x =", foos[0].x) + + assert foos[0].x == 5 + assert foos[0].y == 21 + +main0() From da3751467d2c25fcfe34fa0fb319d34b1bc4b130 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 16 May 2023 20:25:06 +0530 Subject: [PATCH 3/3] TEST: Update reference tests --- tests/reference/asr-structs_05-fa98307.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/reference/asr-structs_05-fa98307.json b/tests/reference/asr-structs_05-fa98307.json index f8694a70e6..bf409124dc 100644 --- a/tests/reference/asr-structs_05-fa98307.json +++ b/tests/reference/asr-structs_05-fa98307.json @@ -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",