|
| 1 | +/************************************************************************************ |
| 2 | + * Copyright (c) 2025, xeus-cpp contributors * |
| 3 | + * * |
| 4 | + * Distributed under the terms of the BSD 3-Clause License. * |
| 5 | + * * |
| 6 | + * The full license is in the file LICENSE, distributed with this software. * |
| 7 | + ************************************************************************************/ |
| 8 | + |
| 9 | +#include "pythonexec.hpp" |
| 10 | +#include "../xparser.hpp" |
| 11 | +#include <string> |
| 12 | + |
| 13 | +#include "Python.h" |
| 14 | + |
| 15 | +#include "clang/Interpreter/CppInterOp.h" |
| 16 | + |
| 17 | +namespace xcpp { |
| 18 | + bool pythonexec::is_initalized = false; |
| 19 | + |
| 20 | + void pythonexec::operator()([[maybe_unused]] const std::string& line, const std::string& cell) { |
| 21 | + |
| 22 | + if (!is_initalized) |
| 23 | + initialize(); |
| 24 | + if (!is_initalized) { |
| 25 | + // initializing failed |
| 26 | + std::cout << Cpp::EndStdStreamCapture(); |
| 27 | + std::cerr << Cpp::EndStdStreamCapture(); |
| 28 | + |
| 29 | + std::cerr << "Failed to Initialize Python\n"; |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + std::string code = trim(cell); |
| 34 | + if (code.empty()) |
| 35 | + return; |
| 36 | + |
| 37 | + Cpp::BeginStdStreamCapture(Cpp::kStdErr); |
| 38 | + Cpp::BeginStdStreamCapture(Cpp::kStdOut); |
| 39 | + |
| 40 | + PyRun_SimpleString(code.c_str()); |
| 41 | + |
| 42 | + std::cout << Cpp::EndStdStreamCapture(); |
| 43 | + std::cerr << Cpp::EndStdStreamCapture(); |
| 44 | + } |
| 45 | + |
| 46 | + void pythonexec::initialize() { |
| 47 | +#ifdef EMSCRIPTEN |
| 48 | + PyStatus status; |
| 49 | + |
| 50 | + PyConfig config; |
| 51 | + PyConfig_InitPythonConfig(&config); |
| 52 | + static const std::wstring prefix(L"/"); |
| 53 | + config.base_prefix = const_cast<wchar_t*>(prefix.c_str()); |
| 54 | + config.base_exec_prefix = const_cast<wchar_t*>(prefix.c_str()); |
| 55 | + config.prefix = const_cast<wchar_t*>(prefix.c_str()); |
| 56 | + config.exec_prefix = const_cast<wchar_t*>(prefix.c_str()); |
| 57 | + |
| 58 | + status = Py_InitializeFromConfig(&config); |
| 59 | + if (PyStatus_Exception(status)) { |
| 60 | + // TODO: initialization failed, propagate error |
| 61 | + PyConfig_Clear(&config); |
| 62 | + is_initalized = false; |
| 63 | + return; |
| 64 | + } |
| 65 | + PyConfig_Clear(&config); |
| 66 | +#else |
| 67 | + Py_Initialize(); |
| 68 | +#endif |
| 69 | + |
| 70 | + PyRun_SimpleString("import sys\nsys.path.append('')"); // add current directory to PYTHONPATH |
| 71 | + |
| 72 | + // // Import cppyy module |
| 73 | + // PyObject* cppyyModule = PyImport_ImportModule("cppyy"); |
| 74 | + // if (!cppyyModule) { |
| 75 | + // PyErr_Print(); |
| 76 | + // Py_Finalize(); |
| 77 | + // return; // Handle import error as needed |
| 78 | + // } |
| 79 | + |
| 80 | + // PyObject* mainModule = PyImport_AddModule("__main__"); |
| 81 | + // PyObject_SetAttrString(mainModule, "cppyy", cppyyModule); |
| 82 | + // Py_XDECREF(cppyyModule); |
| 83 | + |
| 84 | + is_initalized = true; |
| 85 | + } |
| 86 | +} // namespace xcpp |
0 commit comments