diff --git a/.wpiformat b/.wpiformat index 74de41271e..d28f42f95f 100644 --- a/.wpiformat +++ b/.wpiformat @@ -13,7 +13,10 @@ generatedFileExclude { docs/theme\.css$ generated/ hal/src/main/native/systemcore/rev/ - python/ + + src/main/python/.*/type_casters/ + src/main/python/.*\.py$ + src/test/python/.*\.py$ robotpyExamples/ resources/ thirdparty/ @@ -41,4 +44,7 @@ licenseUpdateExclude { wpiutil/src/main/native/cpp/sha1\.cpp$ wpiutil/src/main/native/include/wpi/util/sha1\.hpp$ wpinet/src/main/native/linux/AvahiClient\.hpp$ + + src/main/python/ + src/test/python/ } diff --git a/apriltag/src/main/python/robotpy_apriltag/src/main.cpp b/apriltag/src/main/python/robotpy_apriltag/src/main.cpp index caba6f678a..f1d525bcb2 100644 --- a/apriltag/src/main/python/robotpy_apriltag/src/main.cpp +++ b/apriltag/src/main/python/robotpy_apriltag/src/main.cpp @@ -1,4 +1,6 @@ #include "semiwrap_init.robotpy_apriltag._apriltag.hpp" -SEMIWRAP_PYBIND11_MODULE(m) { initWrapper(m); } +SEMIWRAP_PYBIND11_MODULE(m) { + initWrapper(m); +} diff --git a/apriltag/src/main/python/semiwrap/AprilTagDetector.yml b/apriltag/src/main/python/semiwrap/AprilTagDetector.yml index c614eb3e47..95404802e9 100644 --- a/apriltag/src/main/python/semiwrap/AprilTagDetector.yml +++ b/apriltag/src/main/python/semiwrap/AprilTagDetector.yml @@ -32,7 +32,7 @@ classes: // We are going to move the detection result into this shared_ptr // so that python can keep it alive. We don't expose the result directly // to the user because we'd have to pretend it's a list, and that would - // be annoying. + // be annoying. std::shared_ptr c_result; { py::gil_scoped_release unlock; diff --git a/commandsv2/src/main/python/pyproject.toml b/commandsv2/src/main/python/pyproject.toml index 675d580606..ec6b88e6ff 100644 --- a/commandsv2/src/main/python/pyproject.toml +++ b/commandsv2/src/main/python/pyproject.toml @@ -33,4 +33,4 @@ source = "vcs" packages = ["commands2"] [tool.hatch.build.targets.wheel] -packages = ["commands2"] \ No newline at end of file +packages = ["commands2"] diff --git a/datalog/src/main/python/native-pyproject.toml b/datalog/src/main/python/native-pyproject.toml index a8b68f2c46..30c9406eec 100644 --- a/datalog/src/main/python/native-pyproject.toml +++ b/datalog/src/main/python/native-pyproject.toml @@ -38,4 +38,4 @@ name = "datalog" includedir = "src/native/datalog/include" libdir = "src/native/datalog/lib" shared_libraries = ["datalog"] -requires = ["robotpy-native-wpiutil"] \ No newline at end of file +requires = ["robotpy-native-wpiutil"] diff --git a/datalog/src/main/python/src/main.cpp b/datalog/src/main/python/src/main.cpp index 5b9f043aef..a3b2a3cfc5 100644 --- a/datalog/src/main/python/src/main.cpp +++ b/datalog/src/main/python/src/main.cpp @@ -1,6 +1,6 @@ -#include +#include "semiwrap_init.wpilog._wpilog.hpp" SEMIWRAP_PYBIND11_MODULE(m) { - initWrapper(m); + initWrapper(m); } diff --git a/hal/src/main/python/hal/simulation/main.cpp b/hal/src/main/python/hal/simulation/main.cpp index a43c783b5c..d0f89440c7 100644 --- a/hal/src/main/python/hal/simulation/main.cpp +++ b/hal/src/main/python/hal/simulation/main.cpp @@ -1,21 +1,21 @@ -#include #include +#include "semiwrap_init.hal.simulation._simulation.hpp" #include "sim_cb.h" #include "sim_value_cb.h" void HALSIM_ResetGlobalHandles(); SEMIWRAP_PYBIND11_MODULE(m) { - py::class_ cls_SimCB(m, "SimCB"); cls_SimCB.doc() = "Simulation callback handle"; cls_SimCB.def("cancel", &SimCB::Cancel, py::doc("Cancel the callback")); py::class_ cls_SimValueCB(m, "SimValueCB"); cls_SimValueCB.doc() = "Simulation callback handle"; - cls_SimValueCB.def("cancel", &SimValueCB::Cancel, py::doc("Cancel the callback")); + cls_SimValueCB.def("cancel", &SimValueCB::Cancel, + py::doc("Cancel the callback")); initWrapper(m); diff --git a/hal/src/main/python/hal/simulation/resethandles.cpp b/hal/src/main/python/hal/simulation/resethandles.cpp index 39f650a9fd..a95c1abbcb 100644 --- a/hal/src/main/python/hal/simulation/resethandles.cpp +++ b/hal/src/main/python/hal/simulation/resethandles.cpp @@ -4,12 +4,11 @@ #include "wpi/hal/simulation/NotifierData.h" void HALSIM_ResetGlobalHandles() { - // if we just reset the handles, notifiers might hang forever, // so we just enumerate and cancel them all auto sz = HALSIM_GetNotifierInfo(nullptr, 0); if (sz > 0) { - struct HALSIM_NotifierInfo *info = new struct HALSIM_NotifierInfo[sz]; + struct HALSIM_NotifierInfo* info = new struct HALSIM_NotifierInfo[sz]; HALSIM_GetNotifierInfo(info, sz); for (int i = 0; i < sz; i++) { diff --git a/hal/src/main/python/hal/simulation/sim_cb.h b/hal/src/main/python/hal/simulation/sim_cb.h index 8b9d48d048..0a27ca0fbe 100644 --- a/hal/src/main/python/hal/simulation/sim_cb.h +++ b/hal/src/main/python/hal/simulation/sim_cb.h @@ -1,33 +1,29 @@ #pragma once +#include +#include + class SimCB { -public: + public: + SimCB(std::function fn, std::function cancel) + : m_fn(fn), m_cancel(cancel) {} - SimCB(std::function fn, std::function cancel) : - m_fn(fn), - m_cancel(cancel) - {} + void SetUID(int32_t uid) { m_uid = uid; } - void SetUID(int32_t uid) { - m_uid = uid; + ~SimCB() { Cancel(); } + + void Cancel() { + if (m_valid) { + m_cancel(m_uid); + m_valid = false; } + } - ~SimCB() { - Cancel(); - } + std::function m_fn; - void Cancel() { - if (m_valid) { - m_cancel(m_uid); - m_valid = false; - } - } - - std::function m_fn; - -private: - bool m_valid = true; - int32_t m_uid; - std::function m_cancel; -}; \ No newline at end of file + private: + bool m_valid = true; + int32_t m_uid; + std::function m_cancel; +}; diff --git a/hal/src/main/python/hal/simulation/sim_value_cb.h b/hal/src/main/python/hal/simulation/sim_value_cb.h index ff128fc895..e9bd198941 100644 --- a/hal/src/main/python/hal/simulation/sim_value_cb.h +++ b/hal/src/main/python/hal/simulation/sim_value_cb.h @@ -1,37 +1,33 @@ #pragma once +#include + #include "wpi/hal/SimDevice.h" class SimValueCB { -public: + public: + using FnType = std::function; - using FnType = std::function; + SimValueCB(FnType fn, std::function cancel) + : m_fn(fn), m_cancel(cancel) {} - SimValueCB(FnType fn, std::function cancel) : - m_fn(fn), - m_cancel(cancel) - {} + void SetUID(int32_t uid) { m_uid = uid; } - void SetUID(int32_t uid) { - m_uid = uid; + ~SimValueCB() { Cancel(); } + + void Cancel() { + if (m_valid) { + m_cancel(m_uid); + m_valid = false; } + } - ~SimValueCB() { - Cancel(); - } + FnType m_fn; - void Cancel() { - if (m_valid) { - m_cancel(m_uid); - m_valid = false; - } - } - - FnType m_fn; - -private: - bool m_valid = true; - int32_t m_uid; - std::function m_cancel; -}; \ No newline at end of file + private: + bool m_valid = true; + int32_t m_uid; + std::function m_cancel; +}; diff --git a/hal/src/main/python/hal/src/ds_types_fmt.h b/hal/src/main/python/hal/src/ds_types_fmt.h index f9ccb0055e..fd14a23084 100644 --- a/hal/src/main/python/hal/src/ds_types_fmt.h +++ b/hal/src/main/python/hal/src/ds_types_fmt.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include "wpi/hal/DriverStationTypes.h" @@ -8,23 +10,23 @@ namespace pybind11 { template <> struct format_descriptor { - static constexpr const char c = 'B'; - static constexpr const char value[2] = {c, '\0'}; - static std::string format() { return std::string(1, c); } + static constexpr const char c = 'B'; + static constexpr const char value[2] = {c, '\0'}; + static std::string format() { return std::string(1, c); } }; template <> struct format_descriptor { - static constexpr const char c = 'B'; - static constexpr const char value[2] = {c, '\0'}; - static std::string format() { return std::string(1, c); } + static constexpr const char c = 'B'; + static constexpr const char value[2] = {c, '\0'}; + static std::string format() { return std::string(1, c); } }; template <> struct format_descriptor { - static constexpr const char c = 'B'; - static constexpr const char value[2] = {c, '\0'}; - static std::string format() { return std::string(1, c); } + static constexpr const char c = 'B'; + static constexpr const char value[2] = {c, '\0'}; + static std::string format() { return std::string(1, c); } }; -} +} // namespace pybind11 diff --git a/hal/src/main/python/hal/src/hal.cpp b/hal/src/main/python/hal/src/hal.cpp index 04a3ebcd8a..3f0fccd47d 100644 --- a/hal/src/main/python/hal/src/hal.cpp +++ b/hal/src/main/python/hal/src/hal.cpp @@ -1,25 +1,26 @@ #include "wpi/hal/HAL.h" + +#include "semiwrap_init.hal._wpiHal.hpp" #include "wpi/hal/DriverStation.hpp" #include "wpi/hal/Value.h" -#include using namespace pybind11::literals; static py::module_ sys_module; SEMIWRAP_PYBIND11_MODULE(m) { - // Add this manually so it can be used from SimValue py::enum_(m, "Type") - .value("UNASSIGNED", HAL_Type::HAL_UNASSIGNED) - .value("BOOLEAN", HAL_Type::HAL_BOOLEAN) - .value("DOUBLE", HAL_Type::HAL_DOUBLE) - .value("ENUM", HAL_Type::HAL_ENUM) - .value("INT", HAL_Type::HAL_INT) - .value("LONG", HAL_Type::HAL_LONG); + .value("UNASSIGNED", HAL_Type::HAL_UNASSIGNED) + .value("BOOLEAN", HAL_Type::HAL_BOOLEAN) + .value("DOUBLE", HAL_Type::HAL_DOUBLE) + .value("ENUM", HAL_Type::HAL_ENUM) + .value("INT", HAL_Type::HAL_INT) + .value("LONG", HAL_Type::HAL_LONG); + // clang-format off // Add this manually because it would be annoying to do otherwise py::class_(m, "Value") .def_readonly("type", &HAL_Value::type) @@ -57,6 +58,7 @@ SEMIWRAP_PYBIND11_MODULE(m) { return ""; } }); + // clang-format on initWrapper(m); @@ -67,15 +69,18 @@ SEMIWRAP_PYBIND11_MODULE(m) { m.attr("__halplatform__") = "sim"; m.attr("__hal_simulation__") = true; + // clang-format off m.def("__test_senderr", []() { wpi::hal::SendError(1, 2, "\xfa" "badmessage", "location", "callstack", 1); }, release_gil()); + // clang-format on #endif // Redirect stderr to python stderr sys_module = py::module_::import("sys"); + // clang-format off HAL_SetPrintErrorImpl([](const struct WPI_String* line) { if (line == nullptr || line->str == nullptr || line->len == 0) { return; @@ -90,7 +95,9 @@ SEMIWRAP_PYBIND11_MODULE(m) { py::print(py::reinterpret_steal(o), "file"_a=sys_module.attr("stderr")); } }); + // clang-format on + // clang-format off // Do cleanup on module unload static int unused; // the capsule needs something to reference py::capsule cleanup(&unused, [](void *) { @@ -106,5 +113,6 @@ SEMIWRAP_PYBIND11_MODULE(m) { HAL_Shutdown(); } }); + // clang-format on m.add_object("_cleanup", cleanup); } diff --git a/hal/src/main/python/native-pyproject.toml b/hal/src/main/python/native-pyproject.toml index 234985b559..a22a73a120 100644 --- a/hal/src/main/python/native-pyproject.toml +++ b/hal/src/main/python/native-pyproject.toml @@ -60,4 +60,4 @@ name = "wpihal" includedir = "src/native/wpihal/include" libdir = "src/native/wpihal/lib" shared_libraries = ["wpiHal"] -requires = ["robotpy-native-wpiutil", "robotpy-native-ntcore", "robotpy-native-mrclib"] \ No newline at end of file +requires = ["robotpy-native-wpiutil", "robotpy-native-ntcore", "robotpy-native-mrclib"] diff --git a/hal/src/main/python/semiwrap/SimDevice_c.yml b/hal/src/main/python/semiwrap/SimDevice_c.yml index 75e62a1062..1d88b7166f 100644 --- a/hal/src/main/python/semiwrap/SimDevice_c.yml +++ b/hal/src/main/python/semiwrap/SimDevice_c.yml @@ -10,4 +10,3 @@ strip_prefixes: enums: HAL_SimValueDirection: - diff --git a/ntcore/src/main/python/ntcore/src/NetworkTable.cpp.inl b/ntcore/src/main/python/ntcore/src/NetworkTable.cpp.inl index 0d9ff95d19..993e4a9ab7 100644 --- a/ntcore/src/main/python/ntcore/src/NetworkTable.cpp.inl +++ b/ntcore/src/main/python/ntcore/src/NetworkTable.cpp.inl @@ -1,3 +1,9 @@ +#pragma once + +#include +#include + +// clang-format off cls_NetworkTable .def("getValue", [](const NetworkTable &self, std::string_view key, py::object defaultValue) -> py::object { wpi::nt::NetworkTableEntry entry; @@ -54,3 +60,4 @@ cls_NetworkTable return self.ContainsKey(key); }, release_gil()) ; +// clang-format on diff --git a/ntcore/src/main/python/ntcore/src/NetworkTableEntry.cpp.inl b/ntcore/src/main/python/ntcore/src/NetworkTableEntry.cpp.inl index 12ab7ab4f1..7425aa485d 100644 --- a/ntcore/src/main/python/ntcore/src/NetworkTableEntry.cpp.inl +++ b/ntcore/src/main/python/ntcore/src/NetworkTableEntry.cpp.inl @@ -1,3 +1,8 @@ +#pragma once + +#include + +// clang-format off cls_NetworkTableEntry .def_property_readonly("value", [](const wpi::nt::NetworkTableEntry &self) { wpi::nt::Value v; @@ -46,3 +51,4 @@ cls_NetworkTableEntry return self->SetDefaultValue(pyntcore::py2ntvalue(value)); }, py::arg("value")) ; +// clang-format on diff --git a/ntcore/src/main/python/ntcore/src/nt_instance.cpp b/ntcore/src/main/python/ntcore/src/nt_instance.cpp index 27eddf868c..ca7cf2e8a3 100644 --- a/ntcore/src/main/python/ntcore/src/nt_instance.cpp +++ b/ntcore/src/main/python/ntcore/src/nt_instance.cpp @@ -1,56 +1,61 @@ -#include #include "nt_instance.h" -#include "wpi/nt/ntcore_cpp.hpp" #include +#include + +#include "wpi/nt/ntcore_cpp.hpp" + // only accessed under GIL static std::set g_known_instances; namespace pyntcore { -void onInstanceStart(wpi::nt::NetworkTableInstance *instance) { - g_known_instances.emplace(instance->GetHandle()); +void onInstanceStart(wpi::nt::NetworkTableInstance* instance) { + g_known_instances.emplace(instance->GetHandle()); - py::module::import("ntcore._logutil") - .attr("NtLogForwarder").attr("onInstanceStart")(instance); + py::module::import("ntcore._logutil") + .attr("NtLogForwarder") + .attr("onInstanceStart")(instance); } -void onInstancePreReset(wpi::nt::NetworkTableInstance *instance) { - py::module::import("ntcore._logutil") - .attr("NtLogForwarder").attr("onInstanceDestroy")(instance); +void onInstancePreReset(wpi::nt::NetworkTableInstance* instance) { + py::module::import("ntcore._logutil") + .attr("NtLogForwarder") + .attr("onInstanceDestroy")(instance); } -void onInstancePostReset(wpi::nt::NetworkTableInstance *instance) { - py::module::import("ntcore.util") - .attr("_NtProperty").attr("onInstancePostReset")(instance); +void onInstancePostReset(wpi::nt::NetworkTableInstance* instance) { + py::module::import("ntcore.util") + .attr("_NtProperty") + .attr("onInstancePostReset")(instance); } -void onInstanceDestroy(wpi::nt::NetworkTableInstance *instance) { - py::module::import("ntcore._logutil") - .attr("NtLogForwarder").attr("onInstanceDestroy")(instance); - py::module::import("ntcore.util") - .attr("_NtProperty").attr("onInstanceDestroy")(instance); +void onInstanceDestroy(wpi::nt::NetworkTableInstance* instance) { + py::module::import("ntcore._logutil") + .attr("NtLogForwarder") + .attr("onInstanceDestroy")(instance); + py::module::import("ntcore.util") + .attr("_NtProperty") + .attr("onInstanceDestroy")(instance); - g_known_instances.erase(instance->GetHandle()); + g_known_instances.erase(instance->GetHandle()); } // reset all instances to clear out any potential python references that // might be hanging around in a callback or something -void resetAllInstances() -{ - std::set known_instances; - known_instances.swap(g_known_instances); +void resetAllInstances() { + std::set known_instances; + known_instances.swap(g_known_instances); - // always reset the default instance - known_instances.emplace(wpi::nt::GetDefaultInstance()); + // always reset the default instance + known_instances.emplace(wpi::nt::GetDefaultInstance()); - py::gil_scoped_release unlock; + py::gil_scoped_release unlock; - for (auto &inst: known_instances) { - wpi::nt::ResetInstance(inst); - } + for (auto& inst : known_instances) { + wpi::nt::ResetInstance(inst); + } } - -}; // namespace pyntcore \ No newline at end of file +} // namespace pyntcore diff --git a/ntcore/src/main/python/ntcore/src/nt_instance.h b/ntcore/src/main/python/ntcore/src/nt_instance.h index 087d9b2e21..fa3417ac5e 100644 --- a/ntcore/src/main/python/ntcore/src/nt_instance.h +++ b/ntcore/src/main/python/ntcore/src/nt_instance.h @@ -1,16 +1,16 @@ #pragma once -#include "wpi/nt/ntcore.h" #include "wpi/nt/NetworkTableInstance.hpp" +#include "wpi/nt/ntcore.h" namespace pyntcore { -void onInstanceStart(wpi::nt::NetworkTableInstance *instance); -void onInstancePreReset(wpi::nt::NetworkTableInstance *instance); -void onInstancePostReset(wpi::nt::NetworkTableInstance *instance); -void onInstanceDestroy(wpi::nt::NetworkTableInstance *instance); +void onInstanceStart(wpi::nt::NetworkTableInstance* instance); +void onInstancePreReset(wpi::nt::NetworkTableInstance* instance); +void onInstancePostReset(wpi::nt::NetworkTableInstance* instance); +void onInstanceDestroy(wpi::nt::NetworkTableInstance* instance); void resetAllInstances(); -}; // namespace pyntcore +} // namespace pyntcore diff --git a/ntcore/src/main/python/ntcore/src/nt_type_caster.h b/ntcore/src/main/python/ntcore/src/nt_type_caster.h index de9158f29c..2676f69703 100644 --- a/ntcore/src/main/python/ntcore/src/nt_type_caster.h +++ b/ntcore/src/main/python/ntcore/src/nt_type_caster.h @@ -1,39 +1,41 @@ #pragma once +#include + #include -namespace pybind11 { -namespace detail { +namespace pybind11::detail { // ntcore uses std::vector anytime there is a raw value, so // add this specialization to convert to/from bytes directly -template<> +template <> struct type_caster> { - using vector_type = std::vector; - PYBIND11_TYPE_CASTER(vector_type, const_name("bytes")); + using vector_type = std::vector; + PYBIND11_TYPE_CASTER(vector_type, const_name("bytes")); - bool load(handle src, bool convert) { - if (!isinstance(src)) { - return false; - } - auto buf = reinterpret_borrow(src); - auto req = buf.request(); - if (req.ndim != 1) { - return false; - } - - auto begin = (const uint8_t*)req.ptr; - auto end = begin + req.size*req.itemsize; - - value = std::vector(begin, end); - return true; + bool load(handle src, bool convert) { + if (!isinstance(src)) { + return false; + } + auto buf = reinterpret_borrow(src); + auto req = buf.request(); + if (req.ndim != 1) { + return false; } - static handle cast(const std::vector &src, return_value_policy policy, handle parent) { - return py::bytes((char*)src.data(), src.size()).release(); - } + auto begin = (const uint8_t*)req.ptr; + auto end = begin + req.size * req.itemsize; + + value = std::vector(begin, end); + return true; + } + + static handle cast(const std::vector& src, + return_value_policy policy, handle parent) { + return py::bytes(reinterpret_cast(src.data()), src.size()) + .release(); + } }; -} -} \ No newline at end of file +} // namespace pybind11::detail diff --git a/ntcore/src/main/python/ntcore/src/ntcore.cpp b/ntcore/src/main/python/ntcore/src/ntcore.cpp index 169563a1e2..abca9108aa 100644 --- a/ntcore/src/main/python/ntcore/src/ntcore.cpp +++ b/ntcore/src/main/python/ntcore/src/ntcore.cpp @@ -1,14 +1,12 @@ -#include #include "nt_instance.h" +#include "semiwrap_init.ntcore._ntcore.hpp" SEMIWRAP_PYBIND11_MODULE(m) { initWrapper(m); static int unused; - py::capsule cleanup(&unused, [](void *) { - pyntcore::resetAllInstances(); - }); + py::capsule cleanup(&unused, [](void*) { pyntcore::resetAllInstances(); }); m.add_object("_st_cleanup", cleanup); -} \ No newline at end of file +} diff --git a/ntcore/src/main/python/ntcore/src/py2value.cpp b/ntcore/src/main/python/ntcore/src/py2value.cpp index ad9b1ecec8..9a3196a2e5 100644 --- a/ntcore/src/main/python/ntcore/src/py2value.cpp +++ b/ntcore/src/main/python/ntcore/src/py2value.cpp @@ -1,111 +1,110 @@ #include "py2value.h" +#include +#include #include // type casters #include #include - - namespace pyntcore { -const char * nttype2str(NT_Type type) { +const char* nttype2str(NT_Type type) { switch (type) { - case NT_BOOLEAN: - return "bool"; - case NT_DOUBLE: - return "double"; - case NT_STRING: - return "string"; - case NT_RAW: - return "raw"; - case NT_BOOLEAN_ARRAY: - return "bool[]"; - case NT_DOUBLE_ARRAY: - return "double[]"; - case NT_STRING_ARRAY: - return "string[]"; - case NT_INTEGER: - return "int"; - case NT_FLOAT: - return "float"; - case NT_INTEGER_ARRAY: - return "int[]"; - case NT_FLOAT_ARRAY: - return "float[]"; - default: - return "invalid"; + case NT_BOOLEAN: + return "bool"; + case NT_DOUBLE: + return "double"; + case NT_STRING: + return "string"; + case NT_RAW: + return "raw"; + case NT_BOOLEAN_ARRAY: + return "bool[]"; + case NT_DOUBLE_ARRAY: + return "double[]"; + case NT_STRING_ARRAY: + return "string[]"; + case NT_INTEGER: + return "int"; + case NT_FLOAT: + return "float"; + case NT_INTEGER_ARRAY: + return "int[]"; + case NT_FLOAT_ARRAY: + return "float[]"; + default: + return "invalid"; } } - -py::object ntvalue2py(const wpi::nt::Value &ntvalue) { - auto &v = ntvalue.value(); +py::object ntvalue2py(const wpi::nt::Value& ntvalue) { + auto& v = ntvalue.value(); switch (v.type) { - case NT_BOOLEAN: - return py::bool_(v.data.v_boolean); + case NT_BOOLEAN: + return py::bool_(v.data.v_boolean); - case NT_DOUBLE: - return py::float_(v.data.v_double); + case NT_DOUBLE: + return py::float_(v.data.v_double); - case NT_STRING: - return py::str(v.data.v_string.str, v.data.v_string.len); + case NT_STRING: + return py::str(v.data.v_string.str, v.data.v_string.len); - case NT_RAW: - return py::bytes((const char *)v.data.v_raw.data, v.data.v_raw.size); + case NT_RAW: + return py::bytes((const char*)v.data.v_raw.data, v.data.v_raw.size); - case NT_BOOLEAN_ARRAY: { - py::list l(v.data.arr_boolean.size); - for (size_t i = 0; i < v.data.arr_boolean.size; i++) { - auto b = py::bool_(v.data.arr_boolean.arr[i]); - PyList_SET_ITEM(l.ptr(), i, b.release().ptr()); + case NT_BOOLEAN_ARRAY: { + py::list l(v.data.arr_boolean.size); + for (size_t i = 0; i < v.data.arr_boolean.size; i++) { + auto b = py::bool_(v.data.arr_boolean.arr[i]); + PyList_SET_ITEM(l.ptr(), i, b.release().ptr()); + } + return std::move(l); } - return std::move(l); - } - case NT_DOUBLE_ARRAY: { - py::list l(v.data.arr_double.size); - for (size_t i = 0; i < v.data.arr_double.size; i++) { - auto d = py::float_(v.data.arr_double.arr[i]); - PyList_SET_ITEM(l.ptr(), i, d.release().ptr()); + case NT_DOUBLE_ARRAY: { + py::list l(v.data.arr_double.size); + for (size_t i = 0; i < v.data.arr_double.size; i++) { + auto d = py::float_(v.data.arr_double.arr[i]); + PyList_SET_ITEM(l.ptr(), i, d.release().ptr()); + } + return std::move(l); } - return std::move(l); - } - case NT_STRING_ARRAY: { - return py::cast(ntvalue.GetStringArray()); - } - - case NT_INTEGER: { - return py::int_(v.data.v_int); - } - - case NT_FLOAT: { - return py::float_(v.data.v_float); - } - - case NT_INTEGER_ARRAY: { - py::list l(v.data.arr_int.size); - for (size_t i = 0; i < v.data.arr_int.size; i++) { - auto d = py::int_(v.data.arr_int.arr[i]); - PyList_SET_ITEM(l.ptr(), i, d.release().ptr()); + case NT_STRING_ARRAY: { + return py::cast(ntvalue.GetStringArray()); } - return std::move(l); - } - case NT_FLOAT_ARRAY: { - py::list l(v.data.arr_float.size); - for (size_t i = 0; i < v.data.arr_float.size; i++) { - auto d = py::float_(v.data.arr_float.arr[i]); - PyList_SET_ITEM(l.ptr(), i, d.release().ptr()); + case NT_INTEGER: { + return py::int_(v.data.v_int); } - return std::move(l); - } - default: - return py::none(); + case NT_FLOAT: { + return py::float_(v.data.v_float); + } + + case NT_INTEGER_ARRAY: { + py::list l(v.data.arr_int.size); + for (size_t i = 0; i < v.data.arr_int.size; i++) { + auto d = py::int_(v.data.arr_int.arr[i]); + PyList_SET_ITEM(l.ptr(), i, d.release().ptr()); + } + return std::move(l); + } + + case NT_FLOAT_ARRAY: { + py::list l(v.data.arr_float.size); + for (size_t i = 0; i < v.data.arr_float.size; i++) { + auto d = py::float_(v.data.arr_float.arr[i]); + PyList_SET_ITEM(l.ptr(), i, d.release().ptr()); + } + return std::move(l); + } + + default: + return py::none(); } } @@ -143,38 +142,39 @@ wpi::nt::Value py2ntvalue(py::handle h) { auto v = h.cast>(); return wpi::nt::Value::MakeStringArray(v); } else { - throw py::value_error("Can only put bool/int/float/str/bytes or lists/tuples of them"); + throw py::value_error( + "Can only put bool/int/float/str/bytes or lists/tuples of them"); } } py::function valueFactoryByType(wpi::nt::NetworkTableType type) { py::object PyNtValue = py::module::import("ntcore").attr("Value"); switch (type) { - case wpi::nt::NetworkTableType::BOOLEAN: - return PyNtValue.attr("makeBoolean"); - case wpi::nt::NetworkTableType::DOUBLE: - return PyNtValue.attr("makeDouble"); - case wpi::nt::NetworkTableType::STRING: - return PyNtValue.attr("makeString"); - case wpi::nt::NetworkTableType::RAW: - return PyNtValue.attr("makeRaw"); - case wpi::nt::NetworkTableType::BOOLEAN_ARRAY: - return PyNtValue.attr("makeBooleanArray"); - case wpi::nt::NetworkTableType::DOUBLE_ARRAY: - return PyNtValue.attr("makeDoubleArray"); - case wpi::nt::NetworkTableType::STRING_ARRAY: - return PyNtValue.attr("makeStringArray"); - case wpi::nt::NetworkTableType::INTEGER: - return PyNtValue.attr("makeInteger"); - case wpi::nt::NetworkTableType::FLOAT: - return PyNtValue.attr("makeFloat"); - case wpi::nt::NetworkTableType::INTEGER_ARRAY: - return PyNtValue.attr("makeIntegerArray"); - case wpi::nt::NetworkTableType::FLOAT_ARRAY: - return PyNtValue.attr("makeFloatArray"); - default: - throw py::type_error("empty nt value"); + case wpi::nt::NetworkTableType::BOOLEAN: + return PyNtValue.attr("makeBoolean"); + case wpi::nt::NetworkTableType::DOUBLE: + return PyNtValue.attr("makeDouble"); + case wpi::nt::NetworkTableType::STRING: + return PyNtValue.attr("makeString"); + case wpi::nt::NetworkTableType::RAW: + return PyNtValue.attr("makeRaw"); + case wpi::nt::NetworkTableType::BOOLEAN_ARRAY: + return PyNtValue.attr("makeBooleanArray"); + case wpi::nt::NetworkTableType::DOUBLE_ARRAY: + return PyNtValue.attr("makeDoubleArray"); + case wpi::nt::NetworkTableType::STRING_ARRAY: + return PyNtValue.attr("makeStringArray"); + case wpi::nt::NetworkTableType::INTEGER: + return PyNtValue.attr("makeInteger"); + case wpi::nt::NetworkTableType::FLOAT: + return PyNtValue.attr("makeFloat"); + case wpi::nt::NetworkTableType::INTEGER_ARRAY: + return PyNtValue.attr("makeIntegerArray"); + case wpi::nt::NetworkTableType::FLOAT_ARRAY: + return PyNtValue.attr("makeFloatArray"); + default: + throw py::type_error("empty nt value"); } } -} +} // namespace pyntcore diff --git a/ntcore/src/main/python/ntcore/src/py2value.h b/ntcore/src/main/python/ntcore/src/py2value.h index 53f144c730..fce52c8ec6 100644 --- a/ntcore/src/main/python/ntcore/src/py2value.h +++ b/ntcore/src/main/python/ntcore/src/py2value.h @@ -1,25 +1,27 @@ +#pragma once -#include -#include "wpi/nt/NetworkTableValue.hpp" -#include "wpi/nt/NetworkTableType.hpp" #include +#include + +#include "wpi/nt/NetworkTableType.hpp" +#include "wpi/nt/NetworkTableValue.hpp" namespace pyntcore { -const char * nttype2str(NT_Type type); +const char* nttype2str(NT_Type type); -py::object ntvalue2py(const wpi::nt::Value &ntvalue); +py::object ntvalue2py(const wpi::nt::Value& ntvalue); wpi::nt::Value py2ntvalue(py::handle h); py::function valueFactoryByType(wpi::nt::NetworkTableType type); -inline void ensure_value_is(NT_Type expected, wpi::nt::Value *v) { - if (v->type() != expected) { - throw py::value_error(fmt::format( - "Value type is {}, not {}", nttype2str(v->type()), nttype2str(expected) - )); - } +inline void ensure_value_is(NT_Type expected, wpi::nt::Value* v) { + if (v->type() != expected) { + throw py::value_error(fmt::format("Value type is {}, not {}", + nttype2str(v->type()), + nttype2str(expected))); + } } -}; \ No newline at end of file +} // namespace pyntcore diff --git a/ntcore/src/main/python/ntcore/src/pyentry.cpp b/ntcore/src/main/python/ntcore/src/pyentry.cpp index c4147953fa..c52f107c05 100644 --- a/ntcore/src/main/python/ntcore/src/pyentry.cpp +++ b/ntcore/src/main/python/ntcore/src/pyentry.cpp @@ -1,141 +1,179 @@ #include "pyentry.h" -#include "py2value.h" + +#include +#include #include #include +#include "py2value.h" + namespace pyntcore { -py::object GetBooleanEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_BOOLEAN) return defaultValue; - return py::cast(value.GetBoolean()); +py::object GetBooleanEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_BOOLEAN) { + return defaultValue; + } + return py::cast(value.GetBoolean()); } -py::object GetDoubleEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_DOUBLE) return defaultValue; - return py::cast(value.GetDouble()); +py::object GetDoubleEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_DOUBLE) { + return defaultValue; + } + return py::cast(value.GetDouble()); } -py::object GetFloatEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_FLOAT) return defaultValue; - return py::cast(value.GetFloat()); +py::object GetFloatEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_FLOAT) { + return defaultValue; + } + return py::cast(value.GetFloat()); } -py::object GetIntegerEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_INTEGER) return defaultValue; - return py::cast(value.GetInteger()); +py::object GetIntegerEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_INTEGER) { + return defaultValue; + } + return py::cast(value.GetInteger()); } - -py::object GetStringEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_STRING) return defaultValue; - auto s = value.GetString(); - return py::str(s.data(), s.size()); +py::object GetStringEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_STRING) { + return defaultValue; + } + auto s = value.GetString(); + return py::str(s.data(), s.size()); } -py::object GetRawEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_RAW) return defaultValue; - return py::cast(value.GetRaw()); +py::object GetRawEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_RAW) { + return defaultValue; + } + return py::cast(value.GetRaw()); } -py::object GetBooleanArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_BOOLEAN_ARRAY) return defaultValue; - // ntcore will return bit vector by default. Convert to List[bool] - auto v = value.value(); - py::list l(v.data.arr_boolean.size); - for (size_t i = 0; i < v.data.arr_boolean.size; i++) { - auto b = py::bool_(v.data.arr_boolean.arr[i]); - PyList_SET_ITEM(l.ptr(), i, b.release().ptr()); - } - return std::move(l); +py::object GetBooleanArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_BOOLEAN_ARRAY) { + return defaultValue; + } + // ntcore will return bit vector by default. Convert to List[bool] + auto v = value.value(); + py::list l(v.data.arr_boolean.size); + for (size_t i = 0; i < v.data.arr_boolean.size; i++) { + auto b = py::bool_(v.data.arr_boolean.arr[i]); + PyList_SET_ITEM(l.ptr(), i, b.release().ptr()); + } + return std::move(l); } -py::object GetDoubleArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_DOUBLE_ARRAY) return defaultValue; - return py::cast(value.GetDoubleArray()); +py::object GetDoubleArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_DOUBLE_ARRAY) { + return defaultValue; + } + return py::cast(value.GetDoubleArray()); } -py::object GetFloatArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_FLOAT_ARRAY) return defaultValue; - return py::cast(value.GetFloatArray()); +py::object GetFloatArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_FLOAT_ARRAY) { + return defaultValue; + } + return py::cast(value.GetFloatArray()); } -py::object GetIntegerArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_INTEGER_ARRAY) return defaultValue; - return py::cast(value.GetIntegerArray()); +py::object GetIntegerArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_INTEGER_ARRAY) { + return defaultValue; + } + return py::cast(value.GetIntegerArray()); } -py::object GetStringArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value || value.type() != NT_STRING_ARRAY) return defaultValue; - std::span rval = value.GetStringArray(); - return py::cast(rval); +py::object GetStringArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value || value.type() != NT_STRING_ARRAY) { + return defaultValue; + } + std::span rval = value.GetStringArray(); + return py::cast(rval); } -py::object GetValueEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) { - wpi::nt::Value value; - { - py::gil_scoped_release release; - value = wpi::nt::GetEntryValue(entry.GetHandle()); - } - if (!value) return defaultValue; - return ntvalue2py(value); +py::object GetValueEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue) { + wpi::nt::Value value; + { + py::gil_scoped_release release; + value = wpi::nt::GetEntryValue(entry.GetHandle()); + } + if (!value) { + return defaultValue; + } + return ntvalue2py(value); } - -}; // pyntcore +} // namespace pyntcore diff --git a/ntcore/src/main/python/ntcore/src/pyentry.h b/ntcore/src/main/python/ntcore/src/pyentry.h index 9c2d715635..c45188f348 100644 --- a/ntcore/src/main/python/ntcore/src/pyentry.h +++ b/ntcore/src/main/python/ntcore/src/pyentry.h @@ -1,21 +1,35 @@ +#pragma once #include + #include "wpi/nt/NetworkTableEntry.hpp" #include "wpi/nt/NetworkTableValue.hpp" namespace pyntcore { -py::object GetBooleanEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetDoubleEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetFloatEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetIntegerEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetStringEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetRawEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetBooleanArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetDoubleArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetFloatArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetIntegerArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetStringArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); -py::object GetValueEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue); +py::object GetBooleanEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetDoubleEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetFloatEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetIntegerEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetStringEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetRawEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetBooleanArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetDoubleArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetFloatArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetIntegerArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetStringArrayEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); +py::object GetValueEntry(const wpi::nt::NetworkTableEntry& entry, + py::object defaultValue); -}; +} // namespace pyntcore diff --git a/ntcore/src/test/native/cpp/net/NetworkOutgoingQueueTest.cpp b/ntcore/src/test/native/cpp/net/NetworkOutgoingQueueTest.cpp index d41454d1bb..40f83c527d 100644 --- a/ntcore/src/test/native/cpp/net/NetworkOutgoingQueueTest.cpp +++ b/ntcore/src/test/native/cpp/net/NetworkOutgoingQueueTest.cpp @@ -21,8 +21,6 @@ #include "wpi/nt/NetworkTableValue.hpp" #include "wpi/util/raw_ostream.hpp" -using namespace std::string_literals; - namespace wpi::nt::net { namespace { @@ -119,6 +117,7 @@ std::pair DecodeBinary(std::span data, } ClientMessage Publish(int pubuid, std::string_view name) { + using namespace std::literals; return ClientMessage{PublishMsg{ pubuid, std::string{name}, "double"s, wpi::util::json::object(), {}}}; } diff --git a/romiVendordep/src/main/python/romi/src/main.cpp b/romiVendordep/src/main/python/romi/src/main.cpp index 9d713e6bc2..7d149ee979 100644 --- a/romiVendordep/src/main/python/romi/src/main.cpp +++ b/romiVendordep/src/main/python/romi/src/main.cpp @@ -1,6 +1,6 @@ -#include +#include "semiwrap_init.romi._romi.hpp" SEMIWRAP_PYBIND11_MODULE(m) { - initWrapper(m); -} \ No newline at end of file + initWrapper(m); +} diff --git a/simulation/halsim_ds_socket/src/main/python/pyproject.toml b/simulation/halsim_ds_socket/src/main/python/pyproject.toml index 82265d8d67..9971fd6a38 100644 --- a/simulation/halsim_ds_socket/src/main/python/pyproject.toml +++ b/simulation/halsim_ds_socket/src/main/python/pyproject.toml @@ -1,7 +1,7 @@ [build-system] build-backend = "hatchling.build" requires = [ - "hatchling", + "hatchling", "hatch-robotpy~=0.2.1", ] @@ -38,4 +38,4 @@ libs = ["halsim_ds_socket"] [tool.hatch.build.targets.wheel] -packages = ["halsim_ds_socket"] \ No newline at end of file +packages = ["halsim_ds_socket"] diff --git a/simulation/halsim_gui/src/main/python/halsim_gui/_ext/main.cpp b/simulation/halsim_gui/src/main/python/halsim_gui/_ext/main.cpp index a566a30865..40f19a9c54 100644 --- a/simulation/halsim_gui/src/main/python/halsim_gui/_ext/main.cpp +++ b/simulation/halsim_gui/src/main/python/halsim_gui/_ext/main.cpp @@ -1,20 +1,19 @@ -#include - #include -#include #include +#include + +#include "semiwrap_init.halsim_gui._ext._halsim_gui_ext.hpp" std::function g_gui_exit; SEMIWRAP_PYBIND11_MODULE(m) { - initWrapper(m); m.def("_kill_on_signal", []() { HAL_RegisterExtensionListener( - nullptr, [](void *, const char *name, void *data) { + nullptr, [](void*, const char* name, void* data) { std::string_view name_view{name}; if (name_view == HALSIMGUI_EXT_GUIEXIT) { g_gui_exit = (halsimgui::GuiExitFn)data; @@ -23,14 +22,14 @@ SEMIWRAP_PYBIND11_MODULE(m) { AddGuiLateExecute([] { py::gil_scoped_acquire gil; if (PyErr_CheckSignals() == -1) { - - // If a python signal has been triggered, the GUI needs to exit. It's - // not safe to throw an exception here on all platforms so we just - // assume that the only eventual caller of this function is HAL_RunMain, - // and our wrapper around that function will check if a python error is - // set and throw from there. + // If a python signal has been triggered, the GUI needs to exit. + // It's not safe to throw an exception here on all platforms so + // we just assume that the only eventual caller of this function + // is HAL_RunMain, and our wrapper around that function will + // check if a python error is set and throw from there. // - // Reference: https://github.com/wpilibsuite/allwpilib/issues/8528 + // Reference: + // https://github.com/wpilibsuite/allwpilib/issues/8528 if (g_gui_exit) { g_gui_exit(); } else { @@ -41,4 +40,4 @@ SEMIWRAP_PYBIND11_MODULE(m) { } }); }); -} \ No newline at end of file +} diff --git a/wpilibc/src/main/python/LICENSE.txt b/wpilibc/src/main/python/LICENSE.txt index 82323225e9..2736a1488e 100644 --- a/wpilibc/src/main/python/LICENSE.txt +++ b/wpilibc/src/main/python/LICENSE.txt @@ -18,8 +18,8 @@ modification, are permitted provided that the following conditions are met: THIS SOFTWARE IS PROVIDED BY ROBOTPY AND CONTRIBUTORS``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY NONINFRINGEMENT AND FITNESS FOR A PARTICULAR -PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ROBOTPY OR CONTRIBUTORS BE LIABLE FOR +WARRANTIES OF MERCHANTABILITY NONINFRINGEMENT AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ROBOTPY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND @@ -45,12 +45,11 @@ Code for WPILib was derived from code using the following license: * * THIS SOFTWARE IS PROVIDED BY FIRST AND CONTRIBUTORS``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -* WARRANTIES OF MERCHANTABILITY NONINFRINGEMENT AND FITNESS FOR A PARTICULAR -* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FIRST OR CONTRIBUTORS BE LIABLE FOR +* WARRANTIES OF MERCHANTABILITY NONINFRINGEMENT AND FITNESS FOR A PARTICULAR +* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FIRST OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - diff --git a/wpilibc/src/main/python/semiwrap/AddressableLED.yml b/wpilibc/src/main/python/semiwrap/AddressableLED.yml index d759d42975..8a17dd32ee 100644 --- a/wpilibc/src/main/python/semiwrap/AddressableLED.yml +++ b/wpilibc/src/main/python/semiwrap/AddressableLED.yml @@ -24,7 +24,7 @@ classes: ColorOrder: inline_code: | .def("setData", [](wpi::AddressableLED& self, const wpi::AddressableLEDBuffer& data) { - return self.SetData(data); + return self.SetData(data); }, release_gil(), py::prepend()); wpi::AddressableLED::LEDData: force_no_trampoline: true diff --git a/wpilibc/src/main/python/wpilib/simulation/simulation.cpp b/wpilibc/src/main/python/wpilib/simulation/simulation.cpp index 7b949bd7a6..5102084738 100644 --- a/wpilibc/src/main/python/wpilib/simulation/simulation.cpp +++ b/wpilibc/src/main/python/wpilib/simulation/simulation.cpp @@ -6,11 +6,11 @@ namespace wpi::impl { void ResetSmartDashboardInstance(); void ResetMotorSafety(); -} // namespace wpi::impl +} // namespace wpi::impl namespace wpi::util::impl { void ResetSendableRegistry(); -} // namespace wpi::impl +} // namespace wpi::util::impl void resetWpilibSimulationData() { wpi::impl::ResetSmartDashboardInstance(); diff --git a/wpilibc/src/main/python/wpilib/src/main.cpp b/wpilibc/src/main/python/wpilib/src/main.cpp index ea7f803161..9fae077c93 100644 --- a/wpilibc/src/main/python/wpilib/src/main.cpp +++ b/wpilibc/src/main/python/wpilib/src/main.cpp @@ -1,4 +1,6 @@ #include "semiwrap_init.wpilib._wpilib.hpp" -SEMIWRAP_PYBIND11_MODULE(m) { initWrapper(m); } +SEMIWRAP_PYBIND11_MODULE(m) { + initWrapper(m); +} diff --git a/wpilibc/src/main/python/wpilib/src/rpy/AddressableLEDBuffer.cpp b/wpilibc/src/main/python/wpilib/src/rpy/AddressableLEDBuffer.cpp index 5e04259a1c..d6562ab68b 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/AddressableLEDBuffer.cpp +++ b/wpilibc/src/main/python/wpilib/src/rpy/AddressableLEDBuffer.cpp @@ -4,8 +4,8 @@ #include "rpy/AddressableLEDBuffer.h" -#include #include +#include namespace wpi { @@ -21,17 +21,22 @@ void AddressableLEDBuffer::SetLED(size_t index, const wpi::util::Color& color) { m_buffer.at(index).SetLED(color); } -void AddressableLEDBuffer::SetLED(size_t index, const wpi::util::Color8Bit& color) { +void AddressableLEDBuffer::SetLED(size_t index, + const wpi::util::Color8Bit& color) { m_buffer.at(index).SetLED(color); } -int AddressableLEDBuffer::GetRed(size_t index) const { return m_buffer.at(index).r; } +int AddressableLEDBuffer::GetRed(size_t index) const { + return m_buffer.at(index).r; +} int AddressableLEDBuffer::GetGreen(size_t index) const { return m_buffer.at(index).g; } -int AddressableLEDBuffer::GetBlue(size_t index) const { return m_buffer.at(index).b; } +int AddressableLEDBuffer::GetBlue(size_t index) const { + return m_buffer.at(index).b; +} wpi::util::Color AddressableLEDBuffer::GetLED(size_t index) const { const auto& led = m_buffer.at(index); @@ -64,7 +69,8 @@ void AddressableLEDBuffer::View::SetHSV(size_t index, int h, int s, int v) { at(index).SetHSV(h, s, v); } -void AddressableLEDBuffer::View::SetLED(size_t index, const wpi::util::Color& color) { +void AddressableLEDBuffer::View::SetLED(size_t index, + const wpi::util::Color& color) { at(index).SetLED(color); } @@ -81,8 +87,7 @@ AddressableLED::LEDData& AddressableLEDBuffer::View::at(size_t index) { return m_data[index]; } -AddressableLED::LEDData& AddressableLEDBuffer::View::operator[]( - size_t index) { +AddressableLED::LEDData& AddressableLEDBuffer::View::operator[](size_t index) { return at(index); } @@ -105,7 +110,8 @@ wpi::util::Color AddressableLEDBuffer::View::GetLED(size_t index) const { return wpi::util::Color{led.r / 255.0, led.g / 255.0, led.b / 255.0}; } -wpi::util::Color8Bit AddressableLEDBuffer::View::GetLED8Bit(size_t index) const { +wpi::util::Color8Bit AddressableLEDBuffer::View::GetLED8Bit( + size_t index) const { const auto& led = at(index); return wpi::util::Color8Bit{led.r, led.g, led.b}; } diff --git a/wpilibc/src/main/python/wpilib/src/rpy/AddressableLEDBuffer.h b/wpilibc/src/main/python/wpilib/src/rpy/AddressableLEDBuffer.h index ec99cdda79..67d71ffe3b 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/AddressableLEDBuffer.h +++ b/wpilibc/src/main/python/wpilib/src/rpy/AddressableLEDBuffer.h @@ -7,6 +7,7 @@ #include #include #include + #include "pybind11/pytypes.h" #include "wpi/hardware/led/AddressableLED.hpp" #include "wpi/util/Color.hpp" @@ -151,8 +152,9 @@ class AddressableLEDBuffer { auto end() { return m_buffer.end(); } /** - * A view of another addressable LED buffer. Views provide an easy way to split a large LED - * strip into smaller sections that can be animated individually. + * A view of another addressable LED buffer. Views provide an easy way to + * split a large LED strip into smaller sections that can be animated + * individually. */ class View { public: @@ -251,9 +253,7 @@ class AddressableLEDBuffer { /** * Implicit conversion to span of LED data */ - operator std::span() { - return m_data; - } + operator std::span() { return m_data; } /** * Implicit conversion to span of const LED data @@ -272,7 +272,8 @@ class AddressableLEDBuffer { /** * Creates a read/write view of this buffer. * - * @param slice the desired slice of the buffer (e.g. 2:4), step must be unspecified or 1 + * @param slice the desired slice of the buffer (e.g. 2:4), step must be + * unspecified or 1 * @return View object representing the view * @throws std::out_of_range if the view would exceed buffer bounds */ @@ -282,4 +283,4 @@ class AddressableLEDBuffer { std::vector m_buffer; }; -} // namespace frc +} // namespace wpi diff --git a/wpilibc/src/main/python/wpilib/src/rpy/Filesystem.h b/wpilibc/src/main/python/wpilib/src/rpy/Filesystem.h index 95dc59f595..79161438c3 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/Filesystem.h +++ b/wpilibc/src/main/python/wpilib/src/rpy/Filesystem.h @@ -1,6 +1,8 @@ #pragma once +#include + #include "wpi/util/fs.hpp" namespace robotpy::filesystem { @@ -16,8 +18,8 @@ std::string GetOperatingDirectory(); /** * Obtains the deploy directory of the program, which is the remote location * the deploy directory is deployed to by default. On the robot, this is - * /home/systemcore/py/deploy. In simulation, it is where the simulation was launched - * from, in the subdirectory "deploy" (`dirname(robot.py)`/deploy). + * /home/systemcore/py/deploy. In simulation, it is where the simulation was + * launched from, in the subdirectory "deploy" (`dirname(robot.py)`/deploy). * * @return The result of the operating directory lookup */ @@ -28,6 +30,6 @@ fs::path GetOperatingDirectoryFs(); // intended to be used by C++ bindings, returns same as GetDeployDirectory fs::path GetDeployDirectoryFs(); -} // namespace robotpy::filesystem +} // namespace robotpy::filesystem #include "Filesystem.inc" diff --git a/wpilibc/src/main/python/wpilib/src/rpy/Filesystem.inc b/wpilibc/src/main/python/wpilib/src/rpy/Filesystem.inc index 61317990ad..1e424a28da 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/Filesystem.inc +++ b/wpilibc/src/main/python/wpilib/src/rpy/Filesystem.inc @@ -1,5 +1,9 @@ +#pragma once -// TODO: this should be in a shared library, but robotpy-build does not support that +// TODO: this should be in a shared library, but robotpy-build does not support +// that + +#include #include #include @@ -42,13 +46,17 @@ inline std::string GetOperatingDirectory() { return GetOperatingDirectoryFs().string(); } -inline std::string GetDeployDirectory() { return GetDeployDirectoryFs().string(); } +inline std::string GetDeployDirectory() { + return GetDeployDirectoryFs().string(); +} inline fs::path GetOperatingDirectoryFs() { static fs::path operatingPath = getMainPath(); return operatingPath; } -inline fs::path GetDeployDirectoryFs() { return GetOperatingDirectoryFs() / "deploy"; } +inline fs::path GetDeployDirectoryFs() { + return GetOperatingDirectoryFs() / "deploy"; +} -} // namespace robotpy::filesystem +} // namespace robotpy::filesystem diff --git a/wpilibc/src/main/python/wpilib/src/rpy/MotorControllerGroup.cpp b/wpilibc/src/main/python/wpilib/src/rpy/MotorControllerGroup.cpp index 9db4fb9313..300418dd19 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/MotorControllerGroup.cpp +++ b/wpilibc/src/main/python/wpilib/src/rpy/MotorControllerGroup.cpp @@ -40,7 +40,9 @@ void PyMotorControllerGroup::SetInverted(bool isInverted) { m_isInverted = isInverted; } -bool PyMotorControllerGroup::GetInverted() const { return m_isInverted; } +bool PyMotorControllerGroup::GetInverted() const { + return m_isInverted; +} void PyMotorControllerGroup::Disable() { for (auto motorController : m_motorControllers) { @@ -51,6 +53,7 @@ void PyMotorControllerGroup::Disable() { void PyMotorControllerGroup::InitSendable(wpi::util::SendableBuilder& builder) { builder.SetSmartDashboardType("Motor Controller"); builder.SetActuator(true); - builder.AddDoubleProperty("Value", [=, this]() { return GetThrottle(); }, - [=, this](double value) { SetThrottle(value); }); + builder.AddDoubleProperty( + "Value", [=, this]() { return GetThrottle(); }, + [=, this](double value) { SetThrottle(value); }); } diff --git a/wpilibc/src/main/python/wpilib/src/rpy/MotorControllerGroup.h b/wpilibc/src/main/python/wpilib/src/rpy/MotorControllerGroup.h index df5fa26f0f..095c8ac286 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/MotorControllerGroup.h +++ b/wpilibc/src/main/python/wpilib/src/rpy/MotorControllerGroup.h @@ -5,9 +5,8 @@ #pragma once #include -#include #include - +#include #include "wpi/hardware/motor/MotorController.hpp" #include "wpi/util/sendable/Sendable.hpp" @@ -15,12 +14,14 @@ namespace wpi { -class PyMotorControllerGroup : public wpi::util::Sendable, - public MotorController, - public wpi::util::SendableHelper { +class PyMotorControllerGroup + : public wpi::util::Sendable, + public MotorController, + public wpi::util::SendableHelper { public: - PyMotorControllerGroup(std::vector> &&args) : - m_motorControllers(args) {} + explicit PyMotorControllerGroup( + std::vector>&& args) + : m_motorControllers(args) {} ~PyMotorControllerGroup() override = default; PyMotorControllerGroup(PyMotorControllerGroup&&) = default; @@ -42,4 +43,4 @@ class PyMotorControllerGroup : public wpi::util::Sendable, std::vector> m_motorControllers; }; -} // namespace rpy +} // namespace wpi diff --git a/wpilibc/src/main/python/wpilib/src/rpy/Notifier.cpp b/wpilibc/src/main/python/wpilib/src/rpy/Notifier.cpp index a541500da9..a135295c5b 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/Notifier.cpp +++ b/wpilibc/src/main/python/wpilib/src/rpy/Notifier.cpp @@ -6,15 +6,15 @@ #include +#include +#include + #include "wpi/hal/Notifier.hpp" #include "wpi/hal/Threads.h" #include "wpi/system/Errors.hpp" #include "wpi/system/Timer.hpp" #include "wpi/util/Synchronization.h" -#include -#include - using namespace wpi; using namespace pybind11::literals; @@ -100,14 +100,14 @@ PyNotifier::~PyNotifier() { } } -PyNotifier::PyNotifier(PyNotifier &&rhs) +PyNotifier::PyNotifier(PyNotifier&& rhs) : m_thread(std::move(rhs.m_thread)), m_notifier(rhs.m_notifier.load()), m_handler(std::move(rhs.m_handler)) { rhs.m_notifier = HAL_INVALID_HANDLE; } -PyNotifier &PyNotifier::operator=(PyNotifier &&rhs) { +PyNotifier& PyNotifier::operator=(PyNotifier&& rhs) { m_thread = std::move(rhs.m_thread); m_notifier = rhs.m_notifier.load(); rhs.m_notifier = HAL_INVALID_HANDLE; diff --git a/wpilibc/src/main/python/wpilib/src/rpy/Notifier.h b/wpilibc/src/main/python/wpilib/src/rpy/Notifier.h index d008a819cd..4666e569b7 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/Notifier.h +++ b/wpilibc/src/main/python/wpilib/src/rpy/Notifier.h @@ -13,12 +13,12 @@ #include #include +#include + #include "wpi/hal/Types.h" #include "wpi/units/time.hpp" #include "wpi/util/mutex.hpp" -#include - namespace wpi { class PyNotifier { @@ -42,8 +42,8 @@ class PyNotifier { */ virtual ~PyNotifier(); - PyNotifier(PyNotifier &&rhs); - PyNotifier &operator=(PyNotifier &&rhs); + PyNotifier(PyNotifier&& rhs); + PyNotifier& operator=(PyNotifier&& rhs); /** * Sets the name of the notifier. Used for debugging purposes only. @@ -101,7 +101,7 @@ class PyNotifier { */ int32_t GetOverrun() const; -private: + private: // The thread waiting on the HAL alarm py::object m_thread; @@ -115,4 +115,4 @@ private: std::function m_handler; }; -} // namespace wpi +} // namespace wpi diff --git a/wpilibc/src/main/python/wpilib/src/rpy/SmartDashboardData.cpp b/wpilibc/src/main/python/wpilib/src/rpy/SmartDashboardData.cpp index 9e3f1c2dba..f5758cc290 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/SmartDashboardData.cpp +++ b/wpilibc/src/main/python/wpilib/src/rpy/SmartDashboardData.cpp @@ -1,6 +1,8 @@ #include "SmartDashboardData.h" +#include + namespace rpy { // @@ -10,25 +12,26 @@ namespace rpy { // All functions here must be called with the GIL held // -static py::dict &getSmartDashboardData() { +static py::dict& getSmartDashboardData() { static py::dict data; return data; } -void addSmartDashboardData(py::str &key, std::shared_ptr data) { - auto &sdData = getSmartDashboardData(); +void addSmartDashboardData(py::str& key, + std::shared_ptr data) { + auto& sdData = getSmartDashboardData(); sdData[key] = py::cast(data); } void clearSmartDashboardData() { - auto &sdData = getSmartDashboardData(); + auto& sdData = getSmartDashboardData(); if (sdData) { sdData.clear(); } } void destroySmartDashboardData() { - auto &sdData = getSmartDashboardData(); + auto& sdData = getSmartDashboardData(); if (sdData) { sdData.clear(); // force the dictionary to be deleted otherwise it'll crash when libc++ @@ -40,4 +43,4 @@ void destroySmartDashboardData() { } } -} // namespace rpy +} // namespace rpy diff --git a/wpilibc/src/main/python/wpilib/src/rpy/SmartDashboardData.h b/wpilibc/src/main/python/wpilib/src/rpy/SmartDashboardData.h index 6ec19f63b2..b2e1264d99 100644 --- a/wpilibc/src/main/python/wpilib/src/rpy/SmartDashboardData.h +++ b/wpilibc/src/main/python/wpilib/src/rpy/SmartDashboardData.h @@ -1,17 +1,21 @@ #pragma once -#include "wpi/util/sendable/Sendable.hpp" +#include + #include +#include "wpi/util/sendable/Sendable.hpp" + namespace rpy { // // These functions must be called with the GIL held // -void addSmartDashboardData(py::str &key, std::shared_ptr data); +void addSmartDashboardData(py::str& key, + std::shared_ptr data); void clearSmartDashboardData(); void destroySmartDashboardData(); -} // namespace rpy \ No newline at end of file +} // namespace rpy diff --git a/wpimath/src/main/python/semiwrap/ChassisAccelerations.yml b/wpimath/src/main/python/semiwrap/ChassisAccelerations.yml index e10b8074fc..1e69e5fa61 100644 --- a/wpimath/src/main/python/semiwrap/ChassisAccelerations.yml +++ b/wpimath/src/main/python/semiwrap/ChassisAccelerations.yml @@ -43,7 +43,7 @@ inline_code: | self->ax = ax; } ) - .def_property("ay_fpss", + .def_property("ay_fpss", [](ChassisAccelerations * self) -> wpi::units::feet_per_second_squared_t { return self->ay; }, @@ -51,7 +51,7 @@ inline_code: | self->ay = ay; } ) - .def_property("alpha_dpss", + .def_property("alpha_dpss", [](ChassisAccelerations * self) -> wpi::units::degrees_per_second_squared_t { return self->alpha; }, diff --git a/wpimath/src/main/python/semiwrap/ChassisVelocities.yml b/wpimath/src/main/python/semiwrap/ChassisVelocities.yml index bce6fbba28..a86efd559e 100644 --- a/wpimath/src/main/python/semiwrap/ChassisVelocities.yml +++ b/wpimath/src/main/python/semiwrap/ChassisVelocities.yml @@ -45,7 +45,7 @@ inline_code: | self->vx = vx; } ) - .def_property("vy_fps", + .def_property("vy_fps", [](ChassisVelocities * self) -> wpi::units::feet_per_second_t { return self->vy; }, @@ -53,7 +53,7 @@ inline_code: | self->vy = vy; } ) - .def_property("omega_dps", + .def_property("omega_dps", [](ChassisVelocities * self) -> wpi::units::degrees_per_second_t { return self->omega; }, diff --git a/wpimath/src/main/python/semiwrap/DifferentialDriveWheelAccelerations.yml b/wpimath/src/main/python/semiwrap/DifferentialDriveWheelAccelerations.yml index 8b5b139ebd..6b08512185 100644 --- a/wpimath/src/main/python/semiwrap/DifferentialDriveWheelAccelerations.yml +++ b/wpimath/src/main/python/semiwrap/DifferentialDriveWheelAccelerations.yml @@ -36,7 +36,7 @@ inline_code: | self->left = left; } ) - .def_property("right_fpss", + .def_property("right_fpss", [](DifferentialDriveWheelAccelerations * self) -> wpi::units::feet_per_second_squared_t { return self->right; }, diff --git a/wpimath/src/main/python/semiwrap/DifferentialDriveWheelVelocities.yml b/wpimath/src/main/python/semiwrap/DifferentialDriveWheelVelocities.yml index f9f25b403c..8a4161ef99 100644 --- a/wpimath/src/main/python/semiwrap/DifferentialDriveWheelVelocities.yml +++ b/wpimath/src/main/python/semiwrap/DifferentialDriveWheelVelocities.yml @@ -35,7 +35,7 @@ inline_code: | self->left = left; } ) - .def_property("right_fps", + .def_property("right_fps", [](DifferentialDriveWheelVelocities * self) -> wpi::units::feet_per_second_t { return self->right; }, @@ -50,4 +50,3 @@ inline_code: | ; SetupWPyStruct(cls_DifferentialDriveWheelVelocities); - diff --git a/wpimath/src/main/python/semiwrap/MecanumDriveWheelAccelerations.yml b/wpimath/src/main/python/semiwrap/MecanumDriveWheelAccelerations.yml index 58c99dcf47..fa7fca0848 100644 --- a/wpimath/src/main/python/semiwrap/MecanumDriveWheelAccelerations.yml +++ b/wpimath/src/main/python/semiwrap/MecanumDriveWheelAccelerations.yml @@ -29,9 +29,9 @@ inline_code: | >(), py::arg("frontLeft") = 0, py::arg("frontRight") = 0, py::arg("rearLeft") = 0, py::arg("rearRight") = 0 ) - .def_static("fromFps", [](wpi::units::feet_per_second_squared_t frontLeft, - wpi::units::feet_per_second_squared_t frontRight, - wpi::units::feet_per_second_squared_t rearLeft, + .def_static("fromFps", [](wpi::units::feet_per_second_squared_t frontLeft, + wpi::units::feet_per_second_squared_t frontRight, + wpi::units::feet_per_second_squared_t rearLeft, wpi::units::feet_per_second_squared_t rearRight){ return MecanumDriveWheelAccelerations{frontLeft, frontRight, rearLeft, rearRight}; }, py::arg("frontLeft") = 0, py::arg("frontRight") = 0, py::arg("rearLeft") = 0, py::arg("rearRight") = 0) @@ -43,7 +43,7 @@ inline_code: | self->frontLeft = frontLeft; } ) - .def_property("front_right_fpss", + .def_property("front_right_fpss", [](MecanumDriveWheelAccelerations * self) -> wpi::units::feet_per_second_squared_t { return self->frontRight; }, @@ -59,7 +59,7 @@ inline_code: | self->rearLeft = rearLeft; } ) - .def_property("rear_right_fpss", + .def_property("rear_right_fpss", [](MecanumDriveWheelAccelerations * self) -> wpi::units::feet_per_second_squared_t { return self->rearRight; }, diff --git a/wpimath/src/main/python/semiwrap/MecanumDriveWheelVelocities.yml b/wpimath/src/main/python/semiwrap/MecanumDriveWheelVelocities.yml index c88da7ac0b..bda98052d8 100644 --- a/wpimath/src/main/python/semiwrap/MecanumDriveWheelVelocities.yml +++ b/wpimath/src/main/python/semiwrap/MecanumDriveWheelVelocities.yml @@ -46,7 +46,7 @@ inline_code: | self->frontLeft = fps; } ) - .def_property("frontRight_fps", + .def_property("frontRight_fps", [](MecanumDriveWheelVelocities * self) -> wpi::units::feet_per_second_t { return self->frontRight; }, @@ -62,7 +62,7 @@ inline_code: | self->rearLeft = fps; } ) - .def_property("rearRight_fps", + .def_property("rearRight_fps", [](MecanumDriveWheelVelocities * self) -> wpi::units::feet_per_second_t { return self->rearRight; }, diff --git a/wpimath/src/main/python/semiwrap/TrajectoryConfig.yml b/wpimath/src/main/python/semiwrap/TrajectoryConfig.yml index 5cc5d6c950..c00a9340dc 100644 --- a/wpimath/src/main/python/semiwrap/TrajectoryConfig.yml +++ b/wpimath/src/main/python/semiwrap/TrajectoryConfig.yml @@ -41,7 +41,7 @@ inline_code: |- "\n" ":param kinematics: The swerve drive kinematics.") ) - + .def("setKinematics", static_cast&)>( &wpi::math::TrajectoryConfig::SetKinematics<3>), py::arg("kinematics"), release_gil(), py::doc( @@ -50,7 +50,7 @@ inline_code: |- "\n" ":param kinematics: The swerve drive kinematics.") ) - + .def("setKinematics", static_cast&)>( &wpi::math::TrajectoryConfig::SetKinematics<4>), py::arg("kinematics"), release_gil(), py::doc( @@ -59,7 +59,7 @@ inline_code: |- "\n" ":param kinematics: The swerve drive kinematics.") ) - + .def("setKinematics", static_cast&)>( &wpi::math::TrajectoryConfig::SetKinematics<6>), py::arg("kinematics"), release_gil(), py::doc( diff --git a/wpimath/src/main/python/semiwrap/TrajectoryGenerator.yml b/wpimath/src/main/python/semiwrap/TrajectoryGenerator.yml index 4e2db0bd4b..df9cdd0afe 100644 --- a/wpimath/src/main/python/semiwrap/TrajectoryGenerator.yml +++ b/wpimath/src/main/python/semiwrap/TrajectoryGenerator.yml @@ -19,4 +19,3 @@ classes: - [CubicHermiteSpline] - [QuinticHermiteSpline] SetErrorHandler: - diff --git a/wpimath/src/main/python/semiwrap/Twist2d.yml b/wpimath/src/main/python/semiwrap/Twist2d.yml index 149815bbd0..af22e9a2f9 100644 --- a/wpimath/src/main/python/semiwrap/Twist2d.yml +++ b/wpimath/src/main/python/semiwrap/Twist2d.yml @@ -22,7 +22,7 @@ inline_code: | .def_static("fromFeet", [](wpi::units::foot_t dx, wpi::units::foot_t dy, wpi::units::radian_t dtheta){ return Twist2d{dx, dy, dtheta}; }, py::arg("dx") = 0, py::arg("dy") = 0, py::arg("dtheta") = 0) - .def_property("dx_feet", + .def_property("dx_feet", [](Twist2d * self) -> wpi::units::foot_t { return self->dx; }, @@ -30,7 +30,7 @@ inline_code: | self->dx = dx; } ) - .def_property("dy_feet", + .def_property("dy_feet", [](Twist2d * self) -> wpi::units::foot_t { return self->dy; }, @@ -38,7 +38,7 @@ inline_code: | self->dy = dy; } ) - .def_property("dtheta_degrees", + .def_property("dtheta_degrees", [](Twist2d * self) -> wpi::units::degree_t { return self->dtheta; }, diff --git a/wpimath/src/main/python/semiwrap/Twist3d.yml b/wpimath/src/main/python/semiwrap/Twist3d.yml index d2fece48e0..3810cd1de1 100644 --- a/wpimath/src/main/python/semiwrap/Twist3d.yml +++ b/wpimath/src/main/python/semiwrap/Twist3d.yml @@ -30,7 +30,7 @@ inline_code: |- }, py::arg("dx") = 0, py::arg("dy") = 0, py::arg("dz") = 0, py::arg("rx") = 0, py::arg("ry") = 0, py::arg("rz") = 0) - .def_property("dx_feet", + .def_property("dx_feet", [](Twist3d * self) -> wpi::units::foot_t { return self->dx; }, @@ -38,7 +38,7 @@ inline_code: |- self->dx = dx; } ) - .def_property("dy_feet", + .def_property("dy_feet", [](Twist3d * self) -> wpi::units::foot_t { return self->dy; }, @@ -46,7 +46,7 @@ inline_code: |- self->dy = dy; } ) - .def_property("dz_feet", + .def_property("dz_feet", [](Twist3d * self) -> wpi::units::foot_t { return self->dz; }, @@ -54,7 +54,7 @@ inline_code: |- self->dz = dz; } ) - .def_property("rx_degrees", + .def_property("rx_degrees", [](Twist3d * self) -> wpi::units::degree_t { return self->rx; }, @@ -62,7 +62,7 @@ inline_code: |- self->rx = rx; } ) - .def_property("ry_degrees", + .def_property("ry_degrees", [](Twist3d * self) -> wpi::units::degree_t { return self->ry; }, @@ -70,7 +70,7 @@ inline_code: |- self->ry = ry; } ) - .def_property("rz_degrees", + .def_property("rz_degrees", [](Twist3d * self) -> wpi::units::degree_t { return self->rz; }, diff --git a/wpimath/src/main/python/wpimath/_impl/src/PyTrajectoryConstraint.h b/wpimath/src/main/python/wpimath/_impl/src/PyTrajectoryConstraint.h index 977ac6a8aa..f500f0c5a3 100644 --- a/wpimath/src/main/python/wpimath/_impl/src/PyTrajectoryConstraint.h +++ b/wpimath/src/main/python/wpimath/_impl/src/PyTrajectoryConstraint.h @@ -1,37 +1,43 @@ #pragma once -#include "wpi/math/trajectory/constraint/TrajectoryConstraint.hpp" +#include +#include + #include +#include "wpi/math/trajectory/constraint/TrajectoryConstraint.hpp" + namespace wpi::math { struct PyTrajectoryConstraint : public TrajectoryConstraint { - PyTrajectoryConstraint() {} - wpi::units::meters_per_second_t - MaxVelocity(const Pose2d &pose, wpi::units::curvature_t curvature, - wpi::units::meters_per_second_t velocity) const override { + wpi::units::meters_per_second_t MaxVelocity( + const Pose2d& pose, wpi::units::curvature_t curvature, + wpi::units::meters_per_second_t velocity) const override { return m_constraint->MaxVelocity(pose, curvature, velocity); } - MinMax MinMaxAcceleration(const Pose2d &pose, wpi::units::curvature_t curvature, - wpi::units::meters_per_second_t velocity) const override { + MinMax MinMaxAcceleration( + const Pose2d& pose, wpi::units::curvature_t curvature, + wpi::units::meters_per_second_t velocity) const override { return m_constraint->MinMaxAcceleration(pose, curvature, velocity); } std::shared_ptr m_constraint; }; -}; // namespace wpi::math +}; // namespace wpi::math -namespace pybind11 { -namespace detail { +namespace pybind11::detail { -template <> struct type_caster { - using value_conv = make_caster>; +template <> +struct type_caster { + using value_conv = + make_caster>; - PYBIND11_TYPE_CASTER(wpi::math::PyTrajectoryConstraint, _("wpimath._wpimath.TrajectoryConstraint")); + PYBIND11_TYPE_CASTER(wpi::math::PyTrajectoryConstraint, + _("wpimath._wpimath.TrajectoryConstraint")); bool load(handle src, bool convert) { value_conv conv; @@ -40,15 +46,15 @@ template <> struct type_caster { } value.m_constraint = - cast_op>(std::move(conv)); + cast_op>( + std::move(conv)); return true; } - static handle cast(const wpi::math::PyTrajectoryConstraint &src, + static handle cast(const wpi::math::PyTrajectoryConstraint& src, return_value_policy policy, handle parent) { return value_conv::cast(src.m_constraint, policy, parent); } }; -}; // namespace detail -}; // namespace pybind11 +} // namespace pybind11::detail diff --git a/wpimath/src/main/python/wpimath/_impl/src/geometryToString.h b/wpimath/src/main/python/wpimath/_impl/src/geometryToString.h index 7ef82e799b..e5014e95b8 100644 --- a/wpimath/src/main/python/wpimath/_impl/src/geometryToString.h +++ b/wpimath/src/main/python/wpimath/_impl/src/geometryToString.h @@ -1,3 +1,5 @@ +#pragma once + #include #include "wpi/math/geometry/Ellipse2d.hpp" @@ -11,11 +13,11 @@ namespace rpy { -inline std::string toString(const wpi::math::Rotation2d &self) { +inline std::string toString(const wpi::math::Rotation2d& self) { return "Rotation2d(" + std::to_string(self.Radians()()) + ")"; } -inline std::string toString(const wpi::math::Rotation3d &self) { +inline std::string toString(const wpi::math::Rotation3d& self) { return "Rotation3d(" "x=" + std::to_string(self.X()()) + @@ -27,7 +29,7 @@ inline std::string toString(const wpi::math::Rotation3d &self) { std::to_string(self.Z()()) + ")"; } -inline std::string toString(const wpi::math::Translation2d &self) { +inline std::string toString(const wpi::math::Translation2d& self) { return "Translation2d(" "x=" + std::to_string(self.X()()) + @@ -36,7 +38,7 @@ inline std::string toString(const wpi::math::Translation2d &self) { std::to_string(self.Y()()) + ")"; } -inline std::string toString(const wpi::math::Translation3d &self) { +inline std::string toString(const wpi::math::Translation3d& self) { return "Translation3d(" "x=" + std::to_string(self.X()()) + @@ -48,7 +50,7 @@ inline std::string toString(const wpi::math::Translation3d &self) { std::to_string(self.Z()()) + ")"; } -inline std::string toString(const wpi::math::Quaternion &self) { +inline std::string toString(const wpi::math::Quaternion& self) { return "Quaternion(" "w=" + std::to_string(self.W()) + @@ -63,37 +65,36 @@ inline std::string toString(const wpi::math::Quaternion &self) { std::to_string(self.Z()) + ")"; } -inline std::string toString(const wpi::math::Transform2d &self) { +inline std::string toString(const wpi::math::Transform2d& self) { return "Transform2d(" + rpy::toString(self.Translation()) + ", " + rpy::toString(self.Rotation()) + ")"; } -inline std::string toString(const wpi::math::Transform3d &self) { +inline std::string toString(const wpi::math::Transform3d& self) { return "Transform3d(" + rpy::toString(self.Translation()) + ", " + rpy::toString(self.Rotation()) + ")"; } -inline std::string toString(const wpi::math::Pose2d &self) { +inline std::string toString(const wpi::math::Pose2d& self) { return "Pose2d(" + rpy::toString(self.Translation()) + ", " + rpy::toString(self.Rotation()) + ")"; } -inline std::string toString(const wpi::math::Pose3d &self) { +inline std::string toString(const wpi::math::Pose3d& self) { return "Pose3d(" + rpy::toString(self.Translation()) + ", " + rpy::toString(self.Rotation()) + ")"; } -inline std::string toString(const wpi::math::Rectangle2d &self) { +inline std::string toString(const wpi::math::Rectangle2d& self) { return "Rectangle2d(center=" + rpy::toString(self.Center()) + - ", xWidth=" + std::to_string(self.XWidth()()) + - ", yWidth=" + std::to_string(self.YWidth()()) + ")"; + ", xWidth=" + std::to_string(self.XWidth()()) + + ", yWidth=" + std::to_string(self.YWidth()()) + ")"; } -inline std::string toString(const wpi::math::Ellipse2d &self) { +inline std::string toString(const wpi::math::Ellipse2d& self) { return "Ellipse2d(center=" + rpy::toString(self.Center()) + - ", xSemiAxis=" + std::to_string(self.XSemiAxis()()) + - ", ySemiAxis=" + std::to_string(self.YSemiAxis()()) + ")"; + ", xSemiAxis=" + std::to_string(self.XSemiAxis()()) + + ", ySemiAxis=" + std::to_string(self.YSemiAxis()()) + ")"; } - -} // namespace rpy +} // namespace rpy diff --git a/wpimath/src/main/python/wpimath/src/wpimath.cpp b/wpimath/src/main/python/wpimath/src/wpimath.cpp index 15fff17025..6ecd42b290 100644 --- a/wpimath/src/main/python/wpimath/src/wpimath.cpp +++ b/wpimath/src/main/python/wpimath/src/wpimath.cpp @@ -1,4 +1,6 @@ #include "semiwrap_init.wpimath._wpimath.hpp" -SEMIWRAP_PYBIND11_MODULE(m) { initWrapper(m); } +SEMIWRAP_PYBIND11_MODULE(m) { + initWrapper(m); +} diff --git a/wpimath/src/test/python/cpp/wpimath_test/include/module.h b/wpimath/src/test/python/cpp/wpimath_test/include/module.h index 3e2b18876b..5eef33e01d 100644 --- a/wpimath/src/test/python/cpp/wpimath_test/include/module.h +++ b/wpimath/src/test/python/cpp/wpimath_test/include/module.h @@ -1,24 +1,24 @@ #pragma once -#include "wpi/units/time.hpp" #include "wpi/units/length.hpp" +#include "wpi/units/time.hpp" struct SomeClass { - static constexpr auto s_constant = 2_s; - static constexpr auto ms_constant1 = 20_ms; - static constexpr wpi::units::second_t ms_constant2 = 50_ms; - static constexpr wpi::units::millisecond_t ms_constant3 = 0.20_s; + static constexpr auto s_constant = 2_s; + static constexpr auto ms_constant1 = 20_ms; + static constexpr wpi::units::second_t ms_constant2 = 50_ms; + static constexpr wpi::units::millisecond_t ms_constant3 = 0.20_s; - bool checkDefaultByName1(wpi::units::second_t period = ms_constant1); - bool checkDefaultByName2(wpi::units::second_t period = ms_constant2); - bool checkDefaultByNum1(wpi::units::second_t period = 50_ms); - bool checkDefaultByNum2(wpi::units::second_t period = 0.5_s); + bool checkDefaultByName1(wpi::units::second_t period = ms_constant1); + bool checkDefaultByName2(wpi::units::second_t period = ms_constant2); + bool checkDefaultByNum1(wpi::units::second_t period = 50_ms); + bool checkDefaultByNum2(wpi::units::second_t period = 0.5_s); - wpi::units::second_t ms2s(wpi::units::millisecond_t ms); - wpi::units::millisecond_t s2ms(wpi::units::second_t s); + wpi::units::second_t ms2s(wpi::units::millisecond_t ms); + wpi::units::millisecond_t s2ms(wpi::units::second_t s); - static constexpr wpi::units::foot_t five_ft = 5_ft; + static constexpr wpi::units::foot_t five_ft = 5_ft; - wpi::units::meter_t ft2m(wpi::units::foot_t f); -}; \ No newline at end of file + wpi::units::meter_t ft2m(wpi::units::foot_t f); +}; diff --git a/wpimath/src/test/python/cpp/wpimath_test/src/module.cpp b/wpimath/src/test/python/cpp/wpimath_test/src/module.cpp index 129fdb402b..7dbde2a90f 100644 --- a/wpimath/src/test/python/cpp/wpimath_test/src/module.cpp +++ b/wpimath/src/test/python/cpp/wpimath_test/src/module.cpp @@ -1,53 +1,50 @@ -#include "semiwrap_init.wpimath_test._wpimath_test.hpp" -#include #include -SEMIWRAP_PYBIND11_MODULE(m) -{ - initWrapper(m); +#include + +#include "semiwrap_init.wpimath_test._wpimath_test.hpp" + +SEMIWRAP_PYBIND11_MODULE(m) { + initWrapper(m); } -bool SomeClass::checkDefaultByName1(wpi::units::second_t period) -{ - if (period != SomeClass::ms_constant1) { - throw std::runtime_error(wpi::units::to_string(period)); - } - return true; +bool SomeClass::checkDefaultByName1(wpi::units::second_t period) { + if (period != SomeClass::ms_constant1) { + throw std::runtime_error(wpi::units::to_string(period)); + } + return true; } -bool SomeClass::checkDefaultByName2(wpi::units::second_t period) -{ - if (period != SomeClass::ms_constant2) { - throw std::runtime_error(wpi::units::to_string(period)); - } - return true; +bool SomeClass::checkDefaultByName2(wpi::units::second_t period) { + if (period != SomeClass::ms_constant2) { + throw std::runtime_error(wpi::units::to_string(period)); + } + return true; } -bool SomeClass::checkDefaultByNum1(wpi::units::second_t period) -{ - if (period != 50_ms) { - throw std::runtime_error(wpi::units::to_string(period)); - } - return true; +bool SomeClass::checkDefaultByNum1(wpi::units::second_t period) { + if (period != 50_ms) { + throw std::runtime_error(wpi::units::to_string(period)); + } + return true; } -bool SomeClass::checkDefaultByNum2(wpi::units::second_t period) -{ - if (period != 50_ms) { - throw std::runtime_error(wpi::units::to_string(period)); - } - return true; +bool SomeClass::checkDefaultByNum2(wpi::units::second_t period) { + if (period != 50_ms) { + throw std::runtime_error(wpi::units::to_string(period)); + } + return true; } wpi::units::meter_t SomeClass::ft2m(wpi::units::foot_t f) { - return f; + return f; } wpi::units::second_t SomeClass::ms2s(wpi::units::millisecond_t ms) { - return ms; + return ms; } wpi::units::millisecond_t SomeClass::s2ms(wpi::units::second_t s) { - return s; + return s; } diff --git a/wpinet/src/main/python/wpinet/src/main.cpp b/wpinet/src/main/python/wpinet/src/main.cpp index 0c1b0adabf..af08afa07e 100644 --- a/wpinet/src/main/python/wpinet/src/main.cpp +++ b/wpinet/src/main/python/wpinet/src/main.cpp @@ -1,4 +1,6 @@ -#include +#include "semiwrap_init.wpinet._wpinet.hpp" -SEMIWRAP_PYBIND11_MODULE(m) { initWrapper(m); } +SEMIWRAP_PYBIND11_MODULE(m) { + initWrapper(m); +} diff --git a/wpiutil/src/main/python/semiwrap/RawFrame.yml b/wpiutil/src/main/python/semiwrap/RawFrame.yml index 43649627ee..2ab69876a8 100644 --- a/wpiutil/src/main/python/semiwrap/RawFrame.yml +++ b/wpiutil/src/main/python/semiwrap/RawFrame.yml @@ -1,3 +1,2 @@ defaults: ignore: true - diff --git a/wpiutil/src/main/python/wpiutil/src/main.cpp b/wpiutil/src/main/python/wpiutil/src/main.cpp index c540274fd2..c4127784dd 100644 --- a/wpiutil/src/main/python/wpiutil/src/main.cpp +++ b/wpiutil/src/main/python/wpiutil/src/main.cpp @@ -1,5 +1,5 @@ -#include +#include "semiwrap_init.wpiutil._wpiutil.hpp" void setup_stack_trace_hook(py::object fn); void cleanup_stack_trace_hook(); @@ -14,7 +14,7 @@ void cleanup_now_impl(); namespace wpi::util::impl { void ResetSendableRegistry(); -} // namespace wpi::util::impl +} // namespace wpi::util::impl void cleanup_sendable_registry() { py::gil_scoped_release unlock; @@ -31,7 +31,7 @@ SEMIWRAP_PYBIND11_MODULE(m) { initWrapper(m); static int unused; - py::capsule cleanup(&unused, [](void *) { + py::capsule cleanup(&unused, [](void*) { cleanup_sendable_registry(); cleanup_stack_trace_hook(); cleanup_safethread_gil(); diff --git a/wpiutil/src/main/python/wpiutil/src/safethread_gil.cpp b/wpiutil/src/main/python/wpiutil/src/safethread_gil.cpp index 4f6d439373..9713af9ba1 100644 --- a/wpiutil/src/main/python/wpiutil/src/safethread_gil.cpp +++ b/wpiutil/src/main/python/wpiutil/src/safethread_gil.cpp @@ -1,29 +1,30 @@ #include + #include #include -using OnThreadStartFn = void *(*)(); -using OnThreadEndFn = void (*)(void *); +using OnThreadStartFn = void* (*)(); +using OnThreadEndFn = void (*)(void*); namespace wpi::util::impl { void SetSafeThreadNotifiers(OnThreadStartFn OnStart, OnThreadEndFn OnEnd); } struct SafeThreadState { - py::gil_scoped_acquire *acquire = nullptr; - py::gil_scoped_release *release = nullptr; + py::gil_scoped_acquire* acquire = nullptr; + py::gil_scoped_release* release = nullptr; }; std::atomic g_gilstate_managed = false; -void *on_safe_thread_start() { - if (Py_IsFinalizing() // python is shutting down - || !g_gilstate_managed.load() // python has shutdown) +void* on_safe_thread_start() { + if (Py_IsFinalizing() // python is shutting down + || !g_gilstate_managed.load() // python has shutdown) ) { return nullptr; } - auto *st = new SafeThreadState; + auto* st = new SafeThreadState; // acquires the GIL and creates pybind11's thread state for this thread st->acquire = new py::gil_scoped_acquire; @@ -33,20 +34,20 @@ void *on_safe_thread_start() { return st; } -void on_safe_thread_end(void *opaque) { +void on_safe_thread_end(void* opaque) { // on entry, GIL should not be acquired // don't cleanup if it's unsafe to do so. Several possibilities here: - if (!opaque // internal error? - || Py_IsFinalizing() // python is shutting down - || !g_gilstate_managed.load() // python has shutdown + if (!opaque // internal error? + || Py_IsFinalizing() // python is shutting down + || !g_gilstate_managed.load() // python has shutdown ) { return; } - auto *st = (SafeThreadState *)opaque; - delete st->release; // causes GIL to be acquired - delete st->acquire; // causes GIL to be released and thread state deleted + auto* st = reinterpret_cast(opaque); + delete st->release; // causes GIL to be acquired + delete st->acquire; // causes GIL to be released and thread state deleted delete st; } @@ -59,7 +60,10 @@ void setup_safethread_gil() { atexit.attr("register")( py::cpp_function([]() { g_gilstate_managed = false; })); - wpi::util::impl::SetSafeThreadNotifiers(on_safe_thread_start, on_safe_thread_end); + wpi::util::impl::SetSafeThreadNotifiers(on_safe_thread_start, + on_safe_thread_end); } -void cleanup_safethread_gil() { g_gilstate_managed = false; } +void cleanup_safethread_gil() { + g_gilstate_managed = false; +} diff --git a/wpiutil/src/main/python/wpiutil/src/stacktracehook.cpp b/wpiutil/src/main/python/wpiutil/src/stacktracehook.cpp index ab56e1db6e..1012e13d1c 100644 --- a/wpiutil/src/main/python/wpiutil/src/stacktracehook.cpp +++ b/wpiutil/src/main/python/wpiutil/src/stacktracehook.cpp @@ -1,14 +1,18 @@ +#include + #include + #include "wpi/util/StackTrace.hpp" -py::object &get_hook_ref() { +py::object& get_hook_ref() { static py::object hook; return hook; } std::string final_py_stack_trace_hook(int offset) { - std::string msg = "\tat \n"; + std::string msg = + "\tat \n"; msg += wpi::util::GetStackTraceDefault(offset); return msg; } @@ -17,11 +21,11 @@ std::string py_stack_trace_hook(int offset) { py::gil_scoped_acquire gil; try { - auto &hook = get_hook_ref(); + auto& hook = get_hook_ref(); if (hook) { return py::cast(hook(offset)); } - } catch (py::error_already_set &e) { + } catch (py::error_already_set& e) { e.discard_as_unraisable("wpiutil._stacktrace._stack_trace_hook"); } @@ -37,9 +41,9 @@ void cleanup_stack_trace_hook() { wpi::util::SetGetStackTraceImpl(final_py_stack_trace_hook); // release the function during interpreter shutdown - auto &hook = get_hook_ref(); + auto& hook = get_hook_ref(); if (hook) { hook.dec_ref(); hook.release(); } -} \ No newline at end of file +} diff --git a/wpiutil/src/main/python/wpiutil/src/timestamp.cpp b/wpiutil/src/main/python/wpiutil/src/timestamp.cpp index 8ba04b0b6d..17a9ac592e 100644 --- a/wpiutil/src/main/python/wpiutil/src/timestamp.cpp +++ b/wpiutil/src/main/python/wpiutil/src/timestamp.cpp @@ -1,11 +1,11 @@ -#include -#include - #include "wpi/util/timestamp.hpp" +#include +#include + namespace py = pybind11; -py::object &get_now_impl_ref() { +py::object& get_now_impl_ref() { static py::object get_now_impl_ref; return get_now_impl_ref; } @@ -14,11 +14,11 @@ py::object &get_now_impl_ref() { uint64_t now_impl_trampoline() { py::gil_scoped_acquire acquire; try { - auto &hook = get_now_impl_ref(); + auto& hook = get_now_impl_ref(); if (hook) { return hook().cast(); } - } catch (py::error_already_set &e) { + } catch (py::error_already_set& e) { e.discard_as_unraisable("wpiutil.now_impl_trampoline"); } @@ -26,21 +26,21 @@ uint64_t now_impl_trampoline() { } void set_now_impl(py::object func) { - get_now_impl_ref() = func; - if (func.is_none()) { - wpi::util::SetNowImpl(nullptr); - } else { - wpi::util::SetNowImpl(&now_impl_trampoline); - } + get_now_impl_ref() = func; + if (func.is_none()) { + wpi::util::SetNowImpl(nullptr); + } else { + wpi::util::SetNowImpl(&now_impl_trampoline); + } } void cleanup_now_impl() { wpi::util::SetNowImpl(nullptr); // release the function during interpreter shutdown - auto &hook = get_now_impl_ref(); + auto& hook = get_now_impl_ref(); if (hook) { hook.dec_ref(); hook.release(); } -} \ No newline at end of file +} diff --git a/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct.h b/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct.h index 7b11b244a9..ba94438c1c 100644 --- a/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct.h +++ b/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct.h @@ -3,17 +3,18 @@ #include #include +#include #include #include -#include "wpi/util/struct/Struct.hpp" - #include #include #include -static inline std::string pytypename(const py::type &t) { - return ((PyTypeObject *)t.ptr())->tp_name; +#include "wpi/util/struct/Struct.hpp" + +static inline std::string pytypename(const py::type& t) { + return (reinterpret_cast(t.ptr()))->tp_name; } // @@ -23,15 +24,14 @@ static inline std::string pytypename(const py::type &t) { // This merely holds the python object being operated on, the actual // serialization work is done in WPyStructConverter struct WPyStruct { - WPyStruct() = default; - WPyStruct(const WPyStruct &other) { + WPyStruct(const WPyStruct& other) { py::gil_scoped_acquire gil; py = other.py; } - WPyStruct &operator=(const WPyStruct &other) { + WPyStruct& operator=(const WPyStruct& other) { { py::gil_scoped_acquire gil; py = other.py; @@ -39,9 +39,9 @@ struct WPyStruct { return *this; } - WPyStruct(WPyStruct &&) = default; + WPyStruct(WPyStruct&&) = default; - WPyStruct(const py::object &py) : py(py) {} + explicit WPyStruct(const py::object& py) : py(py) {} ~WPyStruct() { py::gil_scoped_acquire gil; @@ -51,10 +51,10 @@ struct WPyStruct { py::object py; }; -namespace pybind11 { -namespace detail { +namespace pybind11::detail { -template <> struct type_caster { +template <> +struct type_caster { // TODO: wpiutil.struct.T/TV? PYBIND11_TYPE_CASTER(WPyStruct, const_name("object")); @@ -64,7 +64,7 @@ template <> struct type_caster { return true; } - static handle cast(const WPyStruct &src, py::return_value_policy policy, + static handle cast(const WPyStruct& src, py::return_value_policy policy, py::handle parent) { py::handle v = src.py; v.inc_ref(); @@ -72,8 +72,7 @@ template <> struct type_caster { } }; -} // namespace detail -} // namespace pybind11 +} // namespace pybind11::detail // // Struct info class implementation @@ -88,7 +87,7 @@ struct WPyStructConverter { virtual std::string_view GetSchema() const = 0; - virtual void Pack(std::span data, const WPyStruct &value) const = 0; + virtual void Pack(std::span data, const WPyStruct& value) const = 0; virtual WPyStruct Unpack(std::span data) const = 0; @@ -96,12 +95,13 @@ struct WPyStructConverter { // std::span data) const = 0; virtual void ForEachNested( - const std::function &fn) + const std::function& fn) const = 0; }; // static C++ converter -template struct WPyStructCppConverter : WPyStructConverter { +template +struct WPyStructCppConverter : WPyStructConverter { std::string_view GetTypeName() const override { return wpi::util::Struct::GetTypeName(); } @@ -112,9 +112,9 @@ template struct WPyStructCppConverter : WPyStructConverter { return wpi::util::Struct::GetSchema(); } - void Pack(std::span data, const WPyStruct &value) const override { + void Pack(std::span data, const WPyStruct& value) const override { py::gil_scoped_acquire gil; - const T &v = value.py.cast(); + const T& v = value.py.cast(); wpi::util::Struct::Pack(data, v); } @@ -131,7 +131,7 @@ template struct WPyStructCppConverter : WPyStructConverter { // } void ForEachNested( - const std::function &fn) + const std::function& fn) const override { if constexpr (wpi::util::HasNestedStruct) { wpi::util::Struct::ForEachNested(fn); @@ -139,13 +139,13 @@ template struct WPyStructCppConverter : WPyStructConverter { } }; -template void SetupWPyStruct(auto pycls) { - - auto *sptr = +template +void SetupWPyStruct(auto pycls) { + auto* sptr = new std::shared_ptr(new WPyStructCppConverter()); - py::capsule c(sptr, "WPyStruct", [](void *ptr) { - delete (std::shared_ptr *)ptr; + py::capsule c(sptr, "WPyStruct", [](void* ptr) { + delete (std::shared_ptr*)ptr; }); pycls.def_property_readonly_static("WPIStruct", @@ -154,8 +154,7 @@ template void SetupWPyStruct(auto pycls) { // dynamic python converter struct WPyStructPyConverter : WPyStructConverter { - - WPyStructPyConverter(py::object o) { + explicit WPyStructPyConverter(py::object o) { m_typename = o.attr("typename").cast(); m_schema = o.attr("schema").cast(); m_size = o.attr("size").cast(); @@ -163,7 +162,8 @@ struct WPyStructPyConverter : WPyStructConverter { m_pack = py::reinterpret_borrow(o.attr("pack")); m_packInto = py::reinterpret_borrow(o.attr("packInto")); m_unpack = py::reinterpret_borrow(o.attr("unpack")); - // m_unpackInto = py::reinterpret_borrow(o.attr("unpackInto")); + // m_unpackInto = + // py::reinterpret_borrow(o.attr("unpackInto")); m_forEachNested = py::reinterpret_borrow(o.attr("forEachNested")); } @@ -177,7 +177,7 @@ struct WPyStructPyConverter : WPyStructConverter { py::function m_packInto; py::function m_unpack; // py::function m_unpackInto; - py::function m_forEachNested; // might be none + py::function m_forEachNested; // might be none std::string_view GetTypeName() const override { return m_typename; } @@ -185,7 +185,7 @@ struct WPyStructPyConverter : WPyStructConverter { std::string_view GetSchema() const override { return m_schema; } - void Pack(std::span data, const WPyStruct &value) const override { + void Pack(std::span data, const WPyStruct& value) const override { py::gil_scoped_acquire gil; py::bytes result = m_pack(value.py); std::string_view rview = result; @@ -196,13 +196,13 @@ struct WPyStructPyConverter : WPyStructConverter { throw py::value_error(msg); } - rview.copy((char *)data.data(), rview.size()); + rview.copy(reinterpret_cast(data.data()), rview.size()); } WPyStruct Unpack(std::span data) const override { py::gil_scoped_acquire gil; auto view = - py::memoryview::from_memory((const void *)data.data(), data.size()); + py::memoryview::from_memory((const void*)data.data(), data.size()); return WPyStruct(m_unpack(view)); } @@ -215,7 +215,7 @@ struct WPyStructPyConverter : WPyStructConverter { // } void ForEachNested( - const std::function &fn) + const std::function& fn) const override { py::gil_scoped_acquire gil; if (!m_forEachNested.is_none()) { @@ -227,9 +227,8 @@ struct WPyStructPyConverter : WPyStructConverter { // passed as I... to the wpi::util::Struct methods struct WPyStructInfo { WPyStructInfo() = default; - WPyStructInfo(const py::type &t) { + explicit WPyStructInfo(const py::type& t) { if (!py::hasattr(t, "WPIStruct")) { - throw py::type_error( fmt::format("{} is not struct serializable (does not have WPIStruct)", pytypename(t))); @@ -238,9 +237,9 @@ struct WPyStructInfo { py::object s = t.attr("WPIStruct"); // C++ version - void *c = PyCapsule_GetPointer(s.ptr(), "WPyStruct"); + void* c = PyCapsule_GetPointer(s.ptr(), "WPyStruct"); if (c != NULL) { - cvt = *(std::shared_ptr *)c; + cvt = *(std::shared_ptr*)c; return; } @@ -249,7 +248,7 @@ struct WPyStructInfo { // Python version try { cvt = std::make_shared(s); - } catch (py::error_already_set &e) { + } catch (py::error_already_set& e) { std::string msg = fmt::format( "{} is not struct serializable (invalid WPIStruct)", pytypename(t)); py::raise_from(e, PyExc_TypeError, msg.c_str()); @@ -257,10 +256,11 @@ struct WPyStructInfo { } } - WPyStructInfo(const WPyStruct &v) : WPyStructInfo(py::type::of(v.py)) {} + explicit WPyStructInfo(const WPyStruct& v) + : WPyStructInfo(py::type::of(v.py)) {} const WPyStructConverter* operator->() const { - const auto *c = cvt.get(); + const auto* c = cvt.get(); if (c == nullptr) { // TODO: would be nice to have a better error here, but we don't have // a good way to know our current context @@ -269,27 +269,26 @@ struct WPyStructInfo { return c; } -private: + private: // holds something used to do serialization std::shared_ptr cvt; }; // Leverages the converter stored in WPyStructInfo to do the actual work -template <> struct wpi::util::Struct { - static std::string_view GetTypeName(const WPyStructInfo &info) { +template <> +struct wpi::util::Struct { + static std::string_view GetTypeName(const WPyStructInfo& info) { return info->GetTypeName(); } - static size_t GetSize(const WPyStructInfo &info) { - return info->GetSize(); - } + static size_t GetSize(const WPyStructInfo& info) { return info->GetSize(); } - static std::string_view GetSchema(const WPyStructInfo &info) { + static std::string_view GetSchema(const WPyStructInfo& info) { return info->GetSchema(); } static WPyStruct Unpack(std::span data, - const WPyStructInfo &info) { + const WPyStructInfo& info) { return info->Unpack(data); } @@ -298,14 +297,14 @@ template <> struct wpi::util::Struct { // info->UnpackInto(v, data); // } - static void Pack(std::span data, const WPyStruct &value, - const WPyStructInfo &info) { + static void Pack(std::span data, const WPyStruct& value, + const WPyStructInfo& info) { info->Pack(data, value); } - static void - ForEachNested(std::invocable auto fn, - const WPyStructInfo &info) { + static void ForEachNested( + std::invocable auto fn, + const WPyStructInfo& info) { info->ForEachNested(fn); } }; @@ -314,4 +313,5 @@ static_assert(wpi::util::StructSerializable); static_assert(wpi::util::HasNestedStruct); // This breaks on readonly structs, so we disable for now -// static_assert(wpi::util::MutableStructSerializable); +// static_assert(wpi::util::MutableStructSerializable); diff --git a/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct_fns.cpp b/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct_fns.cpp index 739d56a801..3d57394378 100644 --- a/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct_fns.cpp +++ b/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct_fns.cpp @@ -1,51 +1,53 @@ +#include "wpystruct_fns.h" + #include "wpystruct.h" void forEachNested( - const py::type &t, - const std::function &fn) { + const py::type& t, + const std::function& fn) { WPyStructInfo info(t); wpi::util::ForEachStructSchema(fn, info); } -py::str getTypeName(const py::type &t) { +py::str getTypeName(const py::type& t) { WPyStructInfo info(t); return wpi::util::GetStructTypeName(info); } -py::str getSchema(const py::type &t) { +py::str getSchema(const py::type& t) { WPyStructInfo info(t); return wpi::util::GetStructSchema(info); } -size_t getSize(const py::type &t) { +size_t getSize(const py::type& t) { WPyStructInfo info(t); return wpi::util::GetStructSize(info); } -py::bytes pack(const WPyStruct &v) { +py::bytes pack(const WPyStruct& v) { WPyStructInfo info(v); auto sz = wpi::util::GetStructSize(info); - PyObject *b = PyBytes_FromStringAndSize(NULL, sz); + PyObject* b = PyBytes_FromStringAndSize(NULL, sz); if (b == NULL) { throw py::error_already_set(); } - char *pybuf; + char* pybuf; py::ssize_t pysz; if (PyBytes_AsStringAndSize(b, &pybuf, &pysz) != 0) { Py_DECREF(b); throw py::error_already_set(); } - auto s = std::span((uint8_t *)pybuf, pysz); + auto s = std::span(reinterpret_cast(pybuf), pysz); wpi::util::PackStruct(s, v, info); return py::reinterpret_steal(b); } -py::bytes packArray(const py::sequence &seq) { +py::bytes packArray(const py::sequence& seq) { auto len = seq.size(); if (len == 0) { return {}; @@ -53,14 +55,14 @@ py::bytes packArray(const py::sequence &seq) { WPyStructInfo info(py::type::of(seq[0])); auto sz = wpi::util::GetStructSize(info); - auto total = sz*len; + auto total = sz * len; - PyObject *b = PyBytes_FromStringAndSize(NULL, total); + PyObject* b = PyBytes_FromStringAndSize(NULL, total); if (b == NULL) { throw py::error_already_set(); } - char *pybuf; + char* pybuf; py::ssize_t pysz; if (PyBytes_AsStringAndSize(b, &pybuf, &pysz) != 0) { Py_DECREF(b); @@ -69,9 +71,9 @@ py::bytes packArray(const py::sequence &seq) { auto bytes_obj = py::reinterpret_steal(b); - for (const auto &v: seq) { + for (const auto& v : seq) { WPyStruct wv(v); - auto s = std::span((uint8_t *)pybuf, sz); + auto s = std::span(reinterpret_cast(pybuf), sz); wpi::util::PackStruct(s, wv, info); pybuf += sz; } @@ -79,7 +81,7 @@ py::bytes packArray(const py::sequence &seq) { return bytes_obj; } -void packInto(const WPyStruct &v, py::buffer &b) { +void packInto(const WPyStruct& v, py::buffer& b) { WPyStructInfo info(v); py::ssize_t sz = wpi::util::GetStructSize(info); @@ -94,11 +96,11 @@ void packInto(const WPyStruct &v, py::buffer &b) { throw py::value_error("buffer must be " + std::to_string(sz) + " bytes"); } - auto s = std::span((uint8_t *)req.ptr, req.size); + auto s = std::span(reinterpret_cast(req.ptr), req.size); wpi::util::PackStruct(s, v, info); } -WPyStruct unpack(const py::type &t, const py::buffer &b) { +WPyStruct unpack(const py::type& t, const py::buffer& b) { WPyStructInfo info(t); py::ssize_t sz = wpi::util::GetStructSize(info); @@ -113,11 +115,12 @@ WPyStruct unpack(const py::type &t, const py::buffer &b) { throw py::value_error("buffer must be " + std::to_string(sz) + " bytes"); } - auto s = std::span((const uint8_t *)req.ptr, req.size); + auto s = std::span(reinterpret_cast(req.ptr), req.size); return wpi::util::UnpackStruct(s, info); } -py::typing::List unpackArray(const py::type &t, const py::buffer &b) { +py::typing::List unpackArray(const py::type& t, + const py::buffer& b) { WPyStructInfo info(t); py::ssize_t sz = wpi::util::GetStructSize(info); @@ -129,12 +132,13 @@ py::typing::List unpackArray(const py::type &t, const py::buffer &b) } if (req.size % sz != 0) { - throw py::value_error("buffer must be multiple of " + std::to_string(sz) + " bytes"); + throw py::value_error("buffer must be multiple of " + std::to_string(sz) + + " bytes"); } auto items = req.size / sz; py::list a(items); - const uint8_t *ptr = (const uint8_t *)req.ptr; + const uint8_t* ptr = reinterpret_cast(req.ptr); for (py::ssize_t i = 0; i < items; i++) { auto s = std::span(ptr, sz); auto v = wpi::util::UnpackStruct(s, info); diff --git a/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct_fns.h b/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct_fns.h index f03f81463c..b43e0564a6 100644 --- a/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct_fns.h +++ b/wpiutil/src/main/python/wpiutil/src/wpistruct/wpystruct_fns.h @@ -7,50 +7,50 @@ Call a function to retrieve the (type string, schema) for each nested struct */ void forEachNested( - const py::type &t, - const std::function &fn); + const py::type& t, + const std::function& fn); /** Retrieve the type name for the specified type */ -py::str getTypeName(const py::type &t); +py::str getTypeName(const py::type& t); /** Retrieve schema for the specified type */ -py::str getSchema(const py::type &t); +py::str getSchema(const py::type& t); /** Returns the serialized size in bytes */ -size_t getSize(const py::type &t); +size_t getSize(const py::type& t); /** Serialize object into byte buffer */ -py::bytes pack(const WPyStruct &v); +py::bytes pack(const WPyStruct& v); /** Serialize objects into byte buffer */ -py::bytes packArray(const py::sequence &seq); +py::bytes packArray(const py::sequence& seq); /** Serialize object into byte buffer. Buffer must be exact size. */ -void packInto(const WPyStruct &v, py::buffer &b); +void packInto(const WPyStruct& v, py::buffer& b); /** Convert byte buffer into object of specified type. Buffer must be exact size. */ -WPyStruct unpack(const py::type &t, const py::buffer &b); +WPyStruct unpack(const py::type& t, const py::buffer& b); /** Convert byte buffer into list of objects of specified type. Buffer must be exact size. */ -py::typing::List unpackArray(const py::type &t, const py::buffer &b); +py::typing::List unpackArray(const py::type& t, const py::buffer& b); // /** // Convert byte buffer into passed in object. Buffer must be exact diff --git a/wpiutil/src/test/python/cpp/wpiutil_test/module.cpp b/wpiutil/src/test/python/cpp/wpiutil_test/module.cpp index 2cef396bc3..21c0cb9bf1 100644 --- a/wpiutil/src/test/python/cpp/wpiutil_test/module.cpp +++ b/wpiutil/src/test/python/cpp/wpiutil_test/module.cpp @@ -13,6 +13,8 @@ #include #include +#include +#include #include @@ -178,7 +180,6 @@ StructWithWPI_String cast_struct_with_wpi_string() { } PYBIND11_MODULE(module, m) { - sendable_test(m); struct_test(m); @@ -209,7 +210,7 @@ PYBIND11_MODULE(module, m) { m.def("load_stringmap_int", &load_stringmap_int); m.def("cast_stringmap", &cast_stringmap); // JSON - m.def("cast_json_arg", &cast_json_arg); + m.def("cast_json_arg", &cast_json_arg); m.def("cast_json_val", &cast_json_val); m.attr("max_uint64") = std::numeric_limits::max(); m.attr("max_int64") = std::numeric_limits::max(); @@ -220,7 +221,7 @@ PYBIND11_MODULE(module, m) { // WPI_String m.def("load_wpi_string", &load_wpi_string); m.def("cast_wpi_string", &cast_wpi_string); - + py::class_ structWithWpiStringCls(m, "StructWithWPI_String"); structWithWpiStringCls.def_readwrite("x", &StructWithWPI_String::x); structWithWpiStringCls.def_readonly("str", &StructWithWPI_String::str); diff --git a/wpiutil/src/test/python/cpp/wpiutil_test/sendable_test.cpp b/wpiutil/src/test/python/cpp/wpiutil_test/sendable_test.cpp index 6f28fb85e0..599fa01f7f 100644 --- a/wpiutil/src/test/python/cpp/wpiutil_test/sendable_test.cpp +++ b/wpiutil/src/test/python/cpp/wpiutil_test/sendable_test.cpp @@ -1,15 +1,20 @@ +#include +#include +#include + #include #include #include + #include "wpi/util/sendable/SendableBuilder.hpp" #include "wpi/util/sendable/SendableRegistry.hpp" class MySendableBuilder : public wpi::util::SendableBuilder { -public: - MySendableBuilder(py::dict keys) : keys(keys) {} + public: + explicit MySendableBuilder(py::dict keys) : keys(keys) {} - ~MySendableBuilder() { + ~MySendableBuilder() override { // leak this so the python interpreter doesn't crash on shutdown keys.release(); } @@ -42,9 +47,9 @@ public: void PublishConstDouble(std::string_view key, double value) override {} - void - AddStringProperty(std::string_view key, std::function getter, - std::function setter) override {} + void AddStringProperty( + std::string_view key, std::function getter, + std::function setter) override {} void PublishConstString(std::string_view key, std::string_view value) override {} @@ -94,44 +99,48 @@ public: void AddSmallStringProperty( std::string_view key, - std::function &buf)> getter, + std::function& buf)> + getter, std::function setter) override {} void AddSmallBooleanArrayProperty( std::string_view key, - std::function(wpi::util::SmallVectorImpl &buf)> + std::function(wpi::util::SmallVectorImpl& buf)> getter, std::function)> setter) override {} void AddSmallIntegerArrayProperty( std::string_view key, std::function< - std::span(wpi::util::SmallVectorImpl &buf)> + std::span(wpi::util::SmallVectorImpl& buf)> getter, std::function)> setter) override {} void AddSmallFloatArrayProperty( std::string_view key, - std::function(wpi::util::SmallVectorImpl &buf)> + std::function< + std::span(wpi::util::SmallVectorImpl& buf)> getter, std::function)> setter) override {} void AddSmallDoubleArrayProperty( std::string_view key, - std::function(wpi::util::SmallVectorImpl &buf)> + std::function< + std::span(wpi::util::SmallVectorImpl& buf)> getter, std::function)> setter) override {} void AddSmallStringArrayProperty( std::string_view key, - std::function< - std::span(wpi::util::SmallVectorImpl &buf)> + std::function( + wpi::util::SmallVectorImpl& buf)> getter, std::function)> setter) override {} void AddSmallRawProperty( std::string_view key, std::string_view typeString, - std::function(wpi::util::SmallVectorImpl &buf)> + std::function< + std::span(wpi::util::SmallVectorImpl& buf)> getter, std::function)> setter) override {} @@ -151,4 +160,6 @@ void Publish(wpi::util::SendableRegistry::UID sendableUid, py::dict keys) { wpi::util::SendableRegistry::Publish(sendableUid, std::move(builder)); } -void sendable_test(py::module &m) { m.def("publish", Publish); } \ No newline at end of file +void sendable_test(py::module& m) { + m.def("publish", Publish); +} diff --git a/wpiutil/src/test/python/cpp/wpiutil_test/struct_test.cpp b/wpiutil/src/test/python/cpp/wpiutil_test/struct_test.cpp index 89080e75c8..acdc1d4452 100644 --- a/wpiutil/src/test/python/cpp/wpiutil_test/struct_test.cpp +++ b/wpiutil/src/test/python/cpp/wpiutil_test/struct_test.cpp @@ -8,33 +8,34 @@ struct ThingA { ThingA() = default; - ThingA(int x) : x(x) {} + explicit ThingA(int x) : x(x) {} const int x = 0; - bool operator==(const ThingA &other) const { return x == other.x; } + bool operator==(const ThingA& other) const { return x == other.x; } }; -template <> struct wpi::util::Struct { +template <> +struct wpi::util::Struct { static constexpr std::string_view GetTypeName() { return "ThingA"; } static constexpr size_t GetSize() { return 1; } static constexpr std::string_view GetSchema() { return "uint8 value"; } static ThingA Unpack(std::span data) { return ThingA{data[0]}; } - static void Pack(std::span data, const ThingA &value) { + static void Pack(std::span data, const ThingA& value) { data[0] = value.x; } }; struct Outer { Outer() = default; - Outer(const ThingA &t, int c) : inner(t), c(c) {} + Outer(const ThingA& t, int c) : inner(t), c(c) {} ThingA inner; int c = 0; - bool operator==(const Outer &other) const { + bool operator==(const Outer& other) const { return inner == other.inner && c == other.c; } }; @@ -42,7 +43,9 @@ struct Outer { template <> struct wpi::util::Struct { static constexpr std::string_view GetTypeName() { return "Outer"; } - static constexpr size_t GetSize() { return wpi::util::GetStructSize() + 4; } + static constexpr size_t GetSize() { + return wpi::util::GetStructSize() + 4; + } static constexpr std::string_view GetSchema() { return "ThingA inner; int32 c"; } @@ -63,8 +66,7 @@ struct wpi::util::Struct { } }; -void struct_test(py::module &m) { - +void struct_test(py::module& m) { py::class_ thingCls(m, "ThingA"); thingCls.def(py::init<>()); thingCls.def(py::init()); @@ -81,4 +83,4 @@ void struct_test(py::module &m) { outerCls.def(py::self == py::self); SetupWPyStruct(outerCls); -} \ No newline at end of file +} diff --git a/xrpVendordep/src/main/python/xrp/src/main.cpp b/xrpVendordep/src/main/python/xrp/src/main.cpp index 002441a180..b14a20b7a5 100644 --- a/xrpVendordep/src/main/python/xrp/src/main.cpp +++ b/xrpVendordep/src/main/python/xrp/src/main.cpp @@ -1,6 +1,6 @@ -#include +#include "semiwrap_init.xrp._xrp.hpp" SEMIWRAP_PYBIND11_MODULE(m) { - initWrapper(m); -} \ No newline at end of file + initWrapper(m); +} diff --git a/xrpVendordep/src/main/python/xrp/xrp.pc b/xrpVendordep/src/main/python/xrp/xrp.pc index cf61cff01f..a75c6d8206 100644 --- a/xrpVendordep/src/main/python/xrp/xrp.pc +++ b/xrpVendordep/src/main/python/xrp/xrp.pc @@ -4,6 +4,6 @@ pkgconf_pypi_initpy=xrp._init__xrp Name: xrp Description: semiwrap pybind11 module -Version: +Version: Cflags: -I${prefix} Requires: robotpy-native-xrp wpilib wpimath