Skip to content

Commit 802628c

Browse files
authored
Merge pull request #7998 from tannewt/fix_mv_cast
Fix memoryview.cast over sliced memoryview
2 parents f2bfced + 177b981 commit 802628c

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
lines changed

locale/circuitpython.pot

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,15 @@ msgstr ""
165165
msgid "%q length must be >= %d"
166166
msgstr ""
167167

168-
#: ports/raspberrypi/common-hal/picodvi/Framebuffer.c py/argcheck.c
168+
#: py/argcheck.c
169169
msgid "%q must be %d"
170170
msgstr ""
171171

172172
#: py/argcheck.c shared-bindings/displayio/Bitmap.c
173+
#: shared-bindings/displayio/Display.c
174+
#: shared-bindings/framebufferio/FramebufferDisplay.c
175+
#: shared-bindings/is31fl3741/FrameBuffer.c
176+
#: shared-bindings/rgbmatrix/RGBMatrix.c
173177
msgid "%q must be %d-%d"
174178
msgstr ""
175179

@@ -203,6 +207,7 @@ msgstr ""
203207

204208
#: ports/raspberrypi/bindings/cyw43/__init__.c py/argcheck.c py/objexcept.c
205209
#: shared-bindings/canio/CAN.c shared-bindings/digitalio/Pull.c
210+
#: shared-module/synthio/Synthesizer.c
206211
msgid "%q must be of type %q or %q, not %q"
207212
msgstr ""
208213

@@ -425,6 +430,7 @@ msgid "Address must be %d bytes long"
425430
msgstr ""
426431

427432
#: ports/espressif/common-hal/memorymap/AddressRange.c
433+
#: ports/nrf/common-hal/memorymap/AddressRange.c
428434
msgid "Address range not allowed"
429435
msgstr ""
430436

@@ -608,13 +614,6 @@ msgstr ""
608614
msgid "Both pins must support hardware interrupts"
609615
msgstr ""
610616

611-
#: shared-bindings/displayio/Display.c
612-
#: shared-bindings/framebufferio/FramebufferDisplay.c
613-
#: shared-bindings/is31fl3741/FrameBuffer.c
614-
#: shared-bindings/rgbmatrix/RGBMatrix.c
615-
msgid "Brightness must be 0-1.0"
616-
msgstr ""
617-
618617
#: shared-bindings/displayio/Display.c
619618
#: shared-bindings/framebufferio/FramebufferDisplay.c
620619
msgid "Brightness not adjustable"
@@ -927,11 +926,6 @@ msgstr ""
927926
msgid "EXTINT channel already in use"
928927
msgstr ""
929928

930-
#: shared-module/synthio/MidiTrack.c
931-
#, c-format
932-
msgid "Error in MIDI stream at position %d"
933-
msgstr ""
934-
935929
#: extmod/modure.c
936930
msgid "Error in regex"
937931
msgstr ""
@@ -1343,11 +1337,6 @@ msgstr ""
13431337
msgid "Mapping must be a tuple"
13441338
msgstr ""
13451339

1346-
#: shared-module/displayio/Shape.c
1347-
#, c-format
1348-
msgid "Maximum x value when mirrored is %d"
1349-
msgstr ""
1350-
13511340
#: shared-bindings/audiobusio/PDMIn.c
13521341
msgid "Microphone startup delay must be in range 0.0 to 1.0"
13531342
msgstr ""
@@ -1441,6 +1430,10 @@ msgstr ""
14411430
msgid "Nimble out of memory"
14421431
msgstr ""
14431432

1433+
#: ports/atmel-samd/common-hal/busio/UART.c
1434+
msgid "No %q pin"
1435+
msgstr ""
1436+
14441437
#: ports/espressif/common-hal/_bleio/Characteristic.c
14451438
#: ports/nrf/common-hal/_bleio/Characteristic.c
14461439
msgid "No CCCD for this Characteristic"
@@ -1489,14 +1482,12 @@ msgstr ""
14891482
msgid "No MOSI pin"
14901483
msgstr ""
14911484

1492-
#: ports/atmel-samd/common-hal/busio/UART.c
14931485
#: ports/espressif/common-hal/busio/UART.c
14941486
#: ports/mimxrt10xx/common-hal/busio/UART.c ports/nrf/common-hal/busio/UART.c
14951487
#: ports/raspberrypi/common-hal/busio/UART.c ports/stm/common-hal/busio/UART.c
14961488
msgid "No RX pin"
14971489
msgstr ""
14981490

1499-
#: ports/atmel-samd/common-hal/busio/UART.c
15001491
#: ports/espressif/common-hal/busio/UART.c
15011492
#: ports/mimxrt10xx/common-hal/busio/UART.c ports/nrf/common-hal/busio/UART.c
15021493
#: ports/raspberrypi/common-hal/busio/UART.c ports/stm/common-hal/busio/UART.c
@@ -2123,8 +2114,8 @@ msgstr ""
21232114
msgid "UART de-init"
21242115
msgstr ""
21252116

2126-
#: ports/atmel-samd/common-hal/busio/UART.c ports/cxd56/common-hal/busio/UART.c
2127-
#: ports/espressif/common-hal/busio/UART.c ports/stm/common-hal/busio/UART.c
2117+
#: ports/cxd56/common-hal/busio/UART.c ports/espressif/common-hal/busio/UART.c
2118+
#: ports/stm/common-hal/busio/UART.c
21282119
msgid "UART init"
21292120
msgstr ""
21302121

@@ -4342,18 +4333,10 @@ msgstr ""
43424333
msgid "wrong output type"
43434334
msgstr ""
43444335

4345-
#: shared-module/displayio/Shape.c
4346-
msgid "x value out of bounds"
4347-
msgstr ""
4348-
43494336
#: ports/espressif/common-hal/audiobusio/__init__.c
43504337
msgid "xTaskCreate failed"
43514338
msgstr ""
43524339

4353-
#: shared-module/displayio/Shape.c
4354-
msgid "y value out of bounds"
4355-
msgstr ""
4356-
43574340
#: extmod/ulab/code/scipy/signal/signal.c
43584341
msgid "zi must be an ndarray"
43594342
msgstr ""

ports/atmel-samd/common-hal/busio/UART.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
241241
}
242242

243243
if (usart_async_init(usart_desc_p, sercom, self->buffer, self->buffer_length, NULL) != ERR_NONE) {
244-
mp_raise_RuntimeError(translate("UART init"));
244+
mp_raise_RuntimeError(NULL);
245245
}
246246

247247
// usart_async_init() sets a number of defaults based on a prototypical SERCOM
@@ -396,7 +396,7 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
396396
// Read characters.
397397
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {
398398
if (self->rx_pin == NO_PIN) {
399-
mp_raise_ValueError(translate("No RX pin"));
399+
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_rx);
400400
}
401401

402402
// This assignment is only here because the usart_async routines take a *const argument.
@@ -453,7 +453,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
453453
// Write characters.
454454
size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
455455
if (self->tx_pin == NO_PIN) {
456-
mp_raise_ValueError(translate("No TX pin"));
456+
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_tx);
457457
}
458458

459459
// This assignment is only here because the usart_async routines take a *const argument.

ports/unix/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
716716
}
717717

718718
#if !MICROPY_VFS
719-
uint mp_import_stat(const char *path) {
719+
mp_import_stat_t mp_import_stat(const char *path) {
720720
struct stat st;
721721
if (stat(path, &st) == 0) {
722722
if (S_ISDIR(st.st_mode)) {

py/objarray.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,14 @@ STATIC mp_obj_t memoryview_make_new(const mp_obj_type_t *type_in, size_t n_args,
254254
STATIC mp_obj_t memoryview_cast(const mp_obj_t self_in, const mp_obj_t typecode_in) {
255255
mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
256256
const char *typecode = mp_obj_str_get_str(typecode_in);
257-
size_t element_size = mp_binary_get_size('@', typecode[0], NULL);
258-
size_t bytelen = self->len * mp_binary_get_size('@', self->typecode & ~MP_OBJ_ARRAY_TYPECODE_FLAG_RW, NULL);
259-
if (bytelen % element_size != 0) {
257+
size_t new_element_size = mp_binary_get_size('@', typecode[0], NULL);
258+
size_t old_element_size = mp_binary_get_size('@', self->typecode & ~MP_OBJ_ARRAY_TYPECODE_FLAG_RW, NULL);
259+
size_t bytelen = self->len * old_element_size;
260+
if (bytelen % new_element_size != 0) {
260261
mp_raise_TypeError(MP_ERROR_TEXT("memoryview: length is not a multiple of itemsize"));
261262
}
262-
mp_obj_array_t *result = MP_OBJ_TO_PTR(mp_obj_new_memoryview(*typecode, bytelen / element_size, self->items));
263+
mp_obj_array_t *result = MP_OBJ_TO_PTR(mp_obj_new_memoryview(*typecode, bytelen / new_element_size, self->items));
264+
result->memview_offset = (self->memview_offset * old_element_size) / new_element_size;
263265

264266
// test if the object can be written to
265267
if (self->typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW) {

tests/basics/memoryview_cast.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
try:
2+
memoryview(b'a').cast
3+
except:
4+
print("SKIP")
5+
raise SystemExit
6+
7+
b = bytearray(range(16))
8+
9+
def print_memview(mv):
10+
print(", ".join(hex(v) for v in mv))
11+
12+
mv = memoryview(b)
13+
print_memview(mv)
14+
print_memview(mv[4:])
15+
16+
words = mv.cast("I")
17+
print_memview(words)
18+
print_memview(mv[4:].cast("I"))
19+
print_memview(words[1:])
20+
21+
print_memview(words.cast("B"))

0 commit comments

Comments
 (0)