-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] supporting cppyy #248
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,8 @@ dependencies: | |
- CppInterOp>=1.5.0 | ||
- cpp-argparse | ||
- pugixml | ||
- python=3.11 | ||
- bzip2 | ||
- zlib | ||
- libffi | ||
- sqlite |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/************************************************************************************ | ||
* Copyright (c) 2025, xeus-cpp contributors * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
************************************************************************************/ | ||
|
||
#include "pythonexec.hpp" | ||
#include "../xparser.hpp" | ||
#include <string> | ||
|
||
#include "Python.h" | ||
|
||
#include "clang/Interpreter/CppInterOp.h" | ||
|
||
namespace xcpp { | ||
bool pythonexec::is_initalized = false; | ||
|
||
void pythonexec::operator()([[maybe_unused]] const std::string& line, const std::string& cell) { | ||
|
||
if (!is_initalized) | ||
initialize(); | ||
if (!is_initalized) { | ||
// initializing failed | ||
std::cout << Cpp::EndStdStreamCapture(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "std::cout" is directly included [misc-include-cleaner] src/xmagics/pythonexec.cpp:10: - #include <string>
+ #include <iostream>
+ #include <string> |
||
std::cerr << Cpp::EndStdStreamCapture(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "std::cerr" is directly included [misc-include-cleaner] std::cerr << Cpp::EndStdStreamCapture();
^ |
||
|
||
std::cerr << "Failed to Initialize Python\n"; | ||
return; | ||
} | ||
|
||
std::string code = trim(cell); | ||
if (code.empty()) | ||
return; | ||
|
||
Cpp::BeginStdStreamCapture(Cpp::kStdErr); | ||
Cpp::BeginStdStreamCapture(Cpp::kStdOut); | ||
|
||
PyRun_SimpleString(code.c_str()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "PyRun_SimpleString" is directly included [misc-include-cleaner] src/xmagics/pythonexec.cpp:13: + #include "pythonrun.h" |
||
|
||
std::cout << Cpp::EndStdStreamCapture(); | ||
std::cerr << Cpp::EndStdStreamCapture(); | ||
} | ||
|
||
void pythonexec::initialize() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: method 'initialize' can be made static [readability-convert-member-functions-to-static] src/xmagics/pythonexec.hpp:24: - void initialize();
+ static void initialize(); |
||
#ifdef EMSCRIPTEN | ||
PyStatus status; | ||
|
||
PyConfig config; | ||
PyConfig_InitPythonConfig(&config); | ||
static const std::wstring prefix(L"/"); | ||
config.base_prefix = const_cast<wchar_t*>(prefix.c_str()); | ||
config.base_exec_prefix = const_cast<wchar_t*>(prefix.c_str()); | ||
config.prefix = const_cast<wchar_t*>(prefix.c_str()); | ||
config.exec_prefix = const_cast<wchar_t*>(prefix.c_str()); | ||
|
||
status = Py_InitializeFromConfig(&config); | ||
if (PyStatus_Exception(status)) { | ||
// TODO: initialization failed, propagate error | ||
PyConfig_Clear(&config); | ||
is_initalized = false; | ||
return; | ||
} | ||
PyConfig_Clear(&config); | ||
#else | ||
Py_Initialize(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "Py_Initialize" is directly included [misc-include-cleaner] src/xmagics/pythonexec.cpp:13: + #include "pylifecycle.h" |
||
#endif | ||
|
||
PyRun_SimpleString("import sys\nsys.path.append('')"); // add current directory to PYTHONPATH | ||
|
||
// // 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); | ||
|
||
is_initalized = true; | ||
} | ||
} // namespace xcpp |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,30 @@ | ||||||||
/************************************************************************************ | ||||||||
* Copyright (c) 2025, xeus-cpp contributors * | ||||||||
* * | ||||||||
* Distributed under the terms of the BSD 3-Clause License. * | ||||||||
* * | ||||||||
* The full license is in the file LICENSE, distributed with this software. * | ||||||||
************************************************************************************/ | ||||||||
|
||||||||
#ifndef XEUS_CPP_PYTHONEXEC_MAGIC_HPP | ||||||||
#define XEUS_CPP_PYTHONEXEC_MAGIC_HPP | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: header guard does not follow preferred style [llvm-header-guard]
Suggested change
src/xmagics/pythonexec.hpp:29: - #endif //XEUS_CPP_PYTHONEXEC_MAGIC_HPP
+ #endif // GITHUB_WORKSPACE_SRC_XMAGICS_PYTHONEXEC_HPP |
||||||||
|
||||||||
#include <string> | ||||||||
|
||||||||
#include "xeus-cpp/xmagics.hpp" | ||||||||
|
||||||||
namespace xcpp | ||||||||
{ | ||||||||
class pythonexec : public xmagic_cell | ||||||||
{ | ||||||||
public: | ||||||||
XEUS_CPP_API | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "XEUS_CPP_API" is directly included [misc-include-cleaner] src/xmagics/pythonexec.hpp:13: - #include "xeus-cpp/xmagics.hpp"
+ #include "xeus-cpp/xeus_cpp_config.hpp"
+ #include "xeus-cpp/xmagics.hpp" |
||||||||
void operator()(const std::string& line, const std::string& cell) override; | ||||||||
private: | ||||||||
static bool is_initalized; | ||||||||
void initialize(); | ||||||||
|
||||||||
}; | ||||||||
} // namespace xcpp | ||||||||
|
||||||||
#endif //XEUS_CPP_PYTHONEXEC_MAGIC_HPP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: included header Python.h is not used directly [misc-include-cleaner]