From 158bbbf941a4083f9db1eee22eb14ca859125fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Sat, 29 Jul 2023 21:09:29 -0500 Subject: [PATCH 1/3] Add a test for lpython emulation mode --- integration_tests/CMakeLists.txt | 3 +++ integration_tests/lpython_emulation_01.py | 21 +++++++++++++++++++ integration_tests/lpython_emulation_01_mod.py | 7 +++++++ 3 files changed, 31 insertions(+) create mode 100644 integration_tests/lpython_emulation_01.py create mode 100644 integration_tests/lpython_emulation_01_mod.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 5e1b733e05..b69a329975 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -763,3 +763,6 @@ RUN(NAME lpython_decorator_01 LABELS cpython) RUN(NAME lpython_decorator_02 LABELS cpython) COMPILE(NAME import_order_01 LABELS cpython llvm c) # any + +# LPython emulation mode +RUN(NAME lpython_emulation_01 LABELS cpython) diff --git a/integration_tests/lpython_emulation_01.py b/integration_tests/lpython_emulation_01.py new file mode 100644 index 0000000000..29b6c4a6e5 --- /dev/null +++ b/integration_tests/lpython_emulation_01.py @@ -0,0 +1,21 @@ +import lpython_emulation_01_mod +import lpython +from lpython import ccall, i64 +from types import FunctionType +lpython.CTypes.emulations = {k: v for k, v in \ + lpython_emulation_01_mod.__dict__.items() \ + if isinstance(v, FunctionType)} + +@ccall +def f1(a: i64) -> i64: + pass + +@ccall +def f2(a: i64): + pass + +def main(): + assert f1(2) == 3 + f2(4) + +main() diff --git a/integration_tests/lpython_emulation_01_mod.py b/integration_tests/lpython_emulation_01_mod.py new file mode 100644 index 0000000000..3a12ea4c5d --- /dev/null +++ b/integration_tests/lpython_emulation_01_mod.py @@ -0,0 +1,7 @@ +from lpython import i64 + +def f1(a: i64) -> i64: + return a + 1 + +def f2(a: i64): + assert a == 4 From 81e9c0d5f60dfb2b2d4ce26c663d72970fc9dd2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Sat, 29 Jul 2023 21:34:53 -0500 Subject: [PATCH 2/3] Add NOMOD to the testsuite This is needed in order to use the .emulation dict in lpython.CTypes. --- integration_tests/CMakeLists.txt | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index b69a329975..6a5bf0ce43 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -76,12 +76,13 @@ message("LPYTHON_RTLIB_DIR: ${LPYTHON_RTLIB_DIR}") message("LPYTHON_RTLIB_LIBRARY: ${LPYTHON_RTLIB_LIBRARY}") -macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS RUN_COPY_TO_BIN) +macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_NOMOD RUN_EXTRA_ARGS RUN_COPY_TO_BIN) set(fail ${${RUN_FAIL}}) set(name ${${RUN_NAME}}) set(file_name ${${RUN_FILE_NAME}}) set(labels ${${RUN_LABELS}}) set(extra_files ${${RUN_EXTRAFILES}}) + set(no_mod ${${RUN_NOMOD}}) set(extra_args ${${RUN_EXTRA_ARGS}}) set(copy_to_bin ${${RUN_COPY_TO_BIN}}) @@ -213,8 +214,13 @@ macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXT endif() add_test(${name} python ${CMAKE_CURRENT_SOURCE_DIR}/${file_name}.py) - set_tests_properties(${name} PROPERTIES - ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/../src/runtime/lpython:${CMAKE_SOURCE_DIR}/..;LPYTHON_PY_MOD_NAME=${PY_MOD};LPYTHON_PY_MOD_PATH=${CMAKE_CURRENT_BINARY_DIR}") + if (no_mod) + set_tests_properties(${name} PROPERTIES + ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/../src/runtime/lpython:${CMAKE_SOURCE_DIR}/..") + else() + set_tests_properties(${name} PROPERTIES + ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/../src/runtime/lpython:${CMAKE_SOURCE_DIR}/..;LPYTHON_PY_MOD_NAME=${PY_MOD};LPYTHON_PY_MOD_PATH=${CMAKE_CURRENT_BINARY_DIR}") + endif() if (labels) set_tests_properties(${name} PROPERTIES LABELS "${labels}") endif() @@ -305,7 +311,7 @@ macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXT endmacro(RUN_UTIL) macro(RUN) - set(options FAIL NOFAST ENABLE_CPYTHON LINK_NUMPY) + set(options FAIL NOFAST NOMOD ENABLE_CPYTHON LINK_NUMPY) set(oneValueArgs NAME IMPORT_PATH COPY_TO_BIN) set(multiValueArgs LABELS EXTRAFILES) cmake_parse_arguments(RUN "${options}" "${oneValueArgs}" @@ -329,14 +335,14 @@ macro(RUN) endif() if (NOT FAST) - RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS RUN_COPY_TO_BIN) + RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_NOMOD RUN_EXTRA_ARGS RUN_COPY_TO_BIN) endif() if ((FAST) AND (NOT RUN_NOFAST)) set(RUN_EXTRA_ARGS ${RUN_EXTRA_ARGS} --fast) set(RUN_NAME "${RUN_NAME}_FAST") list(REMOVE_ITEM RUN_LABELS cpython cpython_sym) # remove cpython, cpython_sym, from --fast test - RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS RUN_COPY_TO_BIN) + RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_NOMOD RUN_EXTRA_ARGS RUN_COPY_TO_BIN) endif() endmacro(RUN) @@ -765,4 +771,4 @@ RUN(NAME lpython_decorator_02 LABELS cpython) COMPILE(NAME import_order_01 LABELS cpython llvm c) # any # LPython emulation mode -RUN(NAME lpython_emulation_01 LABELS cpython) +RUN(NAME lpython_emulation_01 LABELS cpython NOMOD) From 9979cceba122867e2dcb612d35a4df2956d136e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Sat, 29 Jul 2023 21:21:34 -0500 Subject: [PATCH 3/3] Ensure cf.restype is always available The previous commit fails and this commit fixes it. Fixes #2222. --- 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 2f0eee0b55..63bcc98c19 100644 --- a/src/runtime/lpython/lpython.py +++ b/src/runtime/lpython/lpython.py @@ -377,6 +377,7 @@ def get_crtlib_path(): arg_ctype = convert_type_to_ctype(arg_type) argtypes.append(arg_ctype) self.cf.argtypes = argtypes + self.cf.restype = None if "return" in self.annotations: res_type = self.annotations["return"] if res_type is not None: