From f806a62673fac347ebe3622f99c9563365387aa7 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Sat, 16 Mar 2024 15:36:00 +0000 Subject: [PATCH 01/50] Initial merge with xeus-clang-repl --- .github/workflows/main.yml | 17 +- CMakeLists.txt | 64 ++++++- share/jupyter/kernels/xcpp/kernel.json.in | 5 +- src/main.cpp | 69 +++++++ src/xinterpreter.cpp | 92 +++++++++- src/xmagics/pythonexec.cpp | 208 ++++++++++++++++++++++ src/xmagics/pythonexec.hpp | 55 ++++++ test/pytest.ini | 2 +- test/test_xcpp_kernel.py | 2 +- 9 files changed, 491 insertions(+), 23 deletions(-) create mode 100644 src/xmagics/pythonexec.cpp create mode 100644 src/xmagics/pythonexec.hpp diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 93f71d38..405ab8ff 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,9 +15,9 @@ concurrency: jobs: unix: - + runs-on: ${{ matrix.os }} - + strategy: fail-fast: false matrix: @@ -53,10 +53,10 @@ jobs: mkdir -p build cd build cmake .. \ - -DCMAKE_PREFIX_PATH=$CONDA_PREFIX \ - -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \ - -DXEUS_CPP_ENABLE_CODE_COVERAGE=${{ matrix.coverage }} \ - ${{ matrix.extra_cmake_flags }} + -DCMAKE_PREFIX_PATH=$CONDA_PREFIX \ + -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \ + -DXEUS_CPP_ENABLE_CODE_COVERAGE=${{ matrix.coverage }} \ + ${{ matrix.extra_cmake_flags }} - name: build shell: bash -l {0} @@ -74,6 +74,7 @@ jobs: shell: bash -l {0} run: | cd build/test + export CPLUS_INCLUDE_PATH=$CONDA_PREFIX/include:$CONDA_BUILD_SYSROOT:$CONDA_BUILD_SYSROOT/..:$CONDA_BUILD_SYSROOT/../x86_64-conda-linux-gnu/include/c++/12.3.0:$CONDA_BUILD_SYSROOT/../x86_64-conda-linux-gnu/include/c++/12.3.0/include:$CONDA_BUILD_SYSROOT/../include:$CONDA_BUILD_SYSROOT/usr/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/include-fixed:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/../../../../x86_64-conda-linux-gnu/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../x86_64-conda-linux-gnu/sysroot/usr/include ./test_xeus_cpp timeout-minutes: 4 @@ -169,7 +170,7 @@ jobs: - name: Setup emsdk shell: bash -l {0} run: | - emsdk install ${{matrix.emsdk_ver}} + emsdk install ${{matrix.emsdk_ver}} - name: Build xeus-cpp shell: bash -l {0} @@ -177,7 +178,7 @@ jobs: emsdk activate ${{matrix.emsdk_ver}} source $CONDA_EMSDK_DIR/emsdk_env.sh micromamba create -f environment-wasm-host.yml --platform=emscripten-wasm32 - + mkdir build pushd build diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e88a228..701132ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,12 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}") set(XEUS_CPP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) +enable_language(CXX) +set(CMAKE_CXX_EXTENSIONS NO) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +include(GNUInstallDirs) + # Versionning # =========== @@ -44,8 +50,8 @@ option(XEUS_CPP_BUILD_STATIC "Build xeus-cpp static library" ON) OPTION(XEUS_CPP_BUILD_SHARED "Split xcpp build into executable and library" ON) OPTION(XEUS_CPP_BUILD_EXECUTABLE "Build the xcpp executable" ON) -OPTION(XEUS_CPP_USE_SHARED_XEUS "Link xcpp with the xeus shared library (instead of the static library)" ON) -OPTION(XEUS_CPP_USE_SHARED_XEUS_CPP "Link xcpp with the xeus shared library (instead of the static library)" ON) +OPTION(XEUS_CPP_USE_SHARED_XEUS "Link xcpp with the xeus shared library (instead of the static library)" ON) +OPTION(XEUS_CPP_USE_SHARED_XEUS_CPP "Link xcpp with the xeus shared library (instead of the static library)" ON) # Test options OPTION(XEUS_CPP_BUILD_TESTS "xeus-cpp test suite" ON) @@ -74,7 +80,6 @@ if(EMSCRIPTEN) SET(XEUS_CPP_BUILD_TESTS OFF) endif() - # Dependencies # ============ @@ -100,7 +105,9 @@ else() add_compile_options(-fexceptions) endif () -if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel") +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR + CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR + CMAKE_CXX_COMPILER_ID MATCHES "Intel") if(NOT EMSCRIPTEN) add_compile_options(-Wunused-parameter -Wextra -Wreorder) endif() @@ -109,6 +116,13 @@ endif () if(EMSCRIPTEN) # ENV (https://github.com/emscripten-core/emscripten/commit/6d9681ad04f60b41ef6345ab06c29bbc9eeb84e0) set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[ENV']\"") + set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") +endif() + +find_package(CppInterOp REQUIRED CONFIG PATHS "${CMAKE_PREFIX_PATH}" "${CMAKE_PREFIX_PATH}/lib" "${CPPINTEROP_DIR}" "${CPPINTEROP_DIR}/lib") +if(CppInterOp_FOUND) + set(CppInterOp_CLANG_VERSION "17") + message(STATUS "Found CppInterOp: config=${CppInterOp_CONFIG} dir=${CppInterOp_DIR} (found version=${CppInterOp_VERSION} compatible with Clang ${CppInterOp_CLANG_VERSION}.x)") endif() find_package(CppInterOp REQUIRED) @@ -184,10 +198,18 @@ set(XEUS_CPP_SRC src/xoptions.cpp src/xparser.cpp src/xutils.cpp + src/xdemangle.hpp + src/xinspect.hpp + src/xsystem.hpp + src/xparser.hpp + src/xmagics/os.hpp + src/xmagics/os.cpp ) if(EMSCRIPTEN) list(APPEND XEUS_CPP_SRC src/xinterpreter_wasm.cpp) +else() + list(APPEND XEUS_CPP_SRC src/xmagics/pythonexec.hpp src/xmagics/pythonexec.cpp) endif() set(XEUS_CPP_MAIN_SRC @@ -349,6 +371,30 @@ if (XEUS_CPP_BUILD_EXECUTABLE) xeus_cpp_set_common_options(xcpp) xeus_cpp_set_kernel_options(xcpp) target_link_libraries(xcpp PRIVATE xeus-zmq) + set_target_properties(xcpp PROPERTIES + ENABLE_EXPORTS 1 + CXX_STANDARD ${CMAKE_CXX_STANDARD}) + target_link_libraries(xcpp PUBLIC xeus-cpp pthread Python::Python) + + ##TODO: We may be need sse RPATH + set_target_properties(xcpp clangCppInterOp PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE) + if(APPLE) + target_link_libraries(xcpp PUBLIC -Wl,-w -Wl,-bind_at_load -Wl,-undefined,dynamic_lookup) + elseif(NOT MSVC) + target_link_libraries(xcpp PUBLIC -Wl,--unresolved-symbols=ignore-in-object-files) + endif() + + target_include_directories(xeus-cpp PUBLIC ${Python_INCLUDE_DIRS}) + target_link_libraries(xeus-cpp PUBLIC ${PYTHON_LIBRARIES}) + target_link_libraries(xeus-cpp ${PYTHON_LIBRARIES_Development_Main}) + set_target_properties(xeus-cpp PROPERTIES + PUBLIC_HEADER "${XEUS_CPP_HEADERS}" + COMPILE_DEFINITIONS "XEUS_CPP_EXPORTS" + PREFIX "" + VERSION ${${PROJECT_NAME}_VERSION} + SOVERSION ${XEUS_CPP_VERSION_MAJOR} + OUTPUT_NAME "libxeus-cpp" + CXX_STANDARD ${CMAKE_CXX_STANDARD}) endif() if(EMSCRIPTEN) @@ -378,10 +424,10 @@ set(XEUS_CPP_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NA if (XEUS_CPP_BUILD_SHARED) install(TARGETS ${XEUS_CPP_TARGETS} EXPORT ${PROJECT_NAME}-targets - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp) + ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp) # Makes the project importable from the build directory export(EXPORT ${PROJECT_NAME}-targets @@ -391,7 +437,7 @@ endif () # Install xcpp if (XEUS_CPP_BUILD_EXECUTABLE) install(TARGETS xcpp - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}) endif() if(XEUS_CPP_BUILD_EXECUTABLE OR EMSCRIPTEN) diff --git a/share/jupyter/kernels/xcpp/kernel.json.in b/share/jupyter/kernels/xcpp/kernel.json.in index de229b64..14238439 100644 --- a/share/jupyter/kernels/xcpp/kernel.json.in +++ b/share/jupyter/kernels/xcpp/kernel.json.in @@ -12,7 +12,6 @@ "-I", "@CMAKE_INSTALL_PREFIX@/include", "-std=c++17"@XEUS_CPP_OMP@ ], - "language": "cpp", - "metadata": {"debugger": false - } + "language": "C++", + "metadata": {"debugger": false} } diff --git a/src/main.cpp b/src/main.cpp index 83f6f73f..568d8a55 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,6 +29,75 @@ #include "xeus-cpp/xinterpreter.hpp" #include "xeus-cpp/xutils.hpp" +/* +#ifdef __GNUC__ +void handler(int sig) +{ + void* array[10]; + + // get void*'s for all entries on the stack + std::size_t size = backtrace(array, 10); + + // print out all the frames to stderr + fprintf(stderr, "Error: signal %d:\n", sig); + backtrace_symbols_fd(array, size, STDERR_FILENO); + exit(1); +} +#endif + +void stop_handler(int /*sig/) +{ + exit(0); +} + +bool should_print_version(int argc, char* argv[]) +{ + for (int i = 0; i < argc; ++i) + { + if (std::string(argv[i]) == "--version") + { + return true; + } + } + return false; +} + +std::string extract_filename(int& argc, char* argv[]) +{ + std::string res = ""; + for (int i = 0; i < argc; ++i) + { + if ((std::string(argv[i]) == "-f") && (i + 1 < argc)) + { + res = argv[i + 1]; + for (int j = i; j < argc - 2; ++j) + { + argv[j] = argv[j + 2]; + } + argc -= 2; + break; + } + } + return res; +} + +using interpreter_ptr = std::unique_ptr; + +interpreter_ptr build_interpreter(int argc, char** argv) +{ + std::vector interpreter_args; + for (int i = 1; i < argc; i++) { + if (argv[i] == "-f") { + i++; // skip the value of -f which is a json file. + continue; + } + interpreter_args.push_back(argv[i]); + } + interpreter_ptr interp_ptr = interpreter_ptr(new xcpp::interpreter(interpreter_args.size(), interpreter_args.data())); + return interp_ptr; +} +*/ + int main(int argc, char* argv[]) { if (xcpp::should_print_version(argc, argv)) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index b25606e5..cff52626 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -1,5 +1,6 @@ /************************************************************************************ * Copyright (c) 2023, xeus-cpp contributors * + * Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht * * Copyright (c) 2023, Martin Vassilev * * * * Distributed under the terms of the BSD 3-Clause License. * @@ -55,6 +56,47 @@ void* createInterpreter(const Args &ExtraArgs = {}) { return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/); } +/* +#include + +#include + +#include "xeus-cpp/xbuffer.hpp" +#include "xeus-cpp/xeus_cpp_config.hpp" + +#include "xeus-cpp/xinterpreter.hpp" +#include "xeus-cpp/xmagics.hpp" + +#include "xinput.hpp" +#include "xinspect.hpp" +// #include "xmagics/executable.hpp" +// #include "xmagics/execution.hpp" +#include "xmagics/os.hpp" +#include "xmagics/pythonexec.hpp" +#include "xparser.hpp" +#include "xsystem.hpp" + +using Args = std::vector; + +void* createInterpreter(const Args &ExtraArgs = {}) { + Args ClangArgs = {/*"-xc++"*/"-v"}; // ? {"-Xclang", "-emit-llvm-only", "-Xclang", "-diagnostic-log-file", "-Xclang", "-", "-xc++"}; + std::string resource_dir = Cpp::DetectResourceDir(); + ClangArgs.push_back("-resource-dir"); + ClangArgs.push_back(resource_dir.c_str()); + std::vector CxxSystemIncludes; + Cpp::DetectSystemCompilerIncludePaths(CxxSystemIncludes); + for (const std::string& CxxInclude : CxxSystemIncludes) { + ClangArgs.push_back("-isystem"); + ClangArgs.push_back(CxxInclude.c_str()); + } + ClangArgs.push_back("-I/home/apenev/.conda/envs/xeus-cpp/include/"); + ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end()); + // FIXME: We should process the kernel input options and conditionally pass + // the gpu args here. + return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/); +} +*/ + using namespace std::placeholders; namespace xcpp @@ -91,9 +133,36 @@ __get_cxx_version () long cxx_version = Cpp::Evaluate(code); return std::to_string(cxx_version); } +/* + std::string interpreter::get_stdopt(int argc, const char* const* argv) + { + std::string res = "17"; //TODO: Detect from CppInterOp + for (int i = 0; i < argc; ++i) + { + std::string tmp(argv[i]); + auto pos = tmp.find("-std=c++"); + if (pos != std::string::npos) + { + res = tmp.substr(pos + 8); + break; + } + } + return res; + } +*/ interpreter::interpreter(int argc, const char* const* argv) : +/* + // todo: why is error_stream necessary + std::string error_message; + llvm::raw_string_ostream error_stream(error_message); + // Expose xinterpreter instance to interpreted C++ + process_code(*m_interpreter, "#include \"xeus/xinterpreter.hpp\"", error_stream); + std::string code = "xeus::register_interpreter(static_cast((void*)" + + std::to_string(intptr_t(this)) + "));"; + process_code(*m_interpreter, code.c_str(), error_stream); +*/ xmagics() , p_cout_strbuf(nullptr) , p_cerr_strbuf(nullptr) @@ -104,9 +173,11 @@ __get_cxx_version () createInterpreter(Args(argv ? argv + 1 : argv, argv + argc)); m_version = get_stdopt(); redirect_output(); + // Bootstrap the execution engine init_includes(); init_preamble(); init_magic(); + Cpp::Process("#include "); } interpreter::~interpreter() @@ -153,10 +224,25 @@ __get_cxx_version () std::cerr.rdbuf(&null); } + // Scope guard performing the temporary redirection of input requests. + auto input_guard = input_redirection(allow_stdin); + //std::string evalue; + //bool compilation_result = false; + + std::string err; + std::string out; + // Attempt normal evaluation try { compilation_result = Cpp::Process(code.c_str()); + + Cpp::BeginStdStreamCapture(Cpp::kStdErr); + Cpp::BeginStdStreamCapture(Cpp::kStdOut); + compilation_result = Cpp::Process(code.c_str()); + out = Cpp::EndStdStreamCapture(); + err = Cpp::EndStdStreamCapture(); + std::cout << out; } catch (std::exception& e) { @@ -174,9 +260,13 @@ __get_cxx_version () { errorlevel = 1; ename = "Error :"; - evalue = "Compilation error!"; + //evalue = error_stream.str(); + std::cerr << err; } + //error_stream.str().clear(); + //DiagnosticsOS.str().clear(); + // Flush streams std::cout << std::flush; std::cerr << std::flush; diff --git a/src/xmagics/pythonexec.cpp b/src/xmagics/pythonexec.cpp new file mode 100644 index 00000000..03d5a488 --- /dev/null +++ b/src/xmagics/pythonexec.cpp @@ -0,0 +1,208 @@ +//===------------ pythonexec.hpp - Python/C++ Interoperability ------------===// +// +// Licensed under the Apache License v2.0. +// SPDX-License-Identifier: Apache-2.0 +// +// The full license is in the file LICENSE, distributed with this software. +// +//===----------------------------------------------------------------------===// +// +// This file defines the Python/C++ interoperability in which two cells within +// the same notebook can be in a either language. +// +//===----------------------------------------------------------------------===// + +#include "Python.h" + +#include "pythonexec.hpp" +#include "../xparser.hpp" + +#include +#include +#include +#include +#include +#include + +namespace xcpp { +static PyObject *gMainDict = 0; + +void pythonexec::startup() { + static bool isInitialized = false; + if (isInitialized) + return; + + Py_Initialize(); + + PyRun_SimpleString("import sys\nprint(sys.path)"); + + // Import cppyy module + PyObject* cppyyModule = PyImport_ImportModule("cppyy"); + if (!cppyyModule) { + PyErr_Print(); + Py_Finalize(); + return; // Handle import error as needed + } + + PyObject* mainModule = PyImport_AddModule("__main__"); + PyObject_SetAttrString(mainModule, "cppyy", cppyyModule); + Py_XDECREF(cppyyModule); + isInitialized = true; + // gMainDict = PyModule_GetDict(mainModule); + // Py_INCREF(gMainDict); + // if (!gMainDict) + // printf("Could not add module __main__"); + + // // Retrieve the dictionary of cppyy module + // PyObject* cppyyDict = PyModule_GetDict(cppyyModule); + // Py_DECREF(cppyyModule); + // if (!cppyyDict) { + // PyErr_Print(); + // Py_Finalize(); + // return; // Handle retrieval error as needed + // } + + // // Add cppyyDict to gMainDict (if needed for further usage) + // PyDict_Update(gMainDict, cppyyDict); + + // Py_DECREF(cppyyDict); + // PyRun_SimpleString("import cppyy"); +} + +void pythonexec::get_options(argparser &argpars) { +} + +void pythonexec::execute(std::string &line, std::string &cell) { + // std::istringstream iss(line); + // std::vector results((std::istream_iterator(iss)), + // std::istream_iterator()); + startup(); + +// argparser argpars{"python", "Start executing Python Cell"}; +// auto argpars = get_options(); +// argpars.parse(line); + + std::string code; + + code += cell; + if (trim(code).empty()) + return; + // PyRun_SimpleString( + // "globals_copy_lists = " + // "globals().copy()\nfirst_dict_ints={k:globals_copy_lists[k] for k in " + // "set(globals_copy_lists) if type(globals_copy_lists[k]) == " + // "int}\nfirst_dict_lists={k:globals_copy_lists[k] for k in " + // "set(globals_copy_lists) if type(globals_copy_lists[k]) == list}"); + + // PyRun_SimpleString("tmp = globals().copy()\nvars = [f'int {k} = {v};' for + // k,v in tmp.items() if type(v) == int and not k.startswith('_') and k!='tmp' + // and k!='In' and k!='Out' and k!='sys' and not hasattr(v, + // '__call__')]\nprint(vars)"); PyRun_SimpleString("b = + // globals().copy()\nnew_ints = ' '.join([f'int {k} = {b[k]};' for k in set(b) + // - set(first_dict) if type(b[k]) == int])\nprint('new_ints: ', new_ints)"); + + Cpp::BeginStdStreamCapture(Cpp::kStdErr); + Cpp::BeginStdStreamCapture(Cpp::kStdOut); + + PyRun_SimpleString(code.c_str()); + + std::cout << Cpp::EndStdStreamCapture(); + std::cerr << Cpp::EndStdStreamCapture(); + + // PyObject* objectsRepresentation = PyObject_Repr(gMainDict); + // const char* s = PyUnicode_AsUTF8(objectsRepresentation); + // printf("REPR of global dict: %s\n", s); +} + +void pythonexec::update_python_dict_var(const char *name, int value) { + if (!gMainDict) + startup(); + PyObject *s; + s = PyLong_FromLong(value); + PyDict_SetItemString(gMainDict, name, s); + Py_DECREF(s); +} + +void pythonexec::update_python_dict_var_vector(const char *name, + std::vector &data) { + if (!gMainDict) + startup(); + PyObject *listObj = PyList_New(data.size()); + if (!listObj) + throw std::logic_error("Unable to allocate memory for Python list"); + for (unsigned int i = 0; i < data.size(); i++) { + PyObject *num = PyLong_FromLong((int)data[i]); + if (!num) { + Py_DECREF(listObj); + throw std::logic_error("Unable to allocate memory for Python list"); + } + PyList_SET_ITEM(listObj, i, num); + } + PyDict_SetItemString(gMainDict, name, listObj); +} + +// check python globals +void pythonexec::check_python_globals() { + if (!Py_IsInitialized()) + return; + // execute the command + PyRun_SimpleString("print(globals())"); +} + +// execute a python comand +void pythonexec::exec_python_simple_command(const std::string code) { + if (!Py_IsInitialized()) + return; + // execute the command + PyRun_SimpleString(code.c_str()); +} + +std::string pythonexec::transfer_python_ints_utility() { + if (!Py_IsInitialized()) + return " "; + // transfer ints utility + PyRun_SimpleString( + "def getNewInts():\n glob_ints_utils = globals().copy()\n new_ints " + "= ' '.join([f'int {k} = {glob_ints_utils[k]};' for k in " + "set(glob_ints_utils) - set(first_dict_ints) if type(glob_ints_utils[k]) " + "== int])\n return new_ints"); + PyObject *ints_result = + PyObject_CallFunction(PyDict_GetItemString(gMainDict, "getNewInts"), 0); + if (!ints_result) { + printf("Could not retrieve Python integers!\n"); + return " "; + } else { + std::string newPythonInts = PyUnicode_AsUTF8(ints_result); + // printf("new ints %s\n", PyUnicode_AsUTF8(ints_result)); + Py_DECREF(ints_result); + return newPythonInts; + } +} + +std::string pythonexec::transfer_python_lists_utility() { + if (!Py_IsInitialized()) + return " "; + // transfer lists utility + PyRun_SimpleString("def getNewLists():\n l = globals().copy()\n " + "new_lists = ' '.join([f'int {k}() = {l[k]};' for k in " + "set(l) - set(first_dict_lists) if type(l[k]) == " + "list]).replace('[','{').replace(']','}').replace('(','[')" + ".replace(')',']')\n return new_lists"); + PyObject *lists_result = + PyObject_CallFunction(PyDict_GetItemString(gMainDict, "getNewLists"), 0); + if (!lists_result) { + printf("Could not retrieve Python lists!\n"); + return " "; + } else { + std::string newPythonLists = PyUnicode_AsUTF8(lists_result); + Py_DECREF(lists_result); + return newPythonLists; + } +} + +bool pythonexec::python_check_for_initialisation() { + if (!Py_IsInitialized()) + return false; + return true; +} +} // namespace xcpp diff --git a/src/xmagics/pythonexec.hpp b/src/xmagics/pythonexec.hpp new file mode 100644 index 00000000..bd8a69ce --- /dev/null +++ b/src/xmagics/pythonexec.hpp @@ -0,0 +1,55 @@ +//===------- pythonexec.hpp - Python/C++ Interoperability -------*- C++ -*-===// +// +// Licensed under the Apache License v2.0. +// SPDX-License-Identifier: Apache-2.0 +// +// The full license is in the file LICENSE, distributed with this software. +// +//===----------------------------------------------------------------------===// +// +// This file defines the Python/C++ interoperability in which two cells within +// the same notebook can be in a either language. +// +//===----------------------------------------------------------------------===// + +#ifndef XMAGICS_PYTHONEXEC_HPP +#define XMAGICS_PYTHONEXEC_HPP + +#include "xeus-cpp/xbuffer.hpp" +#include "xeus-cpp/xinterpreter.hpp" +#include "xeus-cpp/xmagics.hpp" +#include "xeus-cpp/xoptions.hpp" + +#include +#include + +namespace xcpp { + + class pythonexec: public xmagic_cell + { + public: + + void get_options(argparser &argpars); + virtual void operator()(const std::string& line, const std::string& cell) override { + std::string cline = line; + std::string ccell = cell; + execute(cline, ccell); + }; + + static void update_python_dict_var(const char *name, int value); + static void update_python_dict_var_vector(const char *name, + std::vector &data); + void check_python_globals(); + void exec_python_simple_command(const std::string code); + static std::string transfer_python_ints_utility(); + static std::string transfer_python_lists_utility(); + static bool python_check_for_initialisation(); + + private: + + static void startup(); + void execute(std::string &line, std::string &cell); + }; + +} // namespace xcpp +#endif diff --git a/test/pytest.ini b/test/pytest.ini index 5aacf83b..649aa953 100644 --- a/test/pytest.ini +++ b/test/pytest.ini @@ -1,3 +1,3 @@ [pytest] testpaths = test -addopts = --nbval --current-env +addopts = --nbval --nbval-current-env diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index c1ac33a3..23b41fdd 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -24,7 +24,7 @@ class XCppTests(jupyter_kernel_test.KernelTests): code_stderr = '#include \nstd::cerr << "oops" << std::endl;' # Pager: code that should display something (anything) in the pager - #code_page_something = "?std::vector" + code_page_something = "?std::vector" # Samples of code which generate a result value (ie, some text # displayed as Out[n]) From 206b3de69a72738de09eeb874027a6a957995670 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:10:38 +0300 Subject: [PATCH 02/50] Update kernel.json.in --- share/jupyter/kernels/xcpp/kernel.json.in | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/share/jupyter/kernels/xcpp/kernel.json.in b/share/jupyter/kernels/xcpp/kernel.json.in index 14238439..8f270a20 100644 --- a/share/jupyter/kernels/xcpp/kernel.json.in +++ b/share/jupyter/kernels/xcpp/kernel.json.in @@ -11,7 +11,7 @@ "-resource-dir", "@XEUS_CPP_RESOURCE_DIR@", "-I", "@CMAKE_INSTALL_PREFIX@/include", "-std=c++17"@XEUS_CPP_OMP@ - ], - "language": "C++", - "metadata": {"debugger": false} + ], "language": "cpp", + "metadata": {"debugger": false + } } From c56bee2bdfd33d351df2148ef756bd64c13f0730 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:11:11 +0300 Subject: [PATCH 03/50] Update kernel.json.in --- share/jupyter/kernels/xcpp/kernel.json.in | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/share/jupyter/kernels/xcpp/kernel.json.in b/share/jupyter/kernels/xcpp/kernel.json.in index 8f270a20..3bc11e9a 100644 --- a/share/jupyter/kernels/xcpp/kernel.json.in +++ b/share/jupyter/kernels/xcpp/kernel.json.in @@ -11,7 +11,8 @@ "-resource-dir", "@XEUS_CPP_RESOURCE_DIR@", "-I", "@CMAKE_INSTALL_PREFIX@/include", "-std=c++17"@XEUS_CPP_OMP@ - ], "language": "cpp", + ], + "language": "cpp", "metadata": {"debugger": false } } From 30b1eebc37432bca608f045d5479a26e8dc223d4 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:11:33 +0300 Subject: [PATCH 04/50] Update kernel.json.in --- share/jupyter/kernels/xcpp/kernel.json.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/share/jupyter/kernels/xcpp/kernel.json.in b/share/jupyter/kernels/xcpp/kernel.json.in index 3bc11e9a..de229b64 100644 --- a/share/jupyter/kernels/xcpp/kernel.json.in +++ b/share/jupyter/kernels/xcpp/kernel.json.in @@ -11,7 +11,7 @@ "-resource-dir", "@XEUS_CPP_RESOURCE_DIR@", "-I", "@CMAKE_INSTALL_PREFIX@/include", "-std=c++17"@XEUS_CPP_OMP@ - ], + ], "language": "cpp", "metadata": {"debugger": false } From b52b514e23df1452b5a0e2a86cf4430f09f15e3e Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:12:47 +0300 Subject: [PATCH 05/50] Update main.cpp --- src/main.cpp | 69 ---------------------------------------------------- 1 file changed, 69 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 568d8a55..83f6f73f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,75 +29,6 @@ #include "xeus-cpp/xinterpreter.hpp" #include "xeus-cpp/xutils.hpp" -/* -#ifdef __GNUC__ -void handler(int sig) -{ - void* array[10]; - - // get void*'s for all entries on the stack - std::size_t size = backtrace(array, 10); - - // print out all the frames to stderr - fprintf(stderr, "Error: signal %d:\n", sig); - backtrace_symbols_fd(array, size, STDERR_FILENO); - exit(1); -} -#endif - -void stop_handler(int /*sig/) -{ - exit(0); -} - -bool should_print_version(int argc, char* argv[]) -{ - for (int i = 0; i < argc; ++i) - { - if (std::string(argv[i]) == "--version") - { - return true; - } - } - return false; -} - -std::string extract_filename(int& argc, char* argv[]) -{ - std::string res = ""; - for (int i = 0; i < argc; ++i) - { - if ((std::string(argv[i]) == "-f") && (i + 1 < argc)) - { - res = argv[i + 1]; - for (int j = i; j < argc - 2; ++j) - { - argv[j] = argv[j + 2]; - } - argc -= 2; - break; - } - } - return res; -} - -using interpreter_ptr = std::unique_ptr; - -interpreter_ptr build_interpreter(int argc, char** argv) -{ - std::vector interpreter_args; - for (int i = 1; i < argc; i++) { - if (argv[i] == "-f") { - i++; // skip the value of -f which is a json file. - continue; - } - interpreter_args.push_back(argv[i]); - } - interpreter_ptr interp_ptr = interpreter_ptr(new xcpp::interpreter(interpreter_args.size(), interpreter_args.data())); - return interp_ptr; -} -*/ - int main(int argc, char* argv[]) { if (xcpp::should_print_version(argc, argv)) From a96818e4fd11eb161b8683d4b92be395f54a79cd Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:34:17 +0300 Subject: [PATCH 06/50] Update CMakeLists.txt --- CMakeLists.txt | 6 ------ 1 file changed, 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 701132ae..f9dc3d01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,12 +119,6 @@ if(EMSCRIPTEN) set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") endif() -find_package(CppInterOp REQUIRED CONFIG PATHS "${CMAKE_PREFIX_PATH}" "${CMAKE_PREFIX_PATH}/lib" "${CPPINTEROP_DIR}" "${CPPINTEROP_DIR}/lib") -if(CppInterOp_FOUND) - set(CppInterOp_CLANG_VERSION "17") - message(STATUS "Found CppInterOp: config=${CppInterOp_CONFIG} dir=${CppInterOp_DIR} (found version=${CppInterOp_VERSION} compatible with Clang ${CppInterOp_CLANG_VERSION}.x)") -endif() - find_package(CppInterOp REQUIRED) if(CppInterOp_FOUND) message(STATUS "Found CppInterOp: config=${CPPINTEROP_CONFIG} dir=${CPPINTEROP_DIR} (found version=${CPPINTEROP_VERSION} compatible with Clang ${CPPINTEROP_LLVM_VERSION_MAJOR}.x)") From f0f188c1dc60d7f49cdf2cb83d2ec2aa7cdd9886 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:40:48 +0300 Subject: [PATCH 07/50] Update CMakeLists.txt --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index f9dc3d01..aabfe547 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,9 @@ endif() find_package(argparse REQUIRED) find_package(pugixml REQUIRED) +set(Python_FIND_VIRTUALENV ONLY) +find_package(Python COMPONENTS Interpreter Development) + # Configuration # ============= From 3f5349ee1bb8189ea22cdb987f55b0343e2f033f Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 05:57:50 +0300 Subject: [PATCH 08/50] Update CMakeLists.txt --- CMakeLists.txt | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aabfe547..1fa8e079 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,9 +127,6 @@ endif() find_package(argparse REQUIRED) find_package(pugixml REQUIRED) -set(Python_FIND_VIRTUALENV ONLY) -find_package(Python COMPONENTS Interpreter Development) - # Configuration # ============= @@ -205,8 +202,6 @@ set(XEUS_CPP_SRC if(EMSCRIPTEN) list(APPEND XEUS_CPP_SRC src/xinterpreter_wasm.cpp) -else() - list(APPEND XEUS_CPP_SRC src/xmagics/pythonexec.hpp src/xmagics/pythonexec.cpp) endif() set(XEUS_CPP_MAIN_SRC @@ -371,7 +366,7 @@ if (XEUS_CPP_BUILD_EXECUTABLE) set_target_properties(xcpp PROPERTIES ENABLE_EXPORTS 1 CXX_STANDARD ${CMAKE_CXX_STANDARD}) - target_link_libraries(xcpp PUBLIC xeus-cpp pthread Python::Python) + target_link_libraries(xcpp PUBLIC xeus-cpp pthread) ##TODO: We may be need sse RPATH set_target_properties(xcpp clangCppInterOp PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE) @@ -381,9 +376,6 @@ if (XEUS_CPP_BUILD_EXECUTABLE) target_link_libraries(xcpp PUBLIC -Wl,--unresolved-symbols=ignore-in-object-files) endif() - target_include_directories(xeus-cpp PUBLIC ${Python_INCLUDE_DIRS}) - target_link_libraries(xeus-cpp PUBLIC ${PYTHON_LIBRARIES}) - target_link_libraries(xeus-cpp ${PYTHON_LIBRARIES_Development_Main}) set_target_properties(xeus-cpp PROPERTIES PUBLIC_HEADER "${XEUS_CPP_HEADERS}" COMPILE_DEFINITIONS "XEUS_CPP_EXPORTS" From aef6ae54dde92d1ef95bd8bc0a5d30bb20a9c531 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 05:59:49 +0300 Subject: [PATCH 09/50] Delete src/xmagics/pythonexec.cpp --- src/xmagics/pythonexec.cpp | 208 ------------------------------------- 1 file changed, 208 deletions(-) delete mode 100644 src/xmagics/pythonexec.cpp diff --git a/src/xmagics/pythonexec.cpp b/src/xmagics/pythonexec.cpp deleted file mode 100644 index 03d5a488..00000000 --- a/src/xmagics/pythonexec.cpp +++ /dev/null @@ -1,208 +0,0 @@ -//===------------ pythonexec.hpp - Python/C++ Interoperability ------------===// -// -// Licensed under the Apache License v2.0. -// SPDX-License-Identifier: Apache-2.0 -// -// The full license is in the file LICENSE, distributed with this software. -// -//===----------------------------------------------------------------------===// -// -// This file defines the Python/C++ interoperability in which two cells within -// the same notebook can be in a either language. -// -//===----------------------------------------------------------------------===// - -#include "Python.h" - -#include "pythonexec.hpp" -#include "../xparser.hpp" - -#include -#include -#include -#include -#include -#include - -namespace xcpp { -static PyObject *gMainDict = 0; - -void pythonexec::startup() { - static bool isInitialized = false; - if (isInitialized) - return; - - Py_Initialize(); - - PyRun_SimpleString("import sys\nprint(sys.path)"); - - // Import cppyy module - PyObject* cppyyModule = PyImport_ImportModule("cppyy"); - if (!cppyyModule) { - PyErr_Print(); - Py_Finalize(); - return; // Handle import error as needed - } - - PyObject* mainModule = PyImport_AddModule("__main__"); - PyObject_SetAttrString(mainModule, "cppyy", cppyyModule); - Py_XDECREF(cppyyModule); - isInitialized = true; - // gMainDict = PyModule_GetDict(mainModule); - // Py_INCREF(gMainDict); - // if (!gMainDict) - // printf("Could not add module __main__"); - - // // Retrieve the dictionary of cppyy module - // PyObject* cppyyDict = PyModule_GetDict(cppyyModule); - // Py_DECREF(cppyyModule); - // if (!cppyyDict) { - // PyErr_Print(); - // Py_Finalize(); - // return; // Handle retrieval error as needed - // } - - // // Add cppyyDict to gMainDict (if needed for further usage) - // PyDict_Update(gMainDict, cppyyDict); - - // Py_DECREF(cppyyDict); - // PyRun_SimpleString("import cppyy"); -} - -void pythonexec::get_options(argparser &argpars) { -} - -void pythonexec::execute(std::string &line, std::string &cell) { - // std::istringstream iss(line); - // std::vector results((std::istream_iterator(iss)), - // std::istream_iterator()); - startup(); - -// argparser argpars{"python", "Start executing Python Cell"}; -// auto argpars = get_options(); -// argpars.parse(line); - - std::string code; - - code += cell; - if (trim(code).empty()) - return; - // PyRun_SimpleString( - // "globals_copy_lists = " - // "globals().copy()\nfirst_dict_ints={k:globals_copy_lists[k] for k in " - // "set(globals_copy_lists) if type(globals_copy_lists[k]) == " - // "int}\nfirst_dict_lists={k:globals_copy_lists[k] for k in " - // "set(globals_copy_lists) if type(globals_copy_lists[k]) == list}"); - - // PyRun_SimpleString("tmp = globals().copy()\nvars = [f'int {k} = {v};' for - // k,v in tmp.items() if type(v) == int and not k.startswith('_') and k!='tmp' - // and k!='In' and k!='Out' and k!='sys' and not hasattr(v, - // '__call__')]\nprint(vars)"); PyRun_SimpleString("b = - // globals().copy()\nnew_ints = ' '.join([f'int {k} = {b[k]};' for k in set(b) - // - set(first_dict) if type(b[k]) == int])\nprint('new_ints: ', new_ints)"); - - Cpp::BeginStdStreamCapture(Cpp::kStdErr); - Cpp::BeginStdStreamCapture(Cpp::kStdOut); - - PyRun_SimpleString(code.c_str()); - - std::cout << Cpp::EndStdStreamCapture(); - std::cerr << Cpp::EndStdStreamCapture(); - - // PyObject* objectsRepresentation = PyObject_Repr(gMainDict); - // const char* s = PyUnicode_AsUTF8(objectsRepresentation); - // printf("REPR of global dict: %s\n", s); -} - -void pythonexec::update_python_dict_var(const char *name, int value) { - if (!gMainDict) - startup(); - PyObject *s; - s = PyLong_FromLong(value); - PyDict_SetItemString(gMainDict, name, s); - Py_DECREF(s); -} - -void pythonexec::update_python_dict_var_vector(const char *name, - std::vector &data) { - if (!gMainDict) - startup(); - PyObject *listObj = PyList_New(data.size()); - if (!listObj) - throw std::logic_error("Unable to allocate memory for Python list"); - for (unsigned int i = 0; i < data.size(); i++) { - PyObject *num = PyLong_FromLong((int)data[i]); - if (!num) { - Py_DECREF(listObj); - throw std::logic_error("Unable to allocate memory for Python list"); - } - PyList_SET_ITEM(listObj, i, num); - } - PyDict_SetItemString(gMainDict, name, listObj); -} - -// check python globals -void pythonexec::check_python_globals() { - if (!Py_IsInitialized()) - return; - // execute the command - PyRun_SimpleString("print(globals())"); -} - -// execute a python comand -void pythonexec::exec_python_simple_command(const std::string code) { - if (!Py_IsInitialized()) - return; - // execute the command - PyRun_SimpleString(code.c_str()); -} - -std::string pythonexec::transfer_python_ints_utility() { - if (!Py_IsInitialized()) - return " "; - // transfer ints utility - PyRun_SimpleString( - "def getNewInts():\n glob_ints_utils = globals().copy()\n new_ints " - "= ' '.join([f'int {k} = {glob_ints_utils[k]};' for k in " - "set(glob_ints_utils) - set(first_dict_ints) if type(glob_ints_utils[k]) " - "== int])\n return new_ints"); - PyObject *ints_result = - PyObject_CallFunction(PyDict_GetItemString(gMainDict, "getNewInts"), 0); - if (!ints_result) { - printf("Could not retrieve Python integers!\n"); - return " "; - } else { - std::string newPythonInts = PyUnicode_AsUTF8(ints_result); - // printf("new ints %s\n", PyUnicode_AsUTF8(ints_result)); - Py_DECREF(ints_result); - return newPythonInts; - } -} - -std::string pythonexec::transfer_python_lists_utility() { - if (!Py_IsInitialized()) - return " "; - // transfer lists utility - PyRun_SimpleString("def getNewLists():\n l = globals().copy()\n " - "new_lists = ' '.join([f'int {k}() = {l[k]};' for k in " - "set(l) - set(first_dict_lists) if type(l[k]) == " - "list]).replace('[','{').replace(']','}').replace('(','[')" - ".replace(')',']')\n return new_lists"); - PyObject *lists_result = - PyObject_CallFunction(PyDict_GetItemString(gMainDict, "getNewLists"), 0); - if (!lists_result) { - printf("Could not retrieve Python lists!\n"); - return " "; - } else { - std::string newPythonLists = PyUnicode_AsUTF8(lists_result); - Py_DECREF(lists_result); - return newPythonLists; - } -} - -bool pythonexec::python_check_for_initialisation() { - if (!Py_IsInitialized()) - return false; - return true; -} -} // namespace xcpp From ce859d91cc2e59641a37d5d2f96dfa92632f1b31 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 06:00:01 +0300 Subject: [PATCH 10/50] Delete src/xmagics/pythonexec.hpp --- src/xmagics/pythonexec.hpp | 55 -------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 src/xmagics/pythonexec.hpp diff --git a/src/xmagics/pythonexec.hpp b/src/xmagics/pythonexec.hpp deleted file mode 100644 index bd8a69ce..00000000 --- a/src/xmagics/pythonexec.hpp +++ /dev/null @@ -1,55 +0,0 @@ -//===------- pythonexec.hpp - Python/C++ Interoperability -------*- C++ -*-===// -// -// Licensed under the Apache License v2.0. -// SPDX-License-Identifier: Apache-2.0 -// -// The full license is in the file LICENSE, distributed with this software. -// -//===----------------------------------------------------------------------===// -// -// This file defines the Python/C++ interoperability in which two cells within -// the same notebook can be in a either language. -// -//===----------------------------------------------------------------------===// - -#ifndef XMAGICS_PYTHONEXEC_HPP -#define XMAGICS_PYTHONEXEC_HPP - -#include "xeus-cpp/xbuffer.hpp" -#include "xeus-cpp/xinterpreter.hpp" -#include "xeus-cpp/xmagics.hpp" -#include "xeus-cpp/xoptions.hpp" - -#include -#include - -namespace xcpp { - - class pythonexec: public xmagic_cell - { - public: - - void get_options(argparser &argpars); - virtual void operator()(const std::string& line, const std::string& cell) override { - std::string cline = line; - std::string ccell = cell; - execute(cline, ccell); - }; - - static void update_python_dict_var(const char *name, int value); - static void update_python_dict_var_vector(const char *name, - std::vector &data); - void check_python_globals(); - void exec_python_simple_command(const std::string code); - static std::string transfer_python_ints_utility(); - static std::string transfer_python_lists_utility(); - static bool python_check_for_initialisation(); - - private: - - static void startup(); - void execute(std::string &line, std::string &cell); - }; - -} // namespace xcpp -#endif From 7bcbbb84d3561f6f74400b293ce231ffe02aae2a Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 06:01:11 +0300 Subject: [PATCH 11/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index cff52626..372a93cd 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -72,7 +72,7 @@ void* createInterpreter(const Args &ExtraArgs = {}) { // #include "xmagics/executable.hpp" // #include "xmagics/execution.hpp" #include "xmagics/os.hpp" -#include "xmagics/pythonexec.hpp" +//#include "xmagics/pythonexec.hpp" #include "xparser.hpp" #include "xsystem.hpp" From 089d4a5a0878fd466ae2d5c45a5548fdb746b1c0 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 06:10:35 +0300 Subject: [PATCH 12/50] Update pytest.ini --- test/pytest.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pytest.ini b/test/pytest.ini index 649aa953..5aacf83b 100644 --- a/test/pytest.ini +++ b/test/pytest.ini @@ -1,3 +1,3 @@ [pytest] testpaths = test -addopts = --nbval --nbval-current-env +addopts = --nbval --current-env From 6dc91c5d76e8af3e938506f728f8cfb8be6316af Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 06:29:44 +0300 Subject: [PATCH 13/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 41 ----------------------------------------- 1 file changed, 41 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 372a93cd..0e3af74b 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -56,47 +56,6 @@ void* createInterpreter(const Args &ExtraArgs = {}) { return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/); } -/* -#include - -#include - -#include "xeus-cpp/xbuffer.hpp" -#include "xeus-cpp/xeus_cpp_config.hpp" - -#include "xeus-cpp/xinterpreter.hpp" -#include "xeus-cpp/xmagics.hpp" - -#include "xinput.hpp" -#include "xinspect.hpp" -// #include "xmagics/executable.hpp" -// #include "xmagics/execution.hpp" -#include "xmagics/os.hpp" -//#include "xmagics/pythonexec.hpp" -#include "xparser.hpp" -#include "xsystem.hpp" - -using Args = std::vector; - -void* createInterpreter(const Args &ExtraArgs = {}) { - Args ClangArgs = {/*"-xc++"*/"-v"}; // ? {"-Xclang", "-emit-llvm-only", "-Xclang", "-diagnostic-log-file", "-Xclang", "-", "-xc++"}; - std::string resource_dir = Cpp::DetectResourceDir(); - ClangArgs.push_back("-resource-dir"); - ClangArgs.push_back(resource_dir.c_str()); - std::vector CxxSystemIncludes; - Cpp::DetectSystemCompilerIncludePaths(CxxSystemIncludes); - for (const std::string& CxxInclude : CxxSystemIncludes) { - ClangArgs.push_back("-isystem"); - ClangArgs.push_back(CxxInclude.c_str()); - } - ClangArgs.push_back("-I/home/apenev/.conda/envs/xeus-cpp/include/"); - ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end()); - // FIXME: We should process the kernel input options and conditionally pass - // the gpu args here. - return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/); -} -*/ - using namespace std::placeholders; namespace xcpp From 733a9791ac37f38b597b8d9993b9fd4151326a49 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 06:55:13 +0300 Subject: [PATCH 14/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 0e3af74b..ab11610d 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -63,6 +63,16 @@ namespace xcpp void interpreter::configure_impl() { xeus::register_interpreter(this); +/* + // todo: why is error_stream necessary + std::string error_message; + llvm::raw_string_ostream error_stream(error_message); + // Expose xinterpreter instance to interpreted C++ + process_code(*m_interpreter, "#include \"xeus/xinterpreter.hpp\"", error_stream); + std::string code = "xeus::register_interpreter(static_cast((void*)" + + std::to_string(intptr_t(this)) + "));"; + process_code(*m_interpreter, code.c_str(), error_stream); +*/ } static std::string get_stdopt() @@ -112,16 +122,6 @@ __get_cxx_version () interpreter::interpreter(int argc, const char* const* argv) : -/* - // todo: why is error_stream necessary - std::string error_message; - llvm::raw_string_ostream error_stream(error_message); - // Expose xinterpreter instance to interpreted C++ - process_code(*m_interpreter, "#include \"xeus/xinterpreter.hpp\"", error_stream); - std::string code = "xeus::register_interpreter(static_cast((void*)" - + std::to_string(intptr_t(this)) + "));"; - process_code(*m_interpreter, code.c_str(), error_stream); -*/ xmagics() , p_cout_strbuf(nullptr) , p_cerr_strbuf(nullptr) @@ -185,8 +185,6 @@ __get_cxx_version () // Scope guard performing the temporary redirection of input requests. auto input_guard = input_redirection(allow_stdin); - //std::string evalue; - //bool compilation_result = false; std::string err; std::string out; @@ -194,8 +192,6 @@ __get_cxx_version () // Attempt normal evaluation try { - compilation_result = Cpp::Process(code.c_str()); - Cpp::BeginStdStreamCapture(Cpp::kStdErr); Cpp::BeginStdStreamCapture(Cpp::kStdOut); compilation_result = Cpp::Process(code.c_str()); @@ -219,12 +215,12 @@ __get_cxx_version () { errorlevel = 1; ename = "Error :"; - //evalue = error_stream.str(); + evalue = error_stream.str(); //? std::cerr << err; } - //error_stream.str().clear(); - //DiagnosticsOS.str().clear(); + //error_stream.str().clear(); //? + //DiagnosticsOS.str().clear(); //? // Flush streams std::cout << std::flush; From b0678a3a1d2b411b49097f681e68dfd990279cc0 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 07:03:22 +0300 Subject: [PATCH 15/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index ab11610d..cc6f8b5d 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -63,16 +63,6 @@ namespace xcpp void interpreter::configure_impl() { xeus::register_interpreter(this); -/* - // todo: why is error_stream necessary - std::string error_message; - llvm::raw_string_ostream error_stream(error_message); - // Expose xinterpreter instance to interpreted C++ - process_code(*m_interpreter, "#include \"xeus/xinterpreter.hpp\"", error_stream); - std::string code = "xeus::register_interpreter(static_cast((void*)" - + std::to_string(intptr_t(this)) + "));"; - process_code(*m_interpreter, code.c_str(), error_stream); -*/ } static std::string get_stdopt() @@ -215,7 +205,7 @@ __get_cxx_version () { errorlevel = 1; ename = "Error :"; - evalue = error_stream.str(); //? + //evalue = error_stream.str(); //? std::cerr << err; } From 86f53dcfe5f958626b84a61d9b1f9c48c6841963 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 07:22:20 +0300 Subject: [PATCH 16/50] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 405ab8ff..6e1a69ec 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -74,7 +74,7 @@ jobs: shell: bash -l {0} run: | cd build/test - export CPLUS_INCLUDE_PATH=$CONDA_PREFIX/include:$CONDA_BUILD_SYSROOT:$CONDA_BUILD_SYSROOT/..:$CONDA_BUILD_SYSROOT/../x86_64-conda-linux-gnu/include/c++/12.3.0:$CONDA_BUILD_SYSROOT/../x86_64-conda-linux-gnu/include/c++/12.3.0/include:$CONDA_BUILD_SYSROOT/../include:$CONDA_BUILD_SYSROOT/usr/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/include-fixed:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/../../../../x86_64-conda-linux-gnu/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../x86_64-conda-linux-gnu/sysroot/usr/include + #export CPLUS_INCLUDE_PATH=$CONDA_PREFIX/include:$CONDA_BUILD_SYSROOT:$CONDA_BUILD_SYSROOT/..:$CONDA_BUILD_SYSROOT/../x86_64-conda-linux-gnu/include/c++/12.3.0:$CONDA_BUILD_SYSROOT/../x86_64-conda-linux-gnu/include/c++/12.3.0/include:$CONDA_BUILD_SYSROOT/../include:$CONDA_BUILD_SYSROOT/usr/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/include-fixed:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/../../../../x86_64-conda-linux-gnu/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../x86_64-conda-linux-gnu/sysroot/usr/include ./test_xeus_cpp timeout-minutes: 4 From 57c468ffcedf0d68bd48a313a4917aabf926cb45 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 07:36:40 +0300 Subject: [PATCH 17/50] Update main.yml --- .github/workflows/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6e1a69ec..a897a3b3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -74,7 +74,6 @@ jobs: shell: bash -l {0} run: | cd build/test - #export CPLUS_INCLUDE_PATH=$CONDA_PREFIX/include:$CONDA_BUILD_SYSROOT:$CONDA_BUILD_SYSROOT/..:$CONDA_BUILD_SYSROOT/../x86_64-conda-linux-gnu/include/c++/12.3.0:$CONDA_BUILD_SYSROOT/../x86_64-conda-linux-gnu/include/c++/12.3.0/include:$CONDA_BUILD_SYSROOT/../include:$CONDA_BUILD_SYSROOT/usr/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/include-fixed:/home/runner/micromamba-root/envs/xeus-cpp/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/../../../../x86_64-conda-linux-gnu/include:/home/runner/micromamba-root/envs/xeus-cpp/bin/../x86_64-conda-linux-gnu/sysroot/usr/include ./test_xeus_cpp timeout-minutes: 4 From f2f99e84bd8c71328f584d999aad5c03aa2b9e0c Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 07:43:15 +0300 Subject: [PATCH 18/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index cc6f8b5d..a6bd9fa2 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -92,24 +92,6 @@ __get_cxx_version () long cxx_version = Cpp::Evaluate(code); return std::to_string(cxx_version); } -/* - std::string interpreter::get_stdopt(int argc, const char* const* argv) - { - std::string res = "17"; //TODO: Detect from CppInterOp - for (int i = 0; i < argc; ++i) - { - std::string tmp(argv[i]); - auto pos = tmp.find("-std=c++"); - if (pos != std::string::npos) - { - res = tmp.substr(pos + 8); - break; - } - } - return res; - } -*/ - interpreter::interpreter(int argc, const char* const* argv) : xmagics() From d9deba6d3546212066de7a58cfbce6594a9839c8 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 07:47:16 +0300 Subject: [PATCH 19/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index a6bd9fa2..17889fbe 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -93,6 +93,7 @@ __get_cxx_version () return std::to_string(cxx_version); } + interpreter::interpreter(int argc, const char* const* argv) : xmagics() , p_cout_strbuf(nullptr) @@ -187,6 +188,7 @@ __get_cxx_version () { errorlevel = 1; ename = "Error :"; + evalue = "Compilation error!"; //evalue = error_stream.str(); //? std::cerr << err; } From f1e5a1e50e44e422f2ab0db131f82a17e7ed72a5 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 08:18:44 +0300 Subject: [PATCH 20/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 17889fbe..6559f30f 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -109,7 +109,6 @@ __get_cxx_version () init_includes(); init_preamble(); init_magic(); - Cpp::Process("#include "); } interpreter::~interpreter() From 568cd821dec593af096c9c878c19c8058d736974 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 08:20:47 +0300 Subject: [PATCH 21/50] Update main.yml --- .github/workflows/main.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a897a3b3..bd70fba7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -53,10 +53,10 @@ jobs: mkdir -p build cd build cmake .. \ - -DCMAKE_PREFIX_PATH=$CONDA_PREFIX \ - -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \ - -DXEUS_CPP_ENABLE_CODE_COVERAGE=${{ matrix.coverage }} \ - ${{ matrix.extra_cmake_flags }} + -DCMAKE_PREFIX_PATH=$CONDA_PREFIX \ + -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \ + -DXEUS_CPP_ENABLE_CODE_COVERAGE=${{ matrix.coverage }} \ + ${{ matrix.extra_cmake_flags }} - name: build shell: bash -l {0} @@ -110,9 +110,9 @@ jobs: # When debugging increase to a suitable value! timeout-minutes: 30 win: - + runs-on: ${{ matrix.os }} - + strategy: fail-fast: false matrix: From 9dfcb52cef90341d8d96be047b81b71571a7e2c7 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Sun, 14 Apr 2024 05:24:21 +0000 Subject: [PATCH 22/50] Restore original --- .github/workflows/main.yml | 12 ++++++------ test/test_xcpp_kernel.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bd70fba7..93f71d38 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,9 +15,9 @@ concurrency: jobs: unix: - + runs-on: ${{ matrix.os }} - + strategy: fail-fast: false matrix: @@ -110,9 +110,9 @@ jobs: # When debugging increase to a suitable value! timeout-minutes: 30 win: - + runs-on: ${{ matrix.os }} - + strategy: fail-fast: false matrix: @@ -169,7 +169,7 @@ jobs: - name: Setup emsdk shell: bash -l {0} run: | - emsdk install ${{matrix.emsdk_ver}} + emsdk install ${{matrix.emsdk_ver}} - name: Build xeus-cpp shell: bash -l {0} @@ -177,7 +177,7 @@ jobs: emsdk activate ${{matrix.emsdk_ver}} source $CONDA_EMSDK_DIR/emsdk_env.sh micromamba create -f environment-wasm-host.yml --platform=emscripten-wasm32 - + mkdir build pushd build diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 23b41fdd..c1ac33a3 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -24,7 +24,7 @@ class XCppTests(jupyter_kernel_test.KernelTests): code_stderr = '#include \nstd::cerr << "oops" << std::endl;' # Pager: code that should display something (anything) in the pager - code_page_something = "?std::vector" + #code_page_something = "?std::vector" # Samples of code which generate a result value (ie, some text # displayed as Out[n]) From 576347b190a25a6aba72e86558fc28a5a6b618d4 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Sun, 14 Apr 2024 05:25:39 +0000 Subject: [PATCH 23/50] Restore original 1 --- CMakeLists.txt | 53 +++++++++----------------------------------------- 1 file changed, 9 insertions(+), 44 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fa8e079..7e88a228 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,12 +23,6 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}") set(XEUS_CPP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) -enable_language(CXX) -set(CMAKE_CXX_EXTENSIONS NO) -set(CMAKE_POSITION_INDEPENDENT_CODE ON) - -include(GNUInstallDirs) - # Versionning # =========== @@ -50,8 +44,8 @@ option(XEUS_CPP_BUILD_STATIC "Build xeus-cpp static library" ON) OPTION(XEUS_CPP_BUILD_SHARED "Split xcpp build into executable and library" ON) OPTION(XEUS_CPP_BUILD_EXECUTABLE "Build the xcpp executable" ON) -OPTION(XEUS_CPP_USE_SHARED_XEUS "Link xcpp with the xeus shared library (instead of the static library)" ON) -OPTION(XEUS_CPP_USE_SHARED_XEUS_CPP "Link xcpp with the xeus shared library (instead of the static library)" ON) +OPTION(XEUS_CPP_USE_SHARED_XEUS "Link xcpp with the xeus shared library (instead of the static library)" ON) +OPTION(XEUS_CPP_USE_SHARED_XEUS_CPP "Link xcpp with the xeus shared library (instead of the static library)" ON) # Test options OPTION(XEUS_CPP_BUILD_TESTS "xeus-cpp test suite" ON) @@ -80,6 +74,7 @@ if(EMSCRIPTEN) SET(XEUS_CPP_BUILD_TESTS OFF) endif() + # Dependencies # ============ @@ -105,9 +100,7 @@ else() add_compile_options(-fexceptions) endif () -if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR - CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR - CMAKE_CXX_COMPILER_ID MATCHES "Intel") +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel") if(NOT EMSCRIPTEN) add_compile_options(-Wunused-parameter -Wextra -Wreorder) endif() @@ -116,7 +109,6 @@ endif () if(EMSCRIPTEN) # ENV (https://github.com/emscripten-core/emscripten/commit/6d9681ad04f60b41ef6345ab06c29bbc9eeb84e0) set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[ENV']\"") - set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") endif() find_package(CppInterOp REQUIRED) @@ -192,12 +184,6 @@ set(XEUS_CPP_SRC src/xoptions.cpp src/xparser.cpp src/xutils.cpp - src/xdemangle.hpp - src/xinspect.hpp - src/xsystem.hpp - src/xparser.hpp - src/xmagics/os.hpp - src/xmagics/os.cpp ) if(EMSCRIPTEN) @@ -363,27 +349,6 @@ if (XEUS_CPP_BUILD_EXECUTABLE) xeus_cpp_set_common_options(xcpp) xeus_cpp_set_kernel_options(xcpp) target_link_libraries(xcpp PRIVATE xeus-zmq) - set_target_properties(xcpp PROPERTIES - ENABLE_EXPORTS 1 - CXX_STANDARD ${CMAKE_CXX_STANDARD}) - target_link_libraries(xcpp PUBLIC xeus-cpp pthread) - - ##TODO: We may be need sse RPATH - set_target_properties(xcpp clangCppInterOp PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE) - if(APPLE) - target_link_libraries(xcpp PUBLIC -Wl,-w -Wl,-bind_at_load -Wl,-undefined,dynamic_lookup) - elseif(NOT MSVC) - target_link_libraries(xcpp PUBLIC -Wl,--unresolved-symbols=ignore-in-object-files) - endif() - - set_target_properties(xeus-cpp PROPERTIES - PUBLIC_HEADER "${XEUS_CPP_HEADERS}" - COMPILE_DEFINITIONS "XEUS_CPP_EXPORTS" - PREFIX "" - VERSION ${${PROJECT_NAME}_VERSION} - SOVERSION ${XEUS_CPP_VERSION_MAJOR} - OUTPUT_NAME "libxeus-cpp" - CXX_STANDARD ${CMAKE_CXX_STANDARD}) endif() if(EMSCRIPTEN) @@ -413,10 +378,10 @@ set(XEUS_CPP_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NA if (XEUS_CPP_BUILD_SHARED) install(TARGETS ${XEUS_CPP_TARGETS} EXPORT ${PROJECT_NAME}-targets - ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp) + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp) # Makes the project importable from the build directory export(EXPORT ${PROJECT_NAME}-targets @@ -426,7 +391,7 @@ endif () # Install xcpp if (XEUS_CPP_BUILD_EXECUTABLE) install(TARGETS xcpp - RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}) + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() if(XEUS_CPP_BUILD_EXECUTABLE OR EMSCRIPTEN) From c6b13cca5383d930d47770e2c6c7fef3c490dffd Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Sun, 14 Apr 2024 06:18:22 +0000 Subject: [PATCH 24/50] Restore original 2 --- src/xinterpreter.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 6559f30f..6c512697 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -1,6 +1,5 @@ /************************************************************************************ * Copyright (c) 2023, xeus-cpp contributors * - * Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht * * Copyright (c) 2023, Martin Vassilev * * * * Distributed under the terms of the BSD 3-Clause License. * @@ -105,7 +104,6 @@ __get_cxx_version () createInterpreter(Args(argv ? argv + 1 : argv, argv + argc)); m_version = get_stdopt(); redirect_output(); - // Bootstrap the execution engine init_includes(); init_preamble(); init_magic(); @@ -188,13 +186,9 @@ __get_cxx_version () errorlevel = 1; ename = "Error :"; evalue = "Compilation error!"; - //evalue = error_stream.str(); //? std::cerr << err; } - //error_stream.str().clear(); //? - //DiagnosticsOS.str().clear(); //? - // Flush streams std::cout << std::flush; std::cerr << std::flush; From 7cdd306b288a816021a13c1db8ea1b9283ca5f6d Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 15:40:50 +0300 Subject: [PATCH 25/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 6c512697..eef05d13 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -154,7 +154,7 @@ __get_cxx_version () } // Scope guard performing the temporary redirection of input requests. - auto input_guard = input_redirection(allow_stdin); + //auto input_guard = input_redirection(allow_stdin); std::string err; std::string out; From 829a84471ec2ce616c9d022177340cb02d69f13b Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Sun, 14 Apr 2024 20:11:47 +0300 Subject: [PATCH 26/50] Update environment.yml --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index 82d7eb2a..ca242be2 100644 --- a/environment.yml +++ b/environment.yml @@ -2,4 +2,4 @@ name: xeus-cpp channels: - conda-forge dependencies: - - xeus-cpp=0.0.1 + - xeus-cpp From 22af61d4696bd1f0b377b40661720ecb3dbcc443 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Mon, 15 Apr 2024 11:06:30 +0000 Subject: [PATCH 27/50] Fix error messages formatting --- src/xinterpreter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index eef05d13..c7c16a74 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -172,20 +172,20 @@ __get_cxx_version () catch (std::exception& e) { errorlevel = 1; - ename = "Standard Exception :"; + ename = "Standard Exception: "; evalue = e.what(); } catch (...) { errorlevel = 1; - ename = "Error :"; + ename = "Error: "; } if (compilation_result) { errorlevel = 1; - ename = "Error :"; - evalue = "Compilation error!"; + ename = "Error: "; + evalue = "Compilation error! " + err; std::cerr << err; } From b2ea844e8750a4ab9ae9bd95ee996b7ecddbc3f4 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Mon, 15 Apr 2024 13:27:58 +0000 Subject: [PATCH 28/50] Add more tests --- test/test_xcpp_kernel.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index c1ac33a3..f9cd1fbb 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -18,13 +18,16 @@ class XCppTests(jupyter_kernel_test.KernelTests): language_name = 'C++' # Code that should write the exact string `hello, world` to STDOUT - code_hello_world = '#include \nstd::cout << "hello, world" << std::endl;' + code_hello_world = '#include \n#include \nstd::cout << "hello, world" << std::endl;\nprintf("Hello from printf%s", "!");' # Code that should cause (any) text to be written to STDERR code_stderr = '#include \nstd::cerr << "oops" << std::endl;' # Pager: code that should display something (anything) in the pager - #code_page_something = "?std::vector" + code_page_something = "?std::vector" + + # Exception throwing + code_generate_error = 'throw std::runtime_error("Unknown exception");' # Samples of code which generate a result value (ie, some text # displayed as Out[n]) @@ -41,6 +44,31 @@ class XCppTests(jupyter_kernel_test.KernelTests): { 'code': '#include \n#include "xcpp/xdisplay.hpp"\nstd::string test("foobar");\nxcpp::display(test);', 'mime': 'text/plain' + }, + { + 'code': """ +#include +#include +#include "nlohmann/json.hpp" +#include "xtl/xbase64.hpp" +namespace im { + struct image { + inline image(const std::string& filename) { + std::ifstream fin(filename, std::ios::binary); + m_buffer << fin.rdbuf(); + } + std::stringstream m_buffer; + }; + nlohmann::json mime_bundle_repr(const image& i) { + auto bundle = nlohmann::json::object(); + bundle["image/png"] = xtl::base64encode(i.m_buffer.str()); + return bundle; + } +} +#include "xcpp/xdisplay.hpp" +im::image marie("../notebooks/images/marie.png"); +xcpp::display(marie);""", + 'mime': 'image/png' } ] From 14f6d6d1bb0aac6fe7a05eeb5fdf2334d230ce67 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Mon, 15 Apr 2024 13:37:46 +0000 Subject: [PATCH 29/50] Add more tests --- test/test_xcpp_kernel.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index f9cd1fbb..0d787659 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -18,7 +18,7 @@ class XCppTests(jupyter_kernel_test.KernelTests): language_name = 'C++' # Code that should write the exact string `hello, world` to STDOUT - code_hello_world = '#include \n#include \nstd::cout << "hello, world" << std::endl;\nprintf("Hello from printf%s", "!");' + code_hello_world = '#include \nstd::cout << "hello, world" << std::endl;' # Code that should cause (any) text to be written to STDERR code_stderr = '#include \nstd::cerr << "oops" << std::endl;' @@ -72,5 +72,17 @@ class XCppTests(jupyter_kernel_test.KernelTests): } ] + +class XCppTests2(jupyter_kernel_test.KernelTests): + + kernel_name = 'xcpp' + + # language_info.name in a kernel_info_reply should match this + language_name = 'C++' + + # Code that should write the exact string `hello, world` to STDOUT + code_hello_world = '#include \nprintf("hello, world");' + + if __name__ == '__main__': unittest.main() From 90a980c06e71191743d10e4d0f500e7a439a84cd Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Mon, 15 Apr 2024 14:09:42 +0000 Subject: [PATCH 30/50] Disable some tests --- test/test_xcpp_kernel.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 0d787659..6ceeadcc 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -73,15 +73,15 @@ class XCppTests(jupyter_kernel_test.KernelTests): ] -class XCppTests2(jupyter_kernel_test.KernelTests): - - kernel_name = 'xcpp' - - # language_info.name in a kernel_info_reply should match this - language_name = 'C++' - - # Code that should write the exact string `hello, world` to STDOUT - code_hello_world = '#include \nprintf("hello, world");' +#class XCppTests2(jupyter_kernel_test.KernelTests): +# +# kernel_name = 'xcpp' +# +# # language_info.name in a kernel_info_reply should match this +# language_name = 'C++' +# +# # Code that should write the exact string `hello, world` to STDOUT +# code_hello_world = '#include \nprintf("hello, world");' if __name__ == '__main__': From 770871cc42660e33c15087e7f07f3996c0fd267a Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Mon, 15 Apr 2024 14:22:52 +0000 Subject: [PATCH 31/50] Debug 1 --- .github/workflows/main.yml | 2 +- test/test_xcpp_kernel.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 93f71d38..f6ff4c55 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -81,7 +81,7 @@ jobs: shell: bash -l {0} run: | cd test - pytest -sv . --reruns 5 + pytest -svvv . --reruns 5 - name: Prepare code coverage report if: ${{ success() && (matrix.coverage == true) }} diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 6ceeadcc..0d787659 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -73,15 +73,15 @@ class XCppTests(jupyter_kernel_test.KernelTests): ] -#class XCppTests2(jupyter_kernel_test.KernelTests): -# -# kernel_name = 'xcpp' -# -# # language_info.name in a kernel_info_reply should match this -# language_name = 'C++' -# -# # Code that should write the exact string `hello, world` to STDOUT -# code_hello_world = '#include \nprintf("hello, world");' +class XCppTests2(jupyter_kernel_test.KernelTests): + + kernel_name = 'xcpp' + + # language_info.name in a kernel_info_reply should match this + language_name = 'C++' + + # Code that should write the exact string `hello, world` to STDOUT + code_hello_world = '#include \nprintf("hello, world");' if __name__ == '__main__': From 67f3b776e84215f82f2ead76b0281885bbfde2f0 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Mon, 15 Apr 2024 15:47:50 +0000 Subject: [PATCH 32/50] Debug 2 --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f6ff4c55..840bcc9c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -81,7 +81,7 @@ jobs: shell: bash -l {0} run: | cd test - pytest -svvv . --reruns 5 + pytest -sv . --reruns 10 - name: Prepare code coverage report if: ${{ success() && (matrix.coverage == true) }} From 0492fe95584220a2cb442a42af36c9641076566c Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Mon, 15 Apr 2024 17:24:19 +0000 Subject: [PATCH 33/50] Debug 3 --- test/test_xcpp_kernel.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 0d787659..ddcd4e55 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -18,7 +18,8 @@ class XCppTests(jupyter_kernel_test.KernelTests): language_name = 'C++' # Code that should write the exact string `hello, world` to STDOUT - code_hello_world = '#include \nstd::cout << "hello, world" << std::endl;' + #code_hello_world = '#include \nstd::cout << "hello, world" << std::endl;' + code_hello_world = '#include \nprintf("hello, world");' # Code that should cause (any) text to be written to STDERR code_stderr = '#include \nstd::cerr << "oops" << std::endl;' From f2b14321eb73b4c5b30d8e22789bdffc1cc2ca9c Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Mon, 15 Apr 2024 18:25:34 +0000 Subject: [PATCH 34/50] Debug 4 --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 840bcc9c..62d2be3c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -81,7 +81,7 @@ jobs: shell: bash -l {0} run: | cd test - pytest -sv . --reruns 10 + pytest -sv . --reruns 5 - name: Prepare code coverage report if: ${{ success() && (matrix.coverage == true) }} From 26f2929ace2c65e4dd5467e73771d3a10920a18e Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Tue, 16 Apr 2024 03:58:58 +0000 Subject: [PATCH 35/50] Debug 5 --- test/test_xcpp_kernel.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index ddcd4e55..368223de 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -7,19 +7,26 @@ ############################################################################# import unittest -import jupyter_kernel_test +import jupyter_kernel_test as jkt +from jupyter_client.kernelspec import NoSuchKernel -class XCppTests(jupyter_kernel_test.KernelTests): +class XCppTests(jkt.KernelTests): kernel_name = 'xcpp' + @classmethod + def setUpClass(cls): + try: + cls.km, cls.kc = jkt.start_new_kernel(kernel_name=cls.kernel_name) + except NoSuchKernel: + raise unittest.SkipTest("Xeus-cpp Kernel not installed") from None + # language_info.name in a kernel_info_reply should match this language_name = 'C++' # Code that should write the exact string `hello, world` to STDOUT - #code_hello_world = '#include \nstd::cout << "hello, world" << std::endl;' - code_hello_world = '#include \nprintf("hello, world");' + code_hello_world = '#include \nstd::cout << "hello, world" << std::endl;' # Code that should cause (any) text to be written to STDERR code_stderr = '#include \nstd::cerr << "oops" << std::endl;' @@ -74,9 +81,16 @@ class XCppTests(jupyter_kernel_test.KernelTests): ] -class XCppTests2(jupyter_kernel_test.KernelTests): +class XCppTests2(jkt.KernelTests): - kernel_name = 'xcpp' + kernel_name = 'xcpp17' + + @classmethod + def setUpClass(cls): + try: + cls.km, cls.kc = jkt.start_new_kernel(kernel_name=cls.kernel_name) + except NoSuchKernel: + raise unittest.SkipTest("Xeus-cpp Kernel not installed") from None # language_info.name in a kernel_info_reply should match this language_name = 'C++' From 7cb53814a1ecff6ff7bea0a25156cf04ce6cae47 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Tue, 16 Apr 2024 07:28:28 +0000 Subject: [PATCH 36/50] Debug 6 --- CMakeLists.txt | 3 +++ test/test_xcpp_kernel.py | 19 ++----------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e88a228..dfea61e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,7 +126,10 @@ if (NOT DEFINED XEUS_CPP_KERNELSPEC_PATH) set(XEUS_CPP_KERNELSPEC_PATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/") endif () +message("!!!$"${CppInterOp_CLANG_VERSION}) + function(configure_kernel kernel) +message("!!!!!!$"${CppInterOp_CLANG_VERSION}) set(XEUS_CPP_PATH "$ENV{PATH}") set(XEUS_CPP_LD_LIBRARY_PATH "$ENV{LD_LIBRARY_PATH}") set(XEUS_CPP_RESOURCE_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/clang/${CppInterOp_CLANG_VERSION}) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 368223de..2a1e1b41 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -7,21 +7,13 @@ ############################################################################# import unittest -import jupyter_kernel_test as jkt -from jupyter_client.kernelspec import NoSuchKernel +import jupyter_kernel_test class XCppTests(jkt.KernelTests): kernel_name = 'xcpp' - @classmethod - def setUpClass(cls): - try: - cls.km, cls.kc = jkt.start_new_kernel(kernel_name=cls.kernel_name) - except NoSuchKernel: - raise unittest.SkipTest("Xeus-cpp Kernel not installed") from None - # language_info.name in a kernel_info_reply should match this language_name = 'C++' @@ -83,14 +75,7 @@ def setUpClass(cls): class XCppTests2(jkt.KernelTests): - kernel_name = 'xcpp17' - - @classmethod - def setUpClass(cls): - try: - cls.km, cls.kc = jkt.start_new_kernel(kernel_name=cls.kernel_name) - except NoSuchKernel: - raise unittest.SkipTest("Xeus-cpp Kernel not installed") from None + kernel_name = 'xcpp' # language_info.name in a kernel_info_reply should match this language_name = 'C++' From f719d9c41e62e73b1d194b9b503363dbd3d890b7 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Tue, 16 Apr 2024 07:41:54 +0000 Subject: [PATCH 37/50] Debug 7 --- CMakeLists.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dfea61e6..a6ddee14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,6 +113,7 @@ endif() find_package(CppInterOp REQUIRED) if(CppInterOp_FOUND) + set(CPPINTEROP_LLVM_VERSION_MAJOR "17") message(STATUS "Found CppInterOp: config=${CPPINTEROP_CONFIG} dir=${CPPINTEROP_DIR} (found version=${CPPINTEROP_VERSION} compatible with Clang ${CPPINTEROP_LLVM_VERSION_MAJOR}.x)") endif() @@ -126,13 +127,13 @@ if (NOT DEFINED XEUS_CPP_KERNELSPEC_PATH) set(XEUS_CPP_KERNELSPEC_PATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/") endif () -message("!!!$"${CppInterOp_CLANG_VERSION}) +message("!!!$"${CPPINTEROP_LLVM_VERSION_MAJOR}) function(configure_kernel kernel) -message("!!!!!!$"${CppInterOp_CLANG_VERSION}) +message("!!!!!!$"${CPPINTEROP_LLVM_VERSION_MAJOR}) set(XEUS_CPP_PATH "$ENV{PATH}") set(XEUS_CPP_LD_LIBRARY_PATH "$ENV{LD_LIBRARY_PATH}") - set(XEUS_CPP_RESOURCE_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/clang/${CppInterOp_CLANG_VERSION}) + set(XEUS_CPP_RESOURCE_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/clang/${CPPINTEROP_LLVM_VERSION_MAJOR}) configure_file ( "${CMAKE_CURRENT_SOURCE_DIR}/${kernel}/kernel.json.in" From 721f778a8145b7ca374418ce2175bbe5043a2cc3 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Tue, 16 Apr 2024 07:54:38 +0000 Subject: [PATCH 38/50] Debug 8 --- test/test_xcpp_kernel.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 2a1e1b41..0d787659 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -10,7 +10,7 @@ import jupyter_kernel_test -class XCppTests(jkt.KernelTests): +class XCppTests(jupyter_kernel_test.KernelTests): kernel_name = 'xcpp' @@ -73,7 +73,7 @@ class XCppTests(jkt.KernelTests): ] -class XCppTests2(jkt.KernelTests): +class XCppTests2(jupyter_kernel_test.KernelTests): kernel_name = 'xcpp' From 085efd2f0a950393f98e6558f0a2b982c2be219c Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Tue, 16 Apr 2024 11:30:07 +0000 Subject: [PATCH 39/50] Debug 8 --- test/test_xcpp_kernel.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 0d787659..0c270bb7 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -8,7 +8,7 @@ import unittest import jupyter_kernel_test - +import platform class XCppTests(jupyter_kernel_test.KernelTests): @@ -27,7 +27,9 @@ class XCppTests(jupyter_kernel_test.KernelTests): code_page_something = "?std::vector" # Exception throwing - code_generate_error = 'throw std::runtime_error("Unknown exception");' + # TODO: Remove 'if' when test work on MacOS/arm64. Throw Exceptions make + # kernel/test non-workable. + code_generate_error = 'throw std::runtime_error("Unknown exception");' if platform.system() != "Darwin" or platform.processor() != 'arm' else '' # Samples of code which generate a result value (ie, some text # displayed as Out[n]) From 4a4444d4ab548d442a6a50a4c37945f6319b1f18 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Tue, 16 Apr 2024 12:24:32 +0000 Subject: [PATCH 40/50] Cleanup --- CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a6ddee14..afcd6fcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,7 +113,6 @@ endif() find_package(CppInterOp REQUIRED) if(CppInterOp_FOUND) - set(CPPINTEROP_LLVM_VERSION_MAJOR "17") message(STATUS "Found CppInterOp: config=${CPPINTEROP_CONFIG} dir=${CPPINTEROP_DIR} (found version=${CPPINTEROP_VERSION} compatible with Clang ${CPPINTEROP_LLVM_VERSION_MAJOR}.x)") endif() @@ -127,10 +126,7 @@ if (NOT DEFINED XEUS_CPP_KERNELSPEC_PATH) set(XEUS_CPP_KERNELSPEC_PATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/") endif () -message("!!!$"${CPPINTEROP_LLVM_VERSION_MAJOR}) - function(configure_kernel kernel) -message("!!!!!!$"${CPPINTEROP_LLVM_VERSION_MAJOR}) set(XEUS_CPP_PATH "$ENV{PATH}") set(XEUS_CPP_LD_LIBRARY_PATH "$ENV{LD_LIBRARY_PATH}") set(XEUS_CPP_RESOURCE_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/clang/${CPPINTEROP_LLVM_VERSION_MAJOR}) From d68f4cd15f0519ce202e368be4adbd4d0b0dd727 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Tue, 16 Apr 2024 15:34:59 +0300 Subject: [PATCH 41/50] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 62d2be3c..93f71d38 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -81,7 +81,7 @@ jobs: shell: bash -l {0} run: | cd test - pytest -sv . --reruns 5 + pytest -sv . --reruns 5 - name: Prepare code coverage report if: ${{ success() && (matrix.coverage == true) }} From c8a02b871af25d34f00924cbd3c60c7ed118e2b5 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Tue, 16 Apr 2024 15:36:26 +0300 Subject: [PATCH 42/50] Update test_xcpp_kernel.py --- test/test_xcpp_kernel.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 0c270bb7..0018df08 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -10,6 +10,7 @@ import jupyter_kernel_test import platform + class XCppTests(jupyter_kernel_test.KernelTests): kernel_name = 'xcpp' From be05220487b8254a2d99b4c89ee655d32713cb1c Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Tue, 16 Apr 2024 22:28:29 +0300 Subject: [PATCH 43/50] Update test_xcpp_kernel.py --- test/test_xcpp_kernel.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 0018df08..19622e6a 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -30,7 +30,8 @@ class XCppTests(jupyter_kernel_test.KernelTests): # Exception throwing # TODO: Remove 'if' when test work on MacOS/arm64. Throw Exceptions make # kernel/test non-workable. - code_generate_error = 'throw std::runtime_error("Unknown exception");' if platform.system() != "Darwin" or platform.processor() != 'arm' else '' + ###code_generate_error = 'throw std::runtime_error("Unknown exception");' if platform.system() != "Darwin" or platform.processor() != 'arm' else '' + code_generate_error = 'throw std::runtime_error("Unknown exception");' # Samples of code which generate a result value (ie, some text # displayed as Out[n]) From 388ed385d02bd1550a6c765123f36483836ebf39 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Tue, 16 Apr 2024 22:30:42 +0300 Subject: [PATCH 44/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index c7c16a74..d779f80b 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -59,6 +59,20 @@ using namespace std::placeholders; namespace xcpp { + struct StreamRedirectRAII { + std::string *err = NULL; + StreamRedirectRAII(std::string *e) { + err = e; + Cpp::BeginStdStreamCapture(Cpp::kStdErr); + Cpp::BeginStdStreamCapture(Cpp::kStdOut); + } + ~StreamRedirectRAII() { + std::string out = Cpp::EndStdStreamCapture(); + *err = Cpp::EndStdStreamCapture(); + std::cout << out; + } + }; + void interpreter::configure_impl() { xeus::register_interpreter(this); @@ -157,17 +171,12 @@ __get_cxx_version () //auto input_guard = input_redirection(allow_stdin); std::string err; - std::string out; // Attempt normal evaluation try { - Cpp::BeginStdStreamCapture(Cpp::kStdErr); - Cpp::BeginStdStreamCapture(Cpp::kStdOut); + StreamRedirectRAII R(&err); compilation_result = Cpp::Process(code.c_str()); - out = Cpp::EndStdStreamCapture(); - err = Cpp::EndStdStreamCapture(); - std::cout << out; } catch (std::exception& e) { From 71ec37a9c7e0309383317ac2955658af41bc217a Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Tue, 16 Apr 2024 22:33:49 +0300 Subject: [PATCH 45/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index d779f80b..056c2bfe 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -167,9 +167,6 @@ __get_cxx_version () std::cerr.rdbuf(&null); } - // Scope guard performing the temporary redirection of input requests. - //auto input_guard = input_redirection(allow_stdin); - std::string err; // Attempt normal evaluation From 71931e6bd66a66d95be8ace3c63b91e21d758f45 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Tue, 16 Apr 2024 20:10:45 +0000 Subject: [PATCH 46/50] Clean --- test/test_xcpp_kernel.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 19622e6a..13cf552e 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -31,7 +31,6 @@ class XCppTests(jupyter_kernel_test.KernelTests): # TODO: Remove 'if' when test work on MacOS/arm64. Throw Exceptions make # kernel/test non-workable. ###code_generate_error = 'throw std::runtime_error("Unknown exception");' if platform.system() != "Darwin" or platform.processor() != 'arm' else '' - code_generate_error = 'throw std::runtime_error("Unknown exception");' # Samples of code which generate a result value (ie, some text # displayed as Out[n]) From dec330149fc84d3cb7fbf94db2d22634ad70e240 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Tue, 16 Apr 2024 23:13:35 +0300 Subject: [PATCH 47/50] Update src/xinterpreter.cpp Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/xinterpreter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 056c2bfe..dffa13ca 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -60,7 +60,7 @@ using namespace std::placeholders; namespace xcpp { struct StreamRedirectRAII { - std::string *err = NULL; + std::string *err = nullptr; StreamRedirectRAII(std::string *e) { err = e; Cpp::BeginStdStreamCapture(Cpp::kStdErr); From c01b67fb1a37a39c1195257a2644a5fd69593f60 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Tue, 16 Apr 2024 23:15:47 +0300 Subject: [PATCH 48/50] Apply suggestions from code review Co-authored-by: Vassil Vassilev --- src/xinterpreter.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index dffa13ca..231d6795 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -61,8 +61,7 @@ namespace xcpp { struct StreamRedirectRAII { std::string *err = nullptr; - StreamRedirectRAII(std::string *e) { - err = e; + StreamRedirectRAII(std::string &e) : err(e) { Cpp::BeginStdStreamCapture(Cpp::kStdErr); Cpp::BeginStdStreamCapture(Cpp::kStdOut); } From 7a8cf17dfd104d815c1c8e5c78deb7c7d77453b6 Mon Sep 17 00:00:00 2001 From: Alexander Penev <7923188+alexander-penev@users.noreply.github.com> Date: Tue, 16 Apr 2024 23:18:09 +0300 Subject: [PATCH 49/50] Update xinterpreter.cpp --- src/xinterpreter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 231d6795..1282341d 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -60,14 +60,14 @@ using namespace std::placeholders; namespace xcpp { struct StreamRedirectRAII { - std::string *err = nullptr; + std::string &err; StreamRedirectRAII(std::string &e) : err(e) { Cpp::BeginStdStreamCapture(Cpp::kStdErr); Cpp::BeginStdStreamCapture(Cpp::kStdOut); } ~StreamRedirectRAII() { std::string out = Cpp::EndStdStreamCapture(); - *err = Cpp::EndStdStreamCapture(); + err = Cpp::EndStdStreamCapture(); std::cout << out; } }; From 28367af62bb1749453ffcc54a61b74ad72d6f6eb Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Tue, 16 Apr 2024 20:28:26 +0000 Subject: [PATCH 50/50] Fix --- src/xinterpreter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 1282341d..fdef663f 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -171,7 +171,7 @@ __get_cxx_version () // Attempt normal evaluation try { - StreamRedirectRAII R(&err); + StreamRedirectRAII R(err); compilation_result = Cpp::Process(code.c_str()); } catch (std::exception& e)