Skip to content

Commit ff9a24f

Browse files
committed
Merge remote-tracking branch 'upstream/master' into list_count
2 parents a58cf67 + 32f8841 commit ff9a24f

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

py/objstr.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,20 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
4141
int len = strlen(lhs_str);
4242
if (start < 0) {
4343
start = len + start;
44+
if (start < 0) {
45+
start = 0;
46+
}
47+
} else if (start > len) {
48+
start = len;
4449
}
4550
if (stop <= 0) {
4651
stop = len + stop;
52+
// CPython returns empty string in such case
53+
if (stop < 0) {
54+
stop = start;
55+
}
56+
} else if (stop > len) {
57+
stop = len;
4758
}
4859
return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start));
4960
#endif

stm/storage.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ static uint8_t *cache_get_addr_for_write(uint32_t flash_addr) {
4949
return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;
5050
}
5151

52+
static uint8_t *cache_get_addr_for_read(uint32_t flash_addr) {
53+
uint32_t flash_sector_start;
54+
uint32_t flash_sector_size;
55+
uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
56+
if (cache_flash_sector_id == flash_sector_id) {
57+
// in cache, copy from there
58+
return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;
59+
}
60+
// not in cache, copy straight from flash
61+
return (uint8_t*)flash_addr;
62+
}
63+
5264
void storage_init(void) {
5365
if (!is_initialised) {
5466
cache_flash_sector_id = 0;
@@ -131,8 +143,9 @@ bool storage_read_block(uint8_t *dest, uint32_t block) {
131143
return true;
132144

133145
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) {
134-
// non-MBR block, just copy straight from flash
135-
uint8_t *src = (uint8_t*)FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * BLOCK_SIZE;
146+
// non-MBR block, get data from flash memory, possibly via cache
147+
uint32_t flash_addr = FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * BLOCK_SIZE;
148+
uint8_t *src = cache_get_addr_for_read(flash_addr);
136149
memcpy(dest, src, BLOCK_SIZE);
137150
return true;
138151

tests/basics/tests/slice-bstr1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
print(b"123"[:0])
2323
print(b"123"[:-3])
2424
print(b"123"[:-4])
25+
# Range check testing, don't segfault, please ;-)
26+
print(b"123"[:1000000])
27+
print(b"123"[1000000:])
28+
print(b"123"[:-1000000])
29+
print(b"123"[-1000000:])
2530
# No IndexError!
2631
print(b""[1:1])
2732
print(b""[-1:-1])

0 commit comments

Comments
 (0)