diff --git a/CMakeLists.txt b/CMakeLists.txt index 278befa..08f9633 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,16 +83,15 @@ add_definitions( -DBOOST_ALL_DYN_LINK ) ########## if( Nymph_ENABLE_PYTHON ) - find_package( Python3 REQUIRED COMPONENTS Interpreter Development ) set(PYBIND11_PYTHON_VERSION ${Python3_VERSION} CACHE STRING "") find_package( pybind11 REQUIRED ) include_directories( ${Python3_INCLUDE_DIRS} ) - + add_compile_definitions( NYMPH_USING_PYTHON ) set_option( Scarab_BUILD_PYTHON TRUE ) add_subdirectory( Python/Bindings ) - else( Nymph_ENABLE_PYTHON ) + remove_definitions( NYMPH_USING_PYTHON ) set_option( Scarab_BUILD_PYTHON FALSE ) endif( Nymph_ENABLE_PYTHON ) diff --git a/Cpp/Executables/Main/CMakeLists.txt b/Cpp/Executables/Main/CMakeLists.txt index 4bfc255..c7ad842 100644 --- a/Cpp/Executables/Main/CMakeLists.txt +++ b/Cpp/Executables/Main/CMakeLists.txt @@ -17,6 +17,18 @@ if( Nymph_BUILD_NYMPH_EXE ) ) endif( Nymph_BUILD_NYMPH_EXE ) +if( Nymph_ENABLE_PYTHON ) + set( Nymph_SOURCES + ${Nymph_SOURCES} + PyImportTest.cc + ) + set( Nymph_EXE_LIBRARIES + ${Nymph_EXE_LIBRARIES} + pybind11::embed + pybind11::module + ) +endif() + if( Nymph_SOURCES ) set( Nymph_PROGRAMS ) pbuilder_executables( diff --git a/Cpp/Executables/Main/PyImportTest.cc b/Cpp/Executables/Main/PyImportTest.cc new file mode 100644 index 0000000..e52669e --- /dev/null +++ b/Cpp/Executables/Main/PyImportTest.cc @@ -0,0 +1,15 @@ + +#include +#include + +namespace py = pybind11; + +int main() +{ + py::scoped_interpreter guard{}; + + py::module_ sys = py::module_::import("sys"); + py::print(sys.attr("path")); + + return 0; +} diff --git a/Testing/CMakeLists.txt b/Testing/CMakeLists.txt index 7f477bc..a2c268d 100644 --- a/Testing/CMakeLists.txt +++ b/Testing/CMakeLists.txt @@ -90,6 +90,15 @@ set( testing_LIB_DEPENDENCIES NymphTesting ) +set( testing_PUBLIC_LIBS Catch2::Catch2 ) + +if( Nymph_ENABLE_PYTHON ) + set( testing_PUBLIC_LIBS + ${testing_PUBLIC_LIBS} + pybind11::embed + ) +endif( Nymph_ENABLE_PYTHON ) + # Interface library was recommended for Catch: # https://stackoverflow.com/questions/34896891/catch-lib-unit-testing-and-ctest-cmake-integration/34900012#34900012 #add_library( Catch INTERFACE ) @@ -99,7 +108,7 @@ pbuilder_executable( EXECUTABLE RunTests SOURCES ${testing_SOURCES} PROJECT_LIBRARIES ${testing_LIB_DEPENDENCIES} - PUBLIC_EXTERNAL_LIBRARIES Catch2::Catch2 + PUBLIC_EXTERNAL_LIBRARIES ${testing_PUBLIC_LIBS} ) set( programs "RunTests" ) diff --git a/Testing/Cpp/TestProcessorVisibility.cc b/Testing/Cpp/TestProcessorVisibility.cc index 45e7051..4a3ac5c 100644 --- a/Testing/Cpp/TestProcessorVisibility.cc +++ b/Testing/Cpp/TestProcessorVisibility.cc @@ -13,6 +13,11 @@ #include "catch2/catch_test_macros.hpp" +#ifdef NYMPH_USING_PYTHON +#include +namespace py = pybind11; +#endif + LOGGER( testlog, "TestProcessorVisibility" ); TEST_CASE( "processor_visibility" ) @@ -32,10 +37,34 @@ TEST_CASE( "processor_visibility" ) } -// SECTION( "Python" ) -// { -// LINFO( testlog, "Python Processor Visibility"); -// -// } + SECTION( "Python - no import" ) + { + LINFO( testlog, "Python Processor Visibility (no python import)"); + + REQUIRE_FALSE( tptToolbox.CouldBuild( "hello-world-python" ) ); + } + +#ifdef NYMPH_USING_PYTHON + SECTION( "Python - with import" ) + { + py::scoped_interpreter guard{}; +// py::module_ sys = py::module_::import("sys"); +// py::print(sys.attr("path")); + + LINFO( testlog, "Python Processor Visibility (with python import)"); + + try + { + py::module_ nymph = py::module_::import("nymph"); + REQUIRE( tptToolbox.CouldBuild( "hello-world-py" ) ); + } + catch(const std::exception& e) + { + LWARN( testlog, "Unable to import `nymph` ") + } + + + } +#endif } diff --git a/Testing/Python/Bindings/testprocessor.py b/Testing/Python/Bindings/testprocessor.py index d8019ca..2829c55 100644 --- a/Testing/Python/Bindings/testprocessor.py +++ b/Testing/Python/Bindings/testprocessor.py @@ -60,8 +60,8 @@ class TestPyProcCreator(unittest.TestCase): def test_creating(self): - registrar = nymph_bindings.processor.register_py_processor('testprocessor', 'TestProcessor', 'processor-name') - test_proc = nymph_bindings.processor.create_processor('processor-name', 'test-proc') + registrar = nymph_bindings.processor._register('testprocessor', 'TestProcessor', 'processor-name') + test_proc = nymph_bindings.processor._create('processor-name', 'test-proc') self.assertEqual(test_proc.name, 'test-proc') diff --git a/Testing/Python/Bindings/testprocfactory.py b/Testing/Python/Bindings/testprocfactory.py index 5a49e2b..b809d99 100644 --- a/Testing/Python/Bindings/testprocfactory.py +++ b/Testing/Python/Bindings/testprocfactory.py @@ -16,7 +16,7 @@ class TestNoProc(unittest.TestCase): '''Testing the situation where the processor type doesn't exist''' def test_noproc(self): with self.assertRaises(RuntimeError) as cm: - nb.processor.create( "blah", "blah-blah" ) + nb.processor._create( "blah", "blah-blah" ) the_exception = cm.exception self.assertEqual(str(the_exception), "Did not find processor with type ") @@ -24,7 +24,7 @@ class TestCppProc(unittest.TestCase): '''Testing the creation of a C++ processor, which won't work''' def test_cppproc(self): with self.assertRaises(RuntimeError) as cm: - nb.processor.create( "hello-world-cpp", "hw" ) + nb.processor._create( "hello-world-cpp", "hw" ) the_exception = cm.exception self.assertEqual(str(the_exception), "Registrar did not cast correctly for ")