• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pybricks / pybricks-micropython / 19016791193

02 Nov 2025 06:40PM UTC coverage: 57.167% (-2.6%) from 59.744%
19016791193

Pull #406

github

laurensvalk
bricks/virtualhub: Replace with embedded simulation.

Instead of using the newly introduced simhub alongside the virtualhub, we'll just replace the old one entirely now that it has reached feature parity. We can keep calling it the virtualhub.
Pull Request #406: New virtual hub for more effective debugging

41 of 48 new or added lines in 7 files covered. (85.42%)

414 existing lines in 53 files now uncovered.

4479 of 7835 relevant lines covered (57.17%)

17178392.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

9.3
/pybricks/util_pb/pb_error.c
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2018-2022 The Pybricks Authors
3

4
#include "py/mpconfig.h"
5
#include "py/mperrno.h"
6
#include "py/obj.h"
7
#include "py/objstr.h"
8
#include "py/runtime.h"
9

10
#include <pybricks/util_pb/pb_error.h>
11

12
/**
13
 * Raise an exception if @p error is not ::PBIO_SUCCESS. Most errors translate
14
 * to an OSError with the appropriate error code. There are a few special
15
 * cases that use another built-in python exception when it is more appropriate.
16
 */
17
void pb_assert(pbio_error_t error) {
283✔
18
    #if PYBRICKS_OPT_TERSE_ERR
19
    // using EINVAL to mean that the argument to this function was invalid.
20
    // since we raise ValueError for PBIO_ERROR_INVALID_ARG, there isn't a
21
    // possible conflict
22
    int os_err = MP_EINVAL;
23

24
    switch (error) {
25
        case PBIO_SUCCESS:
26
            return;
27
        case PBIO_ERROR_FAILED:
28
            mp_raise_msg(&mp_type_RuntimeError, NULL);
29
            __builtin_unreachable();
30
        case PBIO_ERROR_INVALID_ARG:
31
            mp_raise_ValueError(NULL);
32
            __builtin_unreachable();
33
        case PBIO_ERROR_NOT_IMPLEMENTED:
34
            mp_raise_NotImplementedError(NULL);
35
            __builtin_unreachable();
36
        case PBIO_ERROR_IO:
37
            os_err = MP_EIO;
38
            break;
39
        case PBIO_ERROR_BUSY:
40
            os_err = MP_EBUSY;
41
            break;
42
        case PBIO_ERROR_NO_DEV:
43
            os_err = MP_ENODEV;
44
            break;
45
        case PBIO_ERROR_NOT_SUPPORTED:
46
            os_err = MP_EOPNOTSUPP;
47
            break;
48
        case PBIO_ERROR_AGAIN:
49
            os_err = MP_EAGAIN;
50
            break;
51
        case PBIO_ERROR_INVALID_OP:
52
            os_err = MP_EPERM;
53
            break;
54
        case PBIO_ERROR_TIMEDOUT:
55
            os_err = MP_ETIMEDOUT;
56
            break;
57
        case PBIO_ERROR_CANCELED:
58
            os_err = MP_ECANCELED;
59
            break;
60
    }
61

62
    mp_raise_OSError(os_err);
63
    __builtin_unreachable();
64
    #else // PYBRICKS_OPT_TERSE_ERR
65
    static const MP_DEFINE_STR_OBJ(msg_io_obj, "\n\n"
66
        "Unexpected hardware input/output error with a motor or sensor:\n"
67
        "--> Try unplugging the sensor or motor and plug it back in again.\n"
68
        "--> To see which sensor or motor is causing the problem,\n"
69
        "    check the line in your script that matches\n"
70
        "    the line number given in the 'Traceback' above.\n"
71
        "--> Try rebooting the hub/brick if the problem persists.\n"
72
        "\n");
73
    static const MP_DEFINE_STR_OBJ(msg_no_dev_obj, "\n\n"
74
        "A sensor or motor is not connected to the specified port:\n"
75
        "--> Check the cables to each motor and sensor.\n"
76
        "--> Check the port settings in your script.\n"
77
        "--> Check the line in your script that matches\n"
78
        "    the line number given in the 'Traceback' above.\n"
79
        "\n");
80
    static const MP_DEFINE_STR_OBJ(msg_not_supported_obj, "\n\n"
81
        "The requested operation is not supported on this device:\n"
82
        "--> Check the documentation for device compatibility.\n"
83
        "--> Check the line in your script that matches\n"
84
        "    the line number given in the 'Traceback' above.\n"
85
        "\n");
86
    static const MP_DEFINE_STR_OBJ(msg_invalid_op_obj, "\n\n"
87
        "The requested operation is not valid in the current state:\n"
88
        "--> Check the documentation for required conditions.\n"
89
        "--> Check the line in your script that matches\n"
90
        "    the line number given in the 'Traceback' above.\n"
91
        "\n");
92

93
    mp_obj_t args[2];
94

95
    switch (error) {
283✔
96
        case PBIO_SUCCESS:
283✔
97
            return;
283✔
98
        case PBIO_ERROR_FAILED:
×
99
            mp_raise_msg(&mp_type_RuntimeError, (mp_rom_error_text_t)pbio_error_str(error));
×
100
            __builtin_unreachable();
UNCOV
101
        case PBIO_ERROR_INVALID_ARG:
×
UNCOV
102
            mp_raise_ValueError((mp_rom_error_text_t)pbio_error_str(error));
×
103
            __builtin_unreachable();
104
        case PBIO_ERROR_NOT_IMPLEMENTED:
×
105
            mp_raise_NotImplementedError((mp_rom_error_text_t)pbio_error_str(error));
×
106
            __builtin_unreachable();
107
        case PBIO_ERROR_IO:
×
108
            args[0] = MP_OBJ_NEW_SMALL_INT(MP_EIO);
×
109
            args[1] = MP_OBJ_FROM_PTR(&msg_io_obj);
×
110
            break;
×
111
        case PBIO_ERROR_BUSY:
×
112
            args[0] = MP_OBJ_NEW_SMALL_INT(MP_EBUSY);
×
113
            args[1] = MP_OBJ_NEW_QSTR(qstr_from_str(pbio_error_str(error)));
×
114
            break;
×
115
        case PBIO_ERROR_NO_DEV:
×
116
            args[0] = MP_OBJ_NEW_SMALL_INT(MP_ENODEV);
×
117
            args[1] = MP_OBJ_FROM_PTR(&msg_no_dev_obj);
×
118
            break;
×
119
        case PBIO_ERROR_NOT_SUPPORTED:
×
120
            args[0] = MP_OBJ_NEW_SMALL_INT(MP_EOPNOTSUPP);
×
121
            args[1] = MP_OBJ_FROM_PTR(&msg_not_supported_obj);
×
122
            break;
×
123
        case PBIO_ERROR_AGAIN:
×
124
            args[0] = MP_OBJ_NEW_SMALL_INT(MP_EAGAIN);
×
125
            args[1] = MP_OBJ_NEW_QSTR(qstr_from_str(pbio_error_str(error)));
×
126
            break;
×
127
        case PBIO_ERROR_INVALID_OP:
×
128
            args[0] = MP_OBJ_NEW_SMALL_INT(MP_EPERM);
×
129
            args[1] = MP_OBJ_FROM_PTR(&msg_invalid_op_obj);
×
130
            break;
×
131
        case PBIO_ERROR_TIMEDOUT:
×
132
            args[0] = MP_OBJ_NEW_SMALL_INT(MP_ETIMEDOUT);
×
133
            args[1] = MP_OBJ_NEW_QSTR(qstr_from_str(pbio_error_str(error)));
×
134
            break;
×
135
        case PBIO_ERROR_CANCELED:
×
136
            args[0] = MP_OBJ_NEW_SMALL_INT(MP_ECANCELED);
×
137
            args[1] = MP_OBJ_NEW_QSTR(qstr_from_str(pbio_error_str(error)));
×
138
            break;
×
139
    }
140

141
    nlr_raise(mp_obj_new_exception_args(&mp_type_OSError, 2, args));
×
142
    __builtin_unreachable();
143
    #endif // PYBRICKS_OPT_TERSE_ERR
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc