Skip to content

Commit

Permalink
Merge pull request #19 from robotpy/fix-sendable-chooser-gil
Browse files Browse the repository at this point in the history
Switch SendableChooser to rpy::gilsafe_object
  • Loading branch information
virtuald authored Nov 5, 2023
2 parents 4aa377e + fa44c26 commit 2c981d8
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 18 deletions.
2 changes: 1 addition & 1 deletion rdev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude_artifacts = [
"opencv-cpp"
]

robotpy_build_req = "<2025.0.0,~=2024.0.0b2"
robotpy_build_req = "<2025.0.0,~=2024.0.0b3"

#
# Subproject configuration
Expand Down
2 changes: 1 addition & 1 deletion subprojects/pyntcore/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install_requires = [

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-wpiutil~=2024.0.0b2",
"robotpy-wpinet~=2024.0.0b2",
]
Expand Down
2 changes: 1 addition & 1 deletion subprojects/robotpy-apriltag/gen/AprilTagDetector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ classes:
py::object py_result = py::cast(c_result);
auto len = c_result->size();
auto get = py_result.attr("get");
py::List<AprilTagDetection> l(len);
py::typing::List<AprilTagDetection> l(len);
for (size_t i = 0; i < len; i++) {
l[i] = get(i);
}
Expand Down
2 changes: 1 addition & 1 deletion subprojects/robotpy-apriltag/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install_requires = [

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-wpiutil~=2024.0.0b2",
"robotpy-wpimath~=2024.0.0b2",
]
Expand Down
2 changes: 1 addition & 1 deletion subprojects/robotpy-cscore/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ install_requires = [

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-wpiutil~=2024.0.0b2",
"robotpy-wpinet~=2024.0.0b2",
"pyntcore~=2024.0.0b2",
Expand Down
2 changes: 1 addition & 1 deletion subprojects/robotpy-hal/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ install_requires = [

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-wpiutil~=2024.0.0b2",
]

Expand Down
2 changes: 1 addition & 1 deletion subprojects/robotpy-halsim-ds-socket/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ robotpysimext = ["ds-socket = halsim_ds_socket"]

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-hal~=2024.0.0b2",
"robotpy-wpinet~=2024.0.0b2",
]
Expand Down
2 changes: 1 addition & 1 deletion subprojects/robotpy-halsim-gui/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ install_requires = [

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-wpiutil~=2024.0.0b2",
"robotpy-wpimath~=2024.0.0b2",
"robotpy-hal~=2024.0.0b2",
Expand Down
2 changes: 1 addition & 1 deletion subprojects/robotpy-halsim-ws/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ robotpysimext = [

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-hal~=2024.0.0b2",
"robotpy-wpinet~=2024.0.0b2",
]
Expand Down
22 changes: 17 additions & 5 deletions subprojects/robotpy-wpilib/gen/SendableChooser.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---

extra_includes:
- gilsafe_object.h

classes:
SendableChooser:
shared_ptr: true
Expand All @@ -9,10 +12,6 @@ classes:
SendableChooser:
AddOption:
SetDefaultOption:
AddObject:
ignore: true
AddDefault:
ignore: true
GetSelected:
# weirdness because return type
cpp_code: |
Expand All @@ -24,10 +23,23 @@ classes:
return v;
}
OnChange:
# more weirdness
cpp_code: |
[](frc::SendableChooser<T> *self, std::function<void(T)> fn) {
self->OnChange([fn](T v) {
py::gil_scoped_acquire lock;
if (v) {
fn(v);
} else {
fn(py::none());
}
});
}
InitSendable:

templates:
SendableChooser:
qualname: frc::SendableChooser
params:
- py::object
- rpy::gilsafe_object
2 changes: 1 addition & 1 deletion subprojects/robotpy-wpilib/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ robotpy = ["run = wpilib._impl.start:Main"]

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-wpiutil~=2024.0.0b2",
"robotpy-wpimath~=2024.0.0b2",
"robotpy-hal~=2024.0.0b2",
Expand Down
23 changes: 23 additions & 0 deletions subprojects/robotpy-wpilib/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging

import pytest
import ntcore
import wpilib


@pytest.fixture
def cfg_logging(caplog):
caplog.set_level(logging.INFO)


@pytest.fixture(scope="function")
def nt(cfg_logging):
instance = ntcore.NetworkTableInstance.getDefault()
instance.startLocal()

try:
yield instance
finally:
instance.stopLocal()
instance._reset()
wpilib._wpilib._clearSmartDashboardData()
55 changes: 55 additions & 0 deletions subprojects/robotpy-wpilib/tests/test_sendable_chooser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest

from ntcore import NetworkTableInstance
from wpilib import SendableChooser, SmartDashboard


@pytest.fixture
def chooser() -> SendableChooser:
chooser = SendableChooser()
for i in range(1, 4):
chooser.addOption(str(i), i)
return chooser


@pytest.mark.parametrize("value", [0, 1, 2, 3])
def test_returns_selected(
nt: NetworkTableInstance, chooser: SendableChooser, value: int
):
chooser.setDefaultOption("0", 0)

with nt.getStringTopic(
"/SmartDashboard/ReturnsSelectedChooser/selected"
).publish() as pub:
SmartDashboard.putData("ReturnsSelectedChooser", chooser)
SmartDashboard.updateValues()
print("set", value)
pub.set(str(value))
SmartDashboard.updateValues()
print("get", chooser.getSelected())
assert value == chooser.getSelected()


def test_default_is_returned_on_no_select(chooser: SendableChooser):
chooser.setDefaultOption("4", 4)
assert 4 == chooser.getSelected()


def test_default_constructable_is_returned_on_no_select_and_no_default(
chooser: SendableChooser,
):
assert chooser.getSelected() is None


def test_change_listener(nt: NetworkTableInstance, chooser: SendableChooser):
current_val = [0]

def on_change(val):
current_val[0] = val

chooser.onChange(on_change)
SmartDashboard.putData("ChangeListenerChooser", chooser)
SmartDashboard.updateValues()
SmartDashboard.putString("ChangeListenerChooser/selected", "3")
SmartDashboard.updateValues()
assert 3 == current_val[0]
2 changes: 1 addition & 1 deletion subprojects/robotpy-wpimath/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ install_requires = [

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-wpiutil~=2024.0.0b2",
# "numpy", # broken in raspbian CI
]
Expand Down
2 changes: 1 addition & 1 deletion subprojects/robotpy-wpinet/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ install_requires = [

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
"robotpy-wpiutil~=2024.0.0b2",
]
2 changes: 1 addition & 1 deletion subprojects/robotpy-wpiutil/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ install_requires = []

[build-system]
requires = [
"robotpy-build<2025.0.0,~=2024.0.0b2",
"robotpy-build<2025.0.0,~=2024.0.0b3",
]

[tool.robotpy-build]
Expand Down

0 comments on commit 2c981d8

Please sign in to comment.