diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index eb74de6450..5e9606c619 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -697,6 +697,7 @@ set(SLIC3R_GUI_SOURCES plugin/CloudPluginService.hpp plugin/PluginFsUtils.cpp plugin/PluginFsUtils.hpp + plugin/PluginJsonPy.hpp plugin/PluginConfig.cpp plugin/PluginConfig.hpp plugin/PluginLoader.cpp diff --git a/src/slic3r/plugin/PluginFsUtils.hpp b/src/slic3r/plugin/PluginFsUtils.hpp index 302519a3e4..0bc333be9b 100644 --- a/src/slic3r/plugin/PluginFsUtils.hpp +++ b/src/slic3r/plugin/PluginFsUtils.hpp @@ -1,18 +1,12 @@ #pragma once #include "PluginDescriptor.hpp" -#include - -#include -#include #include -#include #include #include #include -#include #define PLUGIN_SUBSCRIBED_DIR "_subscribed" #define PLUGIN_DATA_DIR "plugin_data" @@ -21,81 +15,6 @@ namespace Slic3r { extern const char* const INSTALL_STATE_FILE; -// JSON <-> Python conversion shared by the plugin bindings. The caller must hold the GIL. -// Plugin config and orca.host.ui payloads both cross the boundary as plain JSON-compatible -// values, so both go through these. - -// Maximum nesting depth for JSON <-> Python conversion. A self-referential or pathologically -// deep value would otherwise recurse until the native C stack overflows, an uncatchable crash; -// past this bound we raise instead. 200 is far beyond any legitimate plugin config or UI payload. -inline constexpr int kMaxJsonConversionDepth = 200; - -inline pybind11::object json_to_py(const nlohmann::json& j, int depth = 0) -{ - namespace py = pybind11; - using json = nlohmann::json; - - if (depth > kMaxJsonConversionDepth) - throw py::value_error("Plugin JSON value nested too deeply"); - - switch (j.type()) { - case json::value_t::null: return py::none(); - case json::value_t::boolean: return py::bool_(j.get()); - case json::value_t::number_integer: return py::int_(j.get()); - case json::value_t::number_unsigned: return py::int_(j.get()); - case json::value_t::number_float: return py::float_(j.get()); - case json::value_t::string: return py::str(j.get()); - case json::value_t::array: { - py::list lst; - for (const auto& e : j) - lst.append(json_to_py(e, depth + 1)); - return lst; - } - case json::value_t::object: { - py::dict d; - for (auto it = j.begin(); it != j.end(); ++it) - d[py::str(it.key())] = json_to_py(it.value(), depth + 1); - return d; - } - default: return py::none(); - } -} - -inline nlohmann::json py_to_json(const pybind11::handle& o, int depth = 0) -{ - namespace py = pybind11; - using json = nlohmann::json; - - if (depth > kMaxJsonConversionDepth) - throw py::value_error("Plugin value nested too deeply (possible cycle)"); - - if (o.is_none()) - return json(nullptr); - if (py::isinstance(o)) // bool before int (bool subclasses int in Python) - return o.cast(); - if (py::isinstance(o)) - return o.cast(); - if (py::isinstance(o)) - return o.cast(); - if (py::isinstance(o)) - return o.cast(); - if (py::isinstance(o)) - return o.cast(); - if (py::isinstance(o)) { - json obj = json::object(); - for (auto item : py::reinterpret_borrow(o)) - obj[py::str(item.first).cast()] = py_to_json(item.second, depth + 1); - return obj; - } - if (py::isinstance(o) || py::isinstance(o)) { - json arr = json::array(); - for (auto e : o) - arr.push_back(py_to_json(e, depth + 1)); - return arr; - } - return py::str(o).cast(); // fallback: str() -} - struct PluginPermissions { // Filesystem targets. A target ending in "/*" (e.g. "/path/to/folder/*") grants the folder diff --git a/src/slic3r/plugin/PluginJsonPy.hpp b/src/slic3r/plugin/PluginJsonPy.hpp new file mode 100644 index 0000000000..cb3a60ddbf --- /dev/null +++ b/src/slic3r/plugin/PluginJsonPy.hpp @@ -0,0 +1,88 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include + +namespace Slic3r { + +// JSON <-> Python conversion shared by the plugin bindings. The caller must hold the GIL. +// Plugin config and orca.host.ui payloads both cross the boundary as plain JSON-compatible +// values, so both go through these. + +// Maximum nesting depth for JSON <-> Python conversion. A self-referential or pathologically +// deep value would otherwise recurse until the native C stack overflows, an uncatchable crash; +// past this bound we raise instead. 200 is far beyond any legitimate plugin config or UI payload. +inline constexpr int kMaxJsonConversionDepth = 200; + +inline pybind11::object json_to_py(const nlohmann::json& j, int depth = 0) +{ + namespace py = pybind11; + using json = nlohmann::json; + + if (depth > kMaxJsonConversionDepth) + throw py::value_error("Plugin JSON value nested too deeply"); + + switch (j.type()) { + case json::value_t::null: return py::none(); + case json::value_t::boolean: return py::bool_(j.get()); + case json::value_t::number_integer: return py::int_(j.get()); + case json::value_t::number_unsigned: return py::int_(j.get()); + case json::value_t::number_float: return py::float_(j.get()); + case json::value_t::string: return py::str(j.get()); + case json::value_t::array: { + py::list lst; + for (const auto& e : j) + lst.append(json_to_py(e, depth + 1)); + return lst; + } + case json::value_t::object: { + py::dict d; + for (auto it = j.begin(); it != j.end(); ++it) + d[py::str(it.key())] = json_to_py(it.value(), depth + 1); + return d; + } + default: return py::none(); + } +} + +inline nlohmann::json py_to_json(const pybind11::handle& o, int depth = 0) +{ + namespace py = pybind11; + using json = nlohmann::json; + + if (depth > kMaxJsonConversionDepth) + throw py::value_error("Plugin value nested too deeply (possible cycle)"); + + if (o.is_none()) + return json(nullptr); + if (py::isinstance(o)) // bool before int (bool subclasses int in Python) + return o.cast(); + if (py::isinstance(o)) + return o.cast(); + if (py::isinstance(o)) + return o.cast(); + if (py::isinstance(o)) + return o.cast(); + if (py::isinstance(o)) + return o.cast(); + if (py::isinstance(o)) { + json obj = json::object(); + for (auto item : py::reinterpret_borrow(o)) + obj[py::str(item.first).cast()] = py_to_json(item.second, depth + 1); + return obj; + } + if (py::isinstance(o) || py::isinstance(o)) { + json arr = json::array(); + for (auto e : o) + arr.push_back(py_to_json(e, depth + 1)); + return arr; + } + return py::str(o).cast(); // fallback: str() +} + +} // namespace Slic3r diff --git a/src/slic3r/plugin/PluginManager.hpp b/src/slic3r/plugin/PluginManager.hpp index aa7cafef84..bd9c80f1dc 100644 --- a/src/slic3r/plugin/PluginManager.hpp +++ b/src/slic3r/plugin/PluginManager.hpp @@ -15,7 +15,6 @@ #include #include #include -#include "pybind11/pytypes.h" #include "slic3r/plugin/PluginFsUtils.hpp" #include #include @@ -23,13 +22,15 @@ #include #include -#include - #include "CloudPluginService.hpp" #include "PluginDescriptor.hpp" #include "PluginLoader.hpp" #include "PluginConfig.hpp" +struct _object; +using PyObject = _object; +namespace pybind11 { class object; } + namespace Slic3r { class OrcaCloudServiceAgent; diff --git a/src/slic3r/plugin/PyPluginTrampoline.hpp b/src/slic3r/plugin/PyPluginTrampoline.hpp index 074a140735..5919ecc57d 100644 --- a/src/slic3r/plugin/PyPluginTrampoline.hpp +++ b/src/slic3r/plugin/PyPluginTrampoline.hpp @@ -15,6 +15,7 @@ #include "PythonPluginInterface.hpp" #include "PythonInterpreter.hpp" #include "PluginFsUtils.hpp" +#include "PluginJsonPy.hpp" #include "PluginAuditManager.hpp" #include "pybind11/cast.h" #include diff --git a/src/slic3r/plugin/PythonPluginInterface.hpp b/src/slic3r/plugin/PythonPluginInterface.hpp index c57c9ce6b6..6d31748e98 100644 --- a/src/slic3r/plugin/PythonPluginInterface.hpp +++ b/src/slic3r/plugin/PythonPluginInterface.hpp @@ -8,7 +8,6 @@ #include #include -#include #include diff --git a/src/slic3r/plugin/host/PluginHostUi.cpp b/src/slic3r/plugin/host/PluginHostUi.cpp index 9f2648619a..3ac050fe97 100644 --- a/src/slic3r/plugin/host/PluginHostUi.cpp +++ b/src/slic3r/plugin/host/PluginHostUi.cpp @@ -5,7 +5,7 @@ #include #include "slic3r/plugin/PluginAuditManager.hpp" #include "slic3r/plugin/PythonInterpreter.hpp" // PythonGILState -#include "slic3r/plugin/PluginFsUtils.hpp" // json_to_py / py_to_json +#include "slic3r/plugin/PluginJsonPy.hpp" #include #include diff --git a/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapability.cpp b/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapability.cpp index e6ab45246b..a9f47cb271 100644 --- a/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapability.cpp +++ b/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapability.cpp @@ -2,6 +2,7 @@ #include "PagesPluginCapabilityTrampoline.hpp" #include "../../PluginFsUtils.hpp" +#include "../../PluginJsonPy.hpp" #include #include diff --git a/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapability.hpp b/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapability.hpp index 978492006c..8baf1e7ddb 100644 --- a/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapability.hpp +++ b/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapability.hpp @@ -2,12 +2,13 @@ #define slic3r_PagesPluginCapability_hpp_ #include "../../PythonPluginInterface.hpp" -#include "pybind11/pybind11.h" #include #include #include +namespace pybind11 { class module_; } + namespace Slic3r { class PagesPluginCapability : public PluginCapabilityInterface { diff --git a/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapabilityTrampoline.hpp b/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapabilityTrampoline.hpp index 24b98bea24..f17c446a0f 100644 --- a/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapabilityTrampoline.hpp +++ b/src/slic3r/plugin/pluginTypes/pages/PagesPluginCapabilityTrampoline.hpp @@ -2,6 +2,7 @@ #include "PagesPluginCapability.hpp" #include "../../PluginFsUtils.hpp" +#include "../../PluginJsonPy.hpp" #include "../../PyPluginTrampoline.hpp" #include #include diff --git a/tests/slic3rutils/test_plugin_json_depth.cpp b/tests/slic3rutils/test_plugin_json_depth.cpp index 85f84d40e8..c06c1d49b2 100644 --- a/tests/slic3rutils/test_plugin_json_depth.cpp +++ b/tests/slic3rutils/test_plugin_json_depth.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "plugin_test_utils.hpp"