diff --git a/tests/slic3rutils/python_test_support.hpp b/tests/slic3rutils/python_test_support.hpp index 347812f0aa..9f02489527 100644 --- a/tests/slic3rutils/python_test_support.hpp +++ b/tests/slic3rutils/python_test_support.hpp @@ -8,9 +8,11 @@ // the app likewise finalizes it only at exit, so nothing here tears it down. // slic3rutils_tests_main.cpp finalizes the production interpreter once, after the last test case. // -// This bootstrap is separate from PythonInterpreter's runtime: whichever of the two brings Python -// up first in a process owns it, and the other cannot initialize it again. ctest runs one case per -// process, so that only matters if the binary is run directly. +// Starts PythonInterpreter, the same runtime PluginManager::initialize() uses, or a bare +// interpreter when there is no bundled runtime. +// +// PythonInterpreter releases the GIL once it is up, so callers take py::gil_scoped_acquire after +// ensure_python_initialized() and before touching any Python object. #include #include #include @@ -21,8 +23,13 @@ #include #include +#include #include +#include + +#include "test_utils.hpp" + namespace { void ensure_python_initialized() @@ -30,6 +37,18 @@ void ensure_python_initialized() if (Py_IsInitialized()) return; + { + // Static, because the interpreter writes {data_dir}/python and {data_dir}/log until the + // process exits. + static ScopedTemporaryDir python_data_dir{"orca-plugin-python"}; + const std::string previous = Slic3r::data_dir(); + Slic3r::set_data_dir(python_data_dir.path().string()); + const bool initialized = Slic3r::PythonInterpreter::instance().initialize(); + Slic3r::set_data_dir(previous); + if (initialized || Py_IsInitialized()) + return; + } + static std::unique_ptr interpreter; PyConfig config; diff --git a/tests/slic3rutils/test_plugin_audit.cpp b/tests/slic3rutils/test_plugin_audit.cpp index 478e9676fe..074bfa07f0 100644 --- a/tests/slic3rutils/test_plugin_audit.cpp +++ b/tests/slic3rutils/test_plugin_audit.cpp @@ -1577,6 +1577,38 @@ std::vector with_shell_grants(std::vector grants, return grants; } +const fs::path& audit_data_parent() +{ + static const ScopedTemporaryDir parent{"orca-plugin-audit-hook"}; + return parent.path(); +} + +// A fresh data_dir() under audit_data_parent() for one AuditHarness. +struct AuditDataDir +{ + fs::path dir = audit_data_parent() / fs::unique_path("%%%%-%%%%-%%%%"); + std::string previous = data_dir(); + + AuditDataDir() + { + fs::create_directories(dir); + set_data_dir(dir.string()); + } + + ~AuditDataDir() + { + set_data_dir(previous); + boost::system::error_code ec; + fs::remove_all(dir, ec); + } + + AuditDataDir(const AuditDataDir&) = delete; + AuditDataDir& operator=(const AuditDataDir&) = delete; + + // The plugin manager scans {data_dir}/orca_plugins. + fs::path plugins_dir() const { return dir / "orca_plugins"; } +}; + // The plugin system with the audit hook installed, and the permission dialog replaced by a // recorder that answers with `approve`. Declare it first in a test, so it is destroyed last. // @@ -1584,10 +1616,10 @@ std::vector with_shell_grants(std::vector grants, // PythonInterpreterRunEnd in slic3rutils_tests_main.cpp. struct AuditHarness { + bool python_was_running = PythonInterpreter::instance().is_initialized(); // Before initialize(): the interpreter creates {data_dir}/python/packages and {data_dir}/log. - // data_dir() is a global allowed root from then on -- the first test to initialize Python in - // this process (under ctest, this one) -- so the test files live in `outside`. - ScopedDataDir data{"plugin-audit-hook"}; + // The test files live in `outside`, away from every writable root. + AuditDataDir data; bool initialized = PluginManager::instance().initialize(); ScopedTemporaryDir outside{"orca-plugin-audit-outside"}; std::vector prompts; @@ -1597,6 +1629,13 @@ struct AuditHarness AuditHarness() { PluginAuditManager& audit = PluginAuditManager::instance(); + // install_hook() grants only the data_dir() current when Python started, so when another + // test case started it, grant the parent of every harness data dir instead. + static bool parent_granted = false; // global roots are never removed + if (python_was_running && !parent_granted) { + audit.add_global_allowed_root(audit_data_parent()); + parent_granted = true; + } audit.clear_current_plugin(); audit.clear_current_capability(); audit.clear_audit_denial(); diff --git a/tests/slic3rutils/test_plugin_host_api.cpp b/tests/slic3rutils/test_plugin_host_api.cpp index 40b882a577..9b4ddabed2 100644 --- a/tests/slic3rutils/test_plugin_host_api.cpp +++ b/tests/slic3rutils/test_plugin_host_api.cpp @@ -35,6 +35,7 @@ #include #include #include "plugin_test_utils.hpp" +#include #include #include #include "python_test_support.hpp" @@ -77,6 +78,8 @@ bool has_attr(const py::handle& object, const char* name) TEST_CASE("Plugin host API exposes host-owned bundle and preset surface to Python", "[PluginHost][Python]") { + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::module_ orca = import_orca_module(); REQUIRE(has_attr(orca, "host")); @@ -171,6 +174,8 @@ TEST_CASE("Plugin host API exposes host-owned bundle and preset surface to Pytho TEST_CASE("Plugin host API exposes the editable preset category handles and guards them before Orca app initialization", "[PluginHost][Python]") { + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::object host = import_orca_module().attr("host"); py::object preset = host.attr("preset"); @@ -204,6 +209,8 @@ TEST_CASE("Plugin host API exposes the editable preset category handles and guar TEST_CASE("Plugin host API exposes the filament slot form of preset update and guards it", "[PluginHost][Python]") { + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::object host = import_orca_module().attr("host"); py::object preset = host.attr("preset"); @@ -234,6 +241,8 @@ TEST_CASE("Plugin host API exposes the filament slot form of preset update and g TEST_CASE("Plugin host API exposes staged preset update transactions", "[PluginHost][Python]") { + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::object preset = import_orca_module().attr("host").attr("preset"); py::object transaction_type = preset.attr("PresetTransaction"); py::object result_type = preset.attr("PresetUpdateResult"); @@ -307,6 +316,8 @@ TEST_CASE("A settings group resolves an option's id from its option map", "[Plug TEST_CASE("Plugin host API reports unavailable GUI objects before Orca app initialization", "[PluginHost][Python]") { + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::object host = import_orca_module().attr("host"); py::object bundles = host.attr("preset").attr("bundles"); @@ -340,6 +351,8 @@ TEST_CASE("Plugin host API reports unavailable GUI objects before Orca app initi TEST_CASE("Plugin host API reports no GL details before the 3D view exists", "[PluginHost][Python]") { + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::object host = import_orca_module().attr("host"); CHECK(host.attr("gl_info")().is_none()); @@ -351,6 +364,8 @@ TEST_CASE("Plugin host API reports no GL details before the 3D view exists", "[P TEST_CASE("Plugin host API exposes the UI module and guards it before Orca app initialization", "[PluginHost][Python]") { + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::object host = import_orca_module().attr("host"); REQUIRE(has_attr(host, "ui")); @@ -460,6 +475,8 @@ TEST_CASE("Plugin host API exposes model geometry and structure to Python", "[Pl using Catch::Matchers::WithinAbs; using Catch::Matchers::WithinRel; + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::object host = import_orca_module().attr("host"); REQUIRE(has_attr(host, "BoundingBox")); REQUIRE(has_attr(host, "Model")); @@ -539,6 +556,8 @@ TEST_CASE("Plugin host API exposes TriangleMesh geometry to Python", "[PluginHos using Catch::Matchers::WithinAbs; using Catch::Matchers::WithinRel; + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::object host = import_orca_module().attr("host"); REQUIRE(has_attr(host, "TriangleMesh")); diff --git a/tests/slic3rutils/test_plugin_lifecycle.cpp b/tests/slic3rutils/test_plugin_lifecycle.cpp index 730ead53b9..03f8f2ac5f 100644 --- a/tests/slic3rutils/test_plugin_lifecycle.cpp +++ b/tests/slic3rutils/test_plugin_lifecycle.cpp @@ -153,18 +153,14 @@ class LifecycleTestPlugin(orca.base): )PY"; // In production GUI_App::init_plugin_gui_wiring subscribes the agent-registry -// callbacks. The test binary has no GUI, so install the same unload-side wiring -// once so these tests exercise the production deregister-on-unload path. +// callbacks. The test binary has no GUI, so it installs the same unload-side wiring +// to exercise the production deregister-on-unload path. +// PluginManager::shutdown() drops every subscription, so each test installs it again. // // The load-side (register) wiring is deliberately not installed: duplicate-id // coverage registers agents manually in a deterministic order instead. void install_agent_registry_wiring() { - static bool installed = false; - if (installed) - return; - installed = true; - PluginManager& manager = PluginManager::instance(); manager.subscribe_on_unload_callback(NetworkAgentFactory::deregister_python_plugin); manager.subscribe_on_capability_unload_callback([](const PluginCapabilityId& capability) { diff --git a/tests/slic3rutils/test_plugin_resource_texture.cpp b/tests/slic3rutils/test_plugin_resource_texture.cpp index 951cd4b591..4d643335b8 100644 --- a/tests/slic3rutils/test_plugin_resource_texture.cpp +++ b/tests/slic3rutils/test_plugin_resource_texture.cpp @@ -86,13 +86,9 @@ struct ScopedPluginManager // The app installs these in GUI_App::init_plugin_gui_wiring. Without an app the callbacks run inline // on the load worker, which is what lets the tests observe the registry right after the wait. +// PluginManager::shutdown() drops every subscription, so each test installs these again. void install_resource_texture_wiring() { - static bool installed = false; - if (installed) - return; - installed = true; - PluginManager& manager = PluginManager::instance(); manager.subscribe_on_load_callback([](const std::string& plugin_key) { PluginResourceTextures::instance().on_plugin_register(plugin_key); @@ -261,12 +257,10 @@ std::vector make_gray_bmp(int width, int height, uint8_t value) // wxImageModule, which registers the standard handlers. wxInitAllImageHandlers() adds PNG, JPEG and // the rest but NOT the BMP handler, so a test binary without a wxApp must add the standard set too or // a .bmp texture cannot be loaded here at all. +// Wx cleanup in another test removes every handler, and adding one that is already registered +// does nothing. void ensure_wx_image_handlers() { - static bool initialized = false; - if (initialized) - return; - initialized = true; wxImage::InitStandardHandlers(); wxInitAllImageHandlers(); } diff --git a/tests/slic3rutils/test_precise_seam_plugin.cpp b/tests/slic3rutils/test_precise_seam_plugin.cpp index b471c95f7b..92ddbdaccb 100644 --- a/tests/slic3rutils/test_precise_seam_plugin.cpp +++ b/tests/slic3rutils/test_precise_seam_plugin.cpp @@ -39,7 +39,6 @@ TEST_CASE("Python exposes every seam mode and distinguishes strong weak and ordi {"SupportBlocker", ModelVolumeType::SUPPORT_BLOCKER, false, false} }}; - // Bootstrap holds the GIL on first initialization; acquire explicitly for an already-running interpreter too. ensure_python_initialized(); py::gil_scoped_acquire gil; py::object host = import_orca_module().attr("host"); diff --git a/tests/slic3rutils/test_printer_agent.cpp b/tests/slic3rutils/test_printer_agent.cpp index ac4140284c..5a9415b9f4 100644 --- a/tests/slic3rutils/test_printer_agent.cpp +++ b/tests/slic3rutils/test_printer_agent.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include "python_test_support.hpp" @@ -63,14 +64,11 @@ TEST_CASE("unit: printer-agent registry register / lookup / duplicate-reject", " // every printer-agent plugin subclasses. If a binding is renamed or removed, // plugins fail at runtime even though C++ still compiles. // =========================================================================== -// ensure_python_initialized()/import_orca_module() come from python_test_support.hpp: -// a bare scoped_interpreter with no PyConfig.home falls back to the build-time -// stdlib path, which doesn't exist beside this test binary, so Py_Initialize() -// fails to load the codecs needed for the filesystem encoding. The shared helper -// points PyConfig.home at the python/ runtime staged next to the test executable. TEST_CASE("integration: orca.printer_agent binding surface", "[integration][Python]") { + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::module_ orca = import_orca_module(); REQUIRE(py::hasattr(orca, "printer_agent")); @@ -122,6 +120,8 @@ TEST_CASE("integration: orca.printer_agent binding surface", "[integration][Pyth // =========================================================================== TEST_CASE("integration: orca plugin-registration API surface + discovery-context guards", "[integration][Python]") { + ensure_python_initialized(); + py::gil_scoped_acquire gil; py::module_ orca = import_orca_module(); // Module-level surface every plugin package relies on. diff --git a/tests/slic3rutils/test_slicing_pipeline_bindings.cpp b/tests/slic3rutils/test_slicing_pipeline_bindings.cpp index d05589b552..199b546705 100644 --- a/tests/slic3rutils/test_slicing_pipeline_bindings.cpp +++ b/tests/slic3rutils/test_slicing_pipeline_bindings.cpp @@ -91,8 +91,8 @@ TEST_CASE("make_writable_rows builds a writable (N,2) int64 view that aliases th TEST_CASE("orca.slicing module: Step enum, context, and a Python capability can execute", "[slicing_pipeline]") { ensure_python_initialized(); - import_orca_module(); // forces PythonPluginBridge::instance() (see import_orca_module in python_test_support.hpp) py::gil_scoped_acquire gil; + import_orca_module(); // links in PythonPluginBridge, which registers the orca module py::module_ orca = py::module_::import("orca"); REQUIRE(py::hasattr(orca, "slicing")); py::object slicing = orca.attr("slicing"); @@ -116,8 +116,8 @@ _probe = Probe() TEST_CASE("orca.slicing is workflow-only: context exposes raw print/object; view classes are gone", "[slicing_pipeline]") { using Catch::Matchers::WithinRel; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::module_ orca = py::module_::import("orca"); py::object slicing = orca.attr("slicing"); @@ -157,8 +157,8 @@ TEST_CASE("orca.slicing is workflow-only: context exposes raw print/object; view // (not the C++ audit trampoline) keeps this a pure binding-surface test. TEST_CASE("orca.slicing psGCodePostProcess context: file edit in place + config fallback", "[slicing_pipeline]") { ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); ScopedTemporaryFile gpath(".gcode"); { @@ -260,8 +260,8 @@ TEST_CASE("orca.host leaf geometry: Surface/ExPolygon/Polygon raw bindings", "[s using Catch::Matchers::WithinRel; using Catch::Matchers::WithinAbs; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::object host = py::module_::import("orca").attr("host"); for (const char* name : { "SurfaceType", "Polygon", "ExPolygon", "Surface", "SurfaceCollection" }) @@ -296,8 +296,8 @@ TEST_CASE("orca.host leaf geometry: Surface/ExPolygon/Polygon raw bindings", "[s TEST_CASE("orca.host Surface/SurfaceCollection: construct, writable members, set()", "[slicing_pipeline]") { using Catch::Matchers::WithinRel; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::object host = py::module_::import("orca").attr("host"); py::object ST = host.attr("SurfaceType"); const coord_t s = (coord_t) scale_(10.0); @@ -330,8 +330,8 @@ TEST_CASE("orca.host Surface/SurfaceCollection: construct, writable members, set TEST_CASE("orca.host Point: construct, read/write coords, arithmetic", "[slicing_pipeline]") { ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::object host = py::module_::import("orca").attr("host"); REQUIRE(py::hasattr(host, "Point")); py::object p = host.attr("Point")(3, 4); @@ -353,8 +353,8 @@ TEST_CASE("orca.host Point: construct, read/write coords, arithmetic", "[slicing TEST_CASE("orca.host Polygon: writable as_array aliases buffer; Point refs; set_points; offset", "[slicing_pipeline]") { using Catch::Matchers::WithinRel; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::object host = py::module_::import("orca").attr("host"); const coord_t s = (coord_t) scale_(10.0); @@ -402,8 +402,8 @@ TEST_CASE("orca.host Polygon: writable as_array aliases buffer; Point refs; set_ TEST_CASE("orca.host ExPolygon: construct, writable contour/holes, transforms, boolean ops", "[slicing_pipeline]") { using Catch::Matchers::WithinRel; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::object host = py::module_::import("orca").attr("host"); const coord_t s = (coord_t) scale_(10.0); @@ -451,8 +451,8 @@ static Slic3r::ExtrusionEntityCollection build_nested_collection() { TEST_CASE("orca.host extrusion tree: polymorphic entities + flatten_paths", "[slicing_pipeline]") { using Catch::Matchers::WithinRel; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::object host = py::module_::import("orca").attr("host"); for (const char* name : { "ExtrusionEntity", "ExtrusionPath", "ExtrusionLoop", "ExtrusionMultiPath", "ExtrusionEntityCollection", "PrintRegion" }) @@ -480,8 +480,8 @@ TEST_CASE("orca.host extrusion tree: polymorphic entities + flatten_paths", "[sl TEST_CASE("orca.host ExtrusionPath.points() is a read-only (N,3) int64 view", "[slicing_pipeline]") { ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); bool have_numpy = false; try { py::module_::import("numpy"); have_numpy = true; } catch (const py::error_already_set&) { have_numpy = false; } @@ -517,8 +517,8 @@ struct TestLayer : Slic3r::Layer { TEST_CASE("orca.host graph classes: LayerRegion/Layer raw traversal; Print/PrintObject registered", "[slicing_pipeline]") { using Catch::Matchers::WithinRel; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::object host = py::module_::import("orca").attr("host"); for (const char* name : { "LayerRegion", "Layer", "PrintObject", "Print" }) @@ -558,8 +558,8 @@ TEST_CASE("orca.host graph classes: LayerRegion/Layer raw traversal; Print/Print TEST_CASE("orca.host: plugin-only mutators are gone; class-API editing works", "[slicing_pipeline]") { ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::object host = py::module_::import("orca").attr("host"); // The three plugin-only mutators were removed in the raw-API realignment. @@ -581,8 +581,8 @@ TEST_CASE("orca.host: plugin-only mutators are gone; class-API editing works", " TEST_CASE("orca.host: SurfaceCollection.set mutates geometry; lslices via make_slices", "[slicing_pipeline]") { using Catch::Matchers::WithinRel; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); bool have_numpy = false; try { py::module_::import("numpy"); have_numpy = true; } catch (const py::error_already_set&) { have_numpy = false; } @@ -626,8 +626,8 @@ TEST_CASE("orca.host: SurfaceCollection.set mutates geometry; lslices via make_s TEST_CASE("orca.host ExPolygon in-place transforms + SurfaceCollection.append (sample ops)", "[slicing_pipeline]") { using Catch::Matchers::WithinRel; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); py::object host = py::module_::import("orca").attr("host"); const coord_t s = (coord_t) scale_(10.0); auto make_square = [&]() { @@ -671,8 +671,8 @@ TEST_CASE("orca.host ExPolygon in-place transforms + SurfaceCollection.append (s TEST_CASE("orca.host: in-place edit of surface.expolygon through a live collection persists to C++", "[slicing_pipeline]") { using Catch::Matchers::WithinRel; ensure_python_initialized(); - import_orca_module(); py::gil_scoped_acquire gil; + import_orca_module(); const coord_t s = (coord_t) scale_(10.0); // Live LayerRegion holding one surface (a 10mm square at the origin). diff --git a/tests/slic3rutils/test_timelapse_codecs.cpp b/tests/slic3rutils/test_timelapse_codecs.cpp index ede43d8f06..1674b79668 100644 --- a/tests/slic3rutils/test_timelapse_codecs.cpp +++ b/tests/slic3rutils/test_timelapse_codecs.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,9 @@ TEST_CASE("JPEG texture decoding coexists with thumbnails and video frames", "[T if (!std::getenv("DISPLAY") && !std::getenv("WAYLAND_DISPLAY")) SKIP("This integration test needs a display (for example, xvfb-run)."); #endif + // Without an app object, wx creates the GUI_App this binary links, whose destructor shuts down + // plugins and Python for every later test case. + wxApp::SetInstance(new wxApp); wxInitializer init; REQUIRE(init.IsOk()); wxInitAllImageHandlers();