Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix integration tests with Python 3.11 #2275

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 6 commits into from
Aug 17, 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
55 changes: 55 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,58 @@ jobs:
cd integration_tests
./run_tests.py -b c_sym cpython_sym llvm_sym
./run_tests.py -b c_sym cpython_sym llvm_sym -f

python_3_11:
name: Run Integration tests with Python 3.11
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: mamba-org/setup-micromamba@v1
with:
environment-name: lp
condarc: |
channels:
- conda-forge
create-args: >-
llvmdev=11.1.0
bison=3.4
re2c
zlib
cmake
make
python=3.11.4
numpy

- uses: hendrikmuhs/ccache-action@main
with:
key: ${{ github.job }}-${{ matrix.os }}

- name: Show Python version
shell: bash -e -l {0}
run: python --version

- name: Build
shell: bash -e -l {0}
run: |
./build0.sh
cmake . -G"Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Debug \
-DWITH_LLVM=yes \
-DLPYTHON_BUILD_ALL=yes \
-DWITH_STACKTRACE=no \
-DWITH_RUNTIME_STACKTRACE=no \
-DCMAKE_PREFIX_PATH="$CONDA_PREFIX" \
-DCMAKE_INSTALL_PREFIX=`pwd`/inst \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache

cmake --build . -j16 --target install

- name: Test
shell: bash -e -l {0}
run: |
cd integration_tests
./run_tests.py -b cpython
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ RUN(NAME structs_31 LABELS cpython llvm c)
RUN(NAME structs_32 LABELS cpython llvm c)
RUN(NAME structs_33 LABELS cpython llvm c)
RUN(NAME structs_34 LABELS cpython llvm c)
RUN(NAME structs_35 LABELS cpython llvm)

RUN(NAME symbolics_01 LABELS cpython_sym c_sym llvm_sym NOFAST)
RUN(NAME symbolics_02 LABELS cpython_sym c_sym llvm_sym NOFAST)
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/array_expr_03.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from lpython import i8, i32, dataclass
from lpython import i8, i32, dataclass, field
from numpy import empty, int8, array


@dataclass
class LPBHV_small:
dim: i32 = 4
a: i8[4] = empty(4, dtype=int8)
a: i8[4] = field(default_factory=lambda: empty(4, dtype=int8))


def g():
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/cast_01.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from lpython import i32, u8, u32, dataclass
from lpython import i32, u8, u32, dataclass, field
from numpy import empty, uint8

@dataclass
class LPBHV_small:
dim : i32 = 4
a : u8[4] = empty(4, dtype=uint8)
a : u8[4] = field(default_factory=lambda: empty(4, dtype=uint8))

def main0():
lphv_small : LPBHV_small = LPBHV_small()
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/structs_04.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lpython import i32, f32, f64, dataclass
from lpython import i32, f32, f64, dataclass, field
from copy import deepcopy

@dataclass
Expand All @@ -9,7 +9,7 @@ class A:
@dataclass
class B:
z: i32
a: A = A(f32(0.0), 0)
a: A = field(default_factory=lambda: A(f32(0.0), 0))

def f(b: B):
print(b.z, b.a.x, b.a.y)
Expand Down
6 changes: 3 additions & 3 deletions integration_tests/structs_09.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lpython import i32, f32, f64, dataclass
from lpython import i32, f32, f64, dataclass, field

@dataclass
class C:
Expand All @@ -7,13 +7,13 @@ class C:
@dataclass
class B:
z: i32
bc: C = C(f32(0.0))
bc: C = field(default_factory=lambda: C(f32(0.0)))

@dataclass
class A:
y: f32
x: i32
b: B = B(0, C(f32(0.0)))
b: B = field(default_factory=lambda: B(0, C(f32(0.0))))


def f(a: A):
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/structs_10.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from lpython import i32, f64, dataclass
from lpython import i32, f64, dataclass, field
from numpy import empty, float64

@dataclass
class Mat:
mat: f64[2, 2] = empty((2, 2), dtype=float64)
mat: f64[2, 2] = field(default_factory=lambda: empty((2, 2), dtype=float64))

@dataclass
class Vec:
vec: f64[2] = empty(2, dtype=float64)
vec: f64[2] = field(default_factory=lambda: empty(2, dtype=float64))

@dataclass
class MatVec:
mat: Mat = Mat()
vec: Vec = Vec()
mat: Mat = field(default_factory=lambda: Mat())
vec: Vec = field(default_factory=lambda: Vec())

def rotate(mat_vec: MatVec) -> f64[2]:
rotated_vec: f64[2] = empty(2, dtype=float64)
Expand Down
6 changes: 3 additions & 3 deletions integration_tests/structs_17.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from lpython import i32, f32, f64, dataclass
from lpython import i32, f32, f64, dataclass, field

@dataclass
class B:
z: i32
@dataclass
class C:
cz: f32
bc: C = C(f32(0.0))
bc: C = field(default_factory=lambda: C(f32(0.0)))

@dataclass
class A:
y: f32
x: i32
b: B = B(0, B.C(f32(0.0)))
b: B = field(default_factory=lambda: B(0, B.C(f32(0.0))))


def f(a: A):
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/structs_31.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lpython import packed, dataclass, i32, InOut
from lpython import packed, dataclass, field, i32, InOut

@packed
@dataclass
Expand All @@ -8,7 +8,7 @@ class inner_struct:
@packed
@dataclass
class outer_struct:
b: inner_struct = inner_struct(0)
b: inner_struct = field(default_factory=lambda: inner_struct(0))

def update_my_inner_struct(my_inner_struct: InOut[inner_struct]) -> None:
my_inner_struct.a = 99999
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/structs_32.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lpython import packed, dataclass, i32, InOut
from lpython import packed, dataclass, field, i32, InOut


@packed
Expand All @@ -10,7 +10,7 @@ class inner_struct:
@packed
@dataclass
class outer_struct:
b: inner_struct = inner_struct(0)
b: inner_struct = field(default_factory=lambda: inner_struct(0))


def update_my_inner_struct(my_inner_struct: InOut[inner_struct]) -> None:
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/structs_33.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lpython import packed, dataclass, i32, ccallback, CPtr, ccall
from lpython import packed, dataclass, field, i32

# test issue 2125

Expand All @@ -11,7 +11,7 @@ class inner_struct:
@packed
@dataclass
class outer_struct:
inner_s : inner_struct = inner_struct()
inner_s : inner_struct = field(default_factory=lambda: inner_struct())


def check() -> None:
Expand Down
26 changes: 26 additions & 0 deletions integration_tests/structs_35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from lpython import dataclass, field, i32
from numpy import array

@dataclass
class X:
a: i32 = 123
b: bool = True
c: list[i32] = field(default_factory=lambda: [1, 2, 3])
d: i32[3] = field(default_factory=lambda: array([4, 5, 6]))
e: i32 = field(default=-5)

def main0():
x: X = X()
print(x)
assert x.a == 123
assert x.b == True
assert x.c[0] == 1
assert x.d[1] == 5
assert x.e == -5
x.c[0] = 3
x.d[0] = 3
print(x)
assert x.c[0] == 3
assert x.d[0] == 3

main0()
29 changes: 29 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7802,6 +7802,35 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = ASR::make_SizeOfType_t(al, x.base.base.loc,
arg_type, size_type, nullptr);
return ;
} else if( call_name == "field" ) {
if (x.n_args != 0) {
throw SemanticError("'field' expects only keyword arguments", x.base.base.loc);
}

if (x.n_keywords != 1) {
throw SemanticError("'field' expects one keyword argument", x.base.base.loc);
}

args.reserve(al, 1);
visit_expr_list(x.m_args, x.n_args, args);

if( std::string(x.m_keywords[0].m_arg) != "default_factory" && std::string(x.m_keywords[0].m_arg) != "default" ) {
throw SemanticError("Unrecognised keyword argument, " +
std::string(x.m_keywords[0].m_arg), x.base.base.loc);
}

if ( std::string(x.m_keywords[0].m_arg) == "default_factory") {
if (!AST::is_a<AST::Lambda_t>(*x.m_keywords[0].m_value)) {
throw SemanticError("Only lambda functions currently supported as default_factory value", x.base.base.loc);
}

AST::Lambda_t* lambda_fn = AST::down_cast<AST::Lambda_t>(x.m_keywords[0].m_value);
this->visit_expr(*lambda_fn->m_body);
} else {
// field has default argument provided
this->visit_expr(*x.m_keywords[0].m_value);
}
return ;
} else if(
call_name == "f64" ||
call_name == "f32" ||
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/lpython/lpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import ctypes
import platform
from dataclasses import dataclass as py_dataclass, is_dataclass as py_is_dataclass
from dataclasses import dataclass as py_dataclass, is_dataclass as py_is_dataclass, field
import functools


Expand All @@ -11,7 +11,7 @@
"overload", "ccall", "TypeVar", "pointer", "c_p_pointer", "Pointer",
"p_c_pointer", "vectorize", "inline", "Union", "static",
"packed", "Const", "sizeof", "ccallable", "ccallback", "Callable",
"Allocatable", "In", "Out", "InOut", "dataclass", "S"]
"Allocatable", "In", "Out", "InOut", "dataclass", "field", "S"]

# data-types

Expand Down Expand Up @@ -717,9 +717,9 @@ def get_rtlib_dir():
python_path = "-I" + get_python_inc() + " "
numpy_path = "-I" + get_include() + " "
rt_path_01 = "-I" + get_rtlib_dir() + "/../libasr/runtime "
rt_path_02 = "-L" + get_rtlib_dir() + " -Wl,-rpath " \
rt_path_02 = "-L" + get_rtlib_dir() + " -Wl,-rpath," \
+ get_rtlib_dir() + " -llpython_runtime "
python_lib = "-L" + get_python_lib() + "/../.. -lpython" + \
python_lib = "-L" + get_python_lib() + "/../.." + f" -Wl,-rpath,{get_python_lib()+'/../..'}" + " -lpython" + \
get_python_version() + " -lm"

# ----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_04-387747b.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"basename": "asr-structs_04-387747b",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/structs_04.py",
"infile_hash": "c19af3c3fbac1430c22c5aaf69aea7c622faa9d7c4e7734edbd0066d",
"infile_hash": "1e20c2ac044ab88183c50ecb481ac7c50992ed622f8bb94772c6df25",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_04-387747b.stdout",
Expand Down