Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
56beb017
Commit
56beb017
authored
10 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
objarray: Support assignment of bytes to bytearray slice.
parent
9a18e210
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
py/objarray.c
+23
-9
23 additions, 9 deletions
py/objarray.c
tests/basics/bytearray_slice_assign.py
+6
-0
6 additions, 0 deletions
tests/basics/bytearray_slice_assign.py
with
29 additions
and
9 deletions
py/objarray.c
+
23
−
9
View file @
56beb017
...
...
@@ -369,17 +369,31 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
if
(
value
!=
MP_OBJ_SENTINEL
)
{
#if MICROPY_PY_ARRAY_SLICE_ASSIGN
// Assign
if
(
!
MP_OBJ_IS_TYPE
(
value
,
&
mp_type_array
)
&&
!
MP_OBJ_IS_TYPE
(
value
,
&
mp_type_bytearray
))
{
mp_not_implemented
(
"array required on right side"
);
}
mp_obj_array_t
*
src_slice
=
value
;
mp_uint_t
src_len
;
void
*
src_items
;
int
item_sz
=
mp_binary_get_size
(
'@'
,
o
->
typecode
,
NULL
);
if
(
MP_OBJ_IS_TYPE
(
value
,
&
mp_type_array
)
||
MP_OBJ_IS_TYPE
(
value
,
&
mp_type_bytearray
))
{
mp_obj_array_t
*
src_slice
=
value
;
if
(
item_sz
!=
mp_binary_get_size
(
'@'
,
src_slice
->
typecode
,
NULL
))
{
mp_not_implemented
(
"arrays should be compatible"
);
compat_error:
mp_not_implemented
(
"lhs and rhs should be compatible"
);
}
src_len
=
src_slice
->
len
;
src_items
=
src_slice
->
items
;
}
else
if
(
MP_OBJ_IS_TYPE
(
value
,
&
mp_type_bytes
))
{
if
(
item_sz
!=
1
)
{
goto
compat_error
;
}
mp_buffer_info_t
bufinfo
;
mp_get_buffer_raise
(
value
,
&
bufinfo
,
MP_BUFFER_READ
);
src_len
=
bufinfo
.
len
;
src_items
=
bufinfo
.
buf
;
}
else
{
mp_not_implemented
(
"array/bytes required on right side"
);
}
// TODO: check src/dst compat
mp_int_t
len_adj
=
src_
slice
->
len
-
(
slice
.
stop
-
slice
.
start
);
mp_int_t
len_adj
=
src_len
-
(
slice
.
stop
-
slice
.
start
);
if
(
len_adj
>
0
)
{
if
(
len_adj
>
o
->
free
)
{
// TODO: alloc policy; at the moment we go conservative
...
...
@@ -387,10 +401,10 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
o
->
free
=
0
;
}
mp_seq_replace_slice_grow_inplace
(
o
->
items
,
o
->
len
,
slice
.
start
,
slice
.
stop
,
src_
slice
->
items
,
src_
slice
->
len
,
len_adj
,
item_sz
);
slice
.
start
,
slice
.
stop
,
src_items
,
src_len
,
len_adj
,
item_sz
);
}
else
{
mp_seq_replace_slice_no_grow
(
o
->
items
,
o
->
len
,
slice
.
start
,
slice
.
stop
,
src_
slice
->
items
,
src_
slice
->
len
,
item_sz
);
slice
.
start
,
slice
.
stop
,
src_items
,
src_len
,
item_sz
);
// Clear "freed" elements at the end of list
// TODO: This is actually only needed for typecode=='O'
mp_seq_clear
(
o
->
items
,
o
->
len
+
len_adj
,
o
->
len
,
item_sz
);
...
...
This diff is collapsed.
Click to expand it.
tests/basics/bytearray_slice_assign.py
+
6
−
0
View file @
56beb017
...
...
@@ -46,3 +46,9 @@ print(l)
b
=
bytearray
(
2
)
b
[
2
:]
=
bytearray
(
10
)
print
(
b
)
# Assignment of bytes to array slice
b
=
bytearray
(
2
)
b
[
1
:
1
]
=
b
"
12345
"
print
(
b
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment