Skip to content

Commit

Permalink
Merge pull request cyclus#1666 from cyclus/fix-python-segfault
Browse files Browse the repository at this point in the history
Fix Python segmentation fault behavior
  • Loading branch information
gonuke authored Feb 7, 2024
2 parents c32ba98 + 0bc09a1 commit 85068a0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -103,4 +103,4 @@ jobs:
- name: Coveralls Finished
uses: coverallsapp/github-action@v2
with:
parallel-finished: true
parallel-finished: true
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Since last release
quantize, bids that one was not able to fullfill and caused cyclus to crash. (#1552)
* Resolve deprecation warnings involving <boost/detail/sp_typeinfo.hpp> (#1611)
* Resolve segmentation faults when calling Cbc (#1614)
* Resolve segmentation faults when using cyclus via Python (#1666)



Expand Down
7 changes: 1 addition & 6 deletions cyclus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
from __future__ import print_function, unicode_literals

__version__ = '1.5.5'

from cyclus.lib import py_import_call_init

py_import_call_init()
import pymodule, eventhooks, pyinfile
3 changes: 1 addition & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ copy_when_diff(
###########################################################
############# cython configuration ########################
###########################################################
SET(NoCythonLibs ${LIBS})
if(Cython_FOUND)
# some setup
set(cython_include_directories "")
Expand Down Expand Up @@ -143,6 +144,28 @@ 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(CythonModuleSrc ${CythonModuleSrc} ${_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 ${NoCythonLibs})

INSTALL(
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})
endforeach()
Expand Down Expand Up @@ -175,6 +198,9 @@ 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})

Expand Down
78 changes: 48 additions & 30 deletions src/pyhooks.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "Python.h"
#include "pyhooks.h"

#ifdef CYCLUS_WITH_PYTHON
#include <stdlib.h>

#include "Python.h"

extern "C" {
#include "eventhooks.h"
#include "pyinfile.h"
Expand All @@ -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) {
Expand Down

0 comments on commit 85068a0

Please sign in to comment.