Skip to content

Commit 4d38575

Browse files
authored
Merge pull request #129 from robotpy/fix-bytes
Fix sendError related bugs
2 parents ecb8f64 + 9a82c41 commit 4d38575

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

subprojects/robotpy-hal/hal/src/hal.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,29 @@ RPYBUILD_PYBIND11_MODULE(m) {
6666
#else
6767
m.attr("__halplatform__") = "sim";
6868
m.attr("__hal_simulation__") = true;
69+
70+
m.def("__test_senderr", []() {
71+
HAL_SendError(1, 2, 0, "\xfa" "badmessage", "location", "callstack", 1);
72+
}, release_gil());
73+
6974
#endif
7075

7176
// Redirect stderr to python stderr
7277
sys_module = py::module_::import("sys");
7378

7479
HAL_SetPrintErrorImpl([](const char *line, size_t size) {
80+
if (size == 0) {
81+
return;
82+
}
83+
7584
py::gil_scoped_acquire lock;
76-
py::print(py::str(line, size), "file"_a=sys_module.attr("stderr"));
85+
PyObject *o = PyUnicode_DecodeUTF8(line, size, "replace");
86+
if (o == nullptr) {
87+
PyErr_Clear();
88+
py::print(py::bytes(line, size), "file"_a=sys_module.attr("stderr"));
89+
} else {
90+
py::print(py::reinterpret_steal<py::str>(o), "file"_a=sys_module.attr("stderr"));
91+
}
7792
});
7893

7994
// Do cleanup on module unload

subprojects/robotpy-hal/tests/test_hal.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ def test_hal_simdevice():
99

1010
v.set(4)
1111
assert v.get() == 4
12+
13+
14+
def test_hal_send_error(capsys):
15+
hal._wpiHal.__test_senderr()
16+
cap = capsys.readouterr()
17+
assert cap.err == "Error at location: �badmessage\ncallstack\n\n"

subprojects/robotpy-wpilib/wpilib/_impl/report_error.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ def reportErrorInternal(
6262
not isWarning,
6363
code,
6464
False,
65-
error.encode("utf-8"),
66-
locString.encode("utf-8"),
67-
traceString.encode("utf-8"),
65+
error,
66+
locString,
67+
traceString,
6868
True,
6969
)
7070

0 commit comments

Comments
 (0)