From 5b3547dfd20c0dbda7a9d617ddafe522303ae566 Mon Sep 17 00:00:00 2001 From: Ben Nibbelink Date: Tue, 6 Feb 2024 12:41:53 -0600 Subject: [PATCH 1/8] working with PYTHONPATH=/root/.local/lib and have the cython libs in /root/.local/lib --- cyclus/__init__.py | 8 ++--- src/CMakeLists.txt | 21 ++++++++++++- src/pyhooks.cc | 78 ++++++++++++++++++++++++++++------------------ 3 files changed, 70 insertions(+), 37 deletions(-) diff --git a/cyclus/__init__.py b/cyclus/__init__.py index d87664c371..164bb9f2c4 100644 --- a/cyclus/__init__.py +++ b/cyclus/__init__.py @@ -1,7 +1,3 @@ -from __future__ import print_function, unicode_literals - __version__ = '1.5.5' - -from cyclus.lib import py_import_call_init - -py_import_call_init() +# from cyclus.lib import py_import_call_init, py_import_init +import pymodule, eventhooks, pyinfile \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4dd765fcc1..520dfe8980 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -98,6 +98,8 @@ copy_when_diff( ########################################################### ############# cython configuration ######################## ########################################################### + +SET(PreCythonLibs ${LIBS}) if(Cython_FOUND) # some setup set(cython_include_directories "") @@ -132,7 +134,7 @@ if(Cython_FOUND) set(_generated_h "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cc.h") set(_generated_cc "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cc.gen") set(_h_file "${CMAKE_CURRENT_SOURCE_DIR}/${name}.h") - set(_cc_file "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cc") + set(_cc_file "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp") set_source_files_properties(${_cc_file} PROPERTIES GENERATED TRUE) EXECUTE_PROCESS(COMMAND ${CYTHON_EXECUTABLE} --cplus @@ -143,6 +145,23 @@ if(Cython_FOUND) IF(NOT "${res_var_c}" STREQUAL "0") message(FATAL_ERROR "Cython compilation of ${file} failed!") ENDIF() + + ADD_LIBRARY(${name} ${_cc_file}) + SET_TARGET_PROPERTIES(${name} PROPERTIES + OUTPUT_NAME "${name}" + PREFIX "" + INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib" + INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib" + ) + SET(LIBS ${LIBS} ${name}) + + TARGET_LINK_LIBRARIES(${name} dl ${PreCythonLibs}) + + INSTALL( + TARGETS ${name} + LIBRARY DESTINATION lib + ) + copy_when_diff(${_generated_h} ${_h_file}) copy_when_diff(${_generated_cc} ${_cc_file}) endforeach() diff --git a/src/pyhooks.cc b/src/pyhooks.cc index d949fc61ad..5bd154c92a 100644 --- a/src/pyhooks.cc +++ b/src/pyhooks.cc @@ -1,10 +1,9 @@ +#include "Python.h" #include "pyhooks.h" #ifdef CYCLUS_WITH_PYTHON #include -#include "Python.h" - extern "C" { #include "eventhooks.h" #include "pyinfile.h" @@ -17,39 +16,58 @@ bool PY_INTERP_INIT = false; void PyAppendInitTab(void) { -#if PY_MAJOR_VERSION < 3 - // Not used before Python 3 -#else - PyImport_AppendInittab("_cyclus_eventhooks", PyInit_eventhooks); - PyImport_AppendInittab("_cyclus_pyinfile", PyInit_pyinfile); - PyImport_AppendInittab("_cyclus_pymodule", PyInit_pymodule); -#endif + if (PyImport_AppendInittab("eventhooks", PyInit_eventhooks) == -1) { + fprintf(stderr, "Error appending 'eventhooks' to the initialization table\n"); + } + + if (PyImport_AppendInittab("pyinfile", PyInit_pyinfile) == -1) { + fprintf(stderr, "Error appending 'pyinfile' to the initialization table\n"); + } + + if (PyImport_AppendInittab("pymodule", PyInit_pymodule) == -1) { + fprintf(stderr, "Error appending 'pymodule' to the initialization table\n"); + } } void PyImportInit(void) { -#if PY_MAJOR_VERSION < 3 - initeventhooks(); - initpyinfile(); - initpymodule(); -#else - PyImport_ImportModule("_cyclus_eventhooks"); - PyImport_ImportModule("_cyclus_pyinfile"); - PyImport_ImportModule("_cyclus_pymodule"); -#endif -}; - + PyObject* module_eventhooks = PyImport_ImportModule("eventhooks"); + if (module_eventhooks == NULL) { + PyErr_Print(); // Print Python error information + fprintf(stderr, "Error importing 'eventhooks' module\n"); + } + + PyObject* module_pyinfile = PyImport_ImportModule("pyinfile"); + if (module_pyinfile == NULL) { + PyErr_Print(); + fprintf(stderr, "Error importing 'pyinfile' module\n"); + } + + PyObject* module_pymodule = PyImport_ImportModule("pymodule"); + if (module_pymodule == NULL) { + PyErr_Print(); + fprintf(stderr, "Error importing 'pymodule' module\n"); + } +} void PyImportCallInit(void) { -#if PY_MAJOR_VERSION < 3 - initeventhooks(); - initpyinfile(); - initpymodule(); -#else - PyInit_eventhooks(); - PyInit_pyinfile(); - PyInit_pymodule(); -#endif -}; + PyObject* init_eventhooks = PyInit_eventhooks(); + if (init_eventhooks == NULL) { + PyErr_Print(); + fprintf(stderr, "Error calling PyInit_eventhooks()\n"); + } + + PyObject* init_pyinfile = PyInit_pyinfile(); + if (init_pyinfile == NULL) { + PyErr_Print(); + fprintf(stderr, "Error calling PyInit_pyinfile()\n"); + } + + PyObject* init_pymodule = PyInit_pymodule(); + if (init_pymodule == NULL) { + PyErr_Print(); + fprintf(stderr, "Error calling PyInit_pymodule()\n"); + } +} void PyStart(void) { From 712c2e5fafad5a726d961259385a3384329b146b Mon Sep 17 00:00:00 2001 From: Ben Nibbelink Date: Tue, 6 Feb 2024 18:47:00 -0600 Subject: [PATCH 2/8] installs cython libs to lib and to python site packages --- .gitignore | 8 +++++--- cyclus/__init__.py | 1 - docker/Dockerfile | 3 +-- src/CMakeLists.txt | 16 ++++++++++------ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 1bf40c6478..37121cc719 100644 --- a/.gitignore +++ b/.gitignore @@ -62,12 +62,14 @@ Debug/ # generated cython ignores *.cc.gen *.cc.h +*.cpp.gen +*.cpp.h *.h.gen -src/eventhooks.cc +src/eventhooks.cpp src/eventhooks.h -src/pyinfile.cc +src/pyinfile.cpp src/pyinfile.h -src/pymodule.cc +src/pymodule.cpp src/pymodule.h # this is copied over by the installer diff --git a/cyclus/__init__.py b/cyclus/__init__.py index 164bb9f2c4..df5c8f4ed4 100644 --- a/cyclus/__init__.py +++ b/cyclus/__init__.py @@ -1,3 +1,2 @@ __version__ = '1.5.5' -# from cyclus.lib import py_import_call_init, py_import_init import pymodule, eventhooks, pyinfile \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index 8c8aca2502..4ca08f92c4 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -134,5 +134,4 @@ RUN cyclus_unit_tests FROM cyclus-test as cyclus-pytest -RUN cd tests && python -m pytest --ignore test_main.py - +RUN cd tests && python -m pytest diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 520dfe8980..f0cba298b1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -131,22 +131,22 @@ if(Cython_FOUND) foreach(file ${CYCLUS_CORE_CYTHON_FILES}) message(STATUS "Cython Compiling ${file}") get_filename_component(name ${file} NAME_WE) - set(_generated_h "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cc.h") - set(_generated_cc "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cc.gen") + set(_generated_h "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp.h") + set(_generated_cpp "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp.gen") set(_h_file "${CMAKE_CURRENT_SOURCE_DIR}/${name}.h") - set(_cc_file "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp") + set(_cpp_file "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp") set_source_files_properties(${_cc_file} PROPERTIES GENERATED TRUE) EXECUTE_PROCESS(COMMAND ${CYTHON_EXECUTABLE} --cplus ${include_directory_arg} ${version_arg} ${cython_debug_arg} ${CYTHON_FLAGS} - --output-file ${_generated_cc} ${file} + --output-file ${_generated_cpp} ${file} RESULT_VARIABLE res_var_c) IF(NOT "${res_var_c}" STREQUAL "0") message(FATAL_ERROR "Cython compilation of ${file} failed!") ENDIF() - ADD_LIBRARY(${name} ${_cc_file}) + ADD_LIBRARY(${name} ${_cpp_file}) SET_TARGET_PROPERTIES(${name} PROPERTIES OUTPUT_NAME "${name}" PREFIX "" @@ -161,9 +161,13 @@ if(Cython_FOUND) TARGETS ${name} LIBRARY DESTINATION lib ) + INSTALL( + TARGETS ${name} + LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES} + ) copy_when_diff(${_generated_h} ${_h_file}) - copy_when_diff(${_generated_cc} ${_cc_file}) + copy_when_diff(${_generated_cpp} ${_cpp_file}) endforeach() endif(Cython_FOUND) ########################################################### From 916842ad03740e2c9799f1704e1f68befc03edf0 Mon Sep 17 00:00:00 2001 From: Ben Nibbelink Date: Tue, 6 Feb 2024 20:21:22 -0600 Subject: [PATCH 3/8] rename cmake var --- src/CMakeLists.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f0cba298b1..041a47f6f5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -98,8 +98,7 @@ copy_when_diff( ########################################################### ############# cython configuration ######################## ########################################################### - -SET(PreCythonLibs ${LIBS}) +SET(NoCythonLibs ${LIBS}) if(Cython_FOUND) # some setup set(cython_include_directories "") @@ -155,7 +154,7 @@ if(Cython_FOUND) ) SET(LIBS ${LIBS} ${name}) - TARGET_LINK_LIBRARIES(${name} dl ${PreCythonLibs}) + TARGET_LINK_LIBRARIES(${name} dl ${NoCythonLibs}) INSTALL( TARGETS ${name} From cba43a0b32051d67e8625a4168821b6672f8edd6 Mon Sep 17 00:00:00 2001 From: bennibbelink <79653949+bennibbelink@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:05:36 -0600 Subject: [PATCH 4/8] remove --ignore test_main.py flag from workflow Signed-off-by: bennibbelink <79653949+bennibbelink@users.noreply.github.com> --- .github/workflows/build_test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index 558196166f..9d4a8a8349 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -70,7 +70,7 @@ jobs: - name: Cyclus Python Tests run: | - cd tests && python -m pytest --ignore test_main.py + cd tests && python -m pytest - name: Unit Test Coverage Report run: | @@ -103,4 +103,4 @@ jobs: - name: Coveralls Finished uses: coverallsapp/github-action@v2 with: - parallel-finished: true \ No newline at end of file + parallel-finished: true From a3f91d251e76d6088165e09ec80c22bcb2abaefc Mon Sep 17 00:00:00 2001 From: bennibbelink <79653949+bennibbelink@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:22:21 -0600 Subject: [PATCH 5/8] Update CHANGELOG.rst Signed-off-by: bennibbelink <79653949+bennibbelink@users.noreply.github.com> --- CHANGELOG.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 144cb5fb94..9c7b6893f7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -45,6 +45,7 @@ Since last release quantize, bids that one was not able to fullfill and caused cyclus to crash. (#1552) * Resolve deprecation warnings involving (#1611) * Resolve segmentation faults when calling Cbc (#1614) +* Resolve segmentation faults wehn using cyclus via Python (#1666) From a897fded809a5b5872ead92c3e78d8c7680b66dd Mon Sep 17 00:00:00 2001 From: bennibbelink <79653949+bennibbelink@users.noreply.github.com> Date: Tue, 6 Feb 2024 22:44:48 -0600 Subject: [PATCH 6/8] Update CHANGELOG.rst Co-authored-by: Paul Wilson Signed-off-by: bennibbelink <79653949+bennibbelink@users.noreply.github.com> --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 9c7b6893f7..70410df19a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -45,7 +45,7 @@ Since last release quantize, bids that one was not able to fullfill and caused cyclus to crash. (#1552) * Resolve deprecation warnings involving (#1611) * Resolve segmentation faults when calling Cbc (#1614) -* Resolve segmentation faults wehn using cyclus via Python (#1666) +* Resolve segmentation faults when using cyclus via Python (#1666) From df213a409d25318a58342fcf33c2f050049f5751 Mon Sep 17 00:00:00 2001 From: Ben Nibbelink Date: Wed, 7 Feb 2024 10:29:06 -0600 Subject: [PATCH 7/8] changed cython compiled code back to .cc and removed from libcyclus build --- .gitignore | 8 +++----- src/CMakeLists.txt | 14 ++++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 37121cc719..1bf40c6478 100644 --- a/.gitignore +++ b/.gitignore @@ -62,14 +62,12 @@ Debug/ # generated cython ignores *.cc.gen *.cc.h -*.cpp.gen -*.cpp.h *.h.gen -src/eventhooks.cpp +src/eventhooks.cc src/eventhooks.h -src/pyinfile.cpp +src/pyinfile.cc src/pyinfile.h -src/pymodule.cpp +src/pymodule.cc src/pymodule.h # this is copied over by the installer diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 041a47f6f5..020d1beb2d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -130,22 +130,23 @@ if(Cython_FOUND) foreach(file ${CYCLUS_CORE_CYTHON_FILES}) message(STATUS "Cython Compiling ${file}") get_filename_component(name ${file} NAME_WE) - set(_generated_h "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp.h") - set(_generated_cpp "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp.gen") + set(_generated_h "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cc.h") + set(_generated_cc "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cc.gen") set(_h_file "${CMAKE_CURRENT_SOURCE_DIR}/${name}.h") - set(_cpp_file "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp") + set(_cc_file "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cc") set_source_files_properties(${_cc_file} PROPERTIES GENERATED TRUE) EXECUTE_PROCESS(COMMAND ${CYTHON_EXECUTABLE} --cplus ${include_directory_arg} ${version_arg} ${cython_debug_arg} ${CYTHON_FLAGS} - --output-file ${_generated_cpp} ${file} + --output-file ${_generated_cc} ${file} RESULT_VARIABLE res_var_c) IF(NOT "${res_var_c}" STREQUAL "0") message(FATAL_ERROR "Cython compilation of ${file} failed!") ENDIF() - ADD_LIBRARY(${name} ${_cpp_file}) + ADD_LIBRARY(${name} ${_cc_file}) + SET(CythonModuleSrc ${CythonModuleSrc} ${_cc_file}) SET_TARGET_PROPERTIES(${name} PROPERTIES OUTPUT_NAME "${name}" PREFIX "" @@ -166,7 +167,7 @@ if(Cython_FOUND) ) copy_when_diff(${_generated_h} ${_h_file}) - copy_when_diff(${_generated_cpp} ${_cpp_file}) + copy_when_diff(${_generated_cc} ${_cc_file}) endforeach() endif(Cython_FOUND) ########################################################### @@ -197,6 +198,7 @@ foreach(ccfile ${CYCLUS_COIN_SRC}) endforeach() list(REMOVE_ITEM cc_files ${CMAKE_CURRENT_SOURCE_DIR}/coin_helpers.cc) list(REMOVE_ITEM cc_files ${CMAKE_CURRENT_SOURCE_DIR}/prog_translator.cc) +list(REMOVE_ITEM cc_files ${CythonModuleSrc}) SET(CYCLUS_CORE_SRC ${CYCLUS_CORE_SRC} ${cc_files}) From 0bc09a128667eea4cdb68f220582d2bd3f355a43 Mon Sep 17 00:00:00 2001 From: Ben Nibbelink Date: Wed, 7 Feb 2024 11:49:57 -0600 Subject: [PATCH 8/8] comment cmake for clarity --- src/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 020d1beb2d..7a37268df4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -198,6 +198,8 @@ foreach(ccfile ${CYCLUS_COIN_SRC}) endforeach() list(REMOVE_ITEM cc_files ${CMAKE_CURRENT_SOURCE_DIR}/coin_helpers.cc) list(REMOVE_ITEM cc_files ${CMAKE_CURRENT_SOURCE_DIR}/prog_translator.cc) +# The cython modules are built as their own shared libraries +# We don't want them to be included in libcyclus.so list(REMOVE_ITEM cc_files ${CythonModuleSrc}) SET(CYCLUS_CORE_SRC ${CYCLUS_CORE_SRC} ${cc_files})