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
32ca1649
Commit
32ca1649
authored
10 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Tidy up array.array; add more error handling.
parent
90edf9e1
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
py/objarray.c
+25
-15
25 additions, 15 deletions
py/objarray.c
with
25 additions
and
15 deletions
py/objarray.c
+
25
−
15
View file @
32ca1649
...
...
@@ -35,7 +35,7 @@ STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *en
}
else
{
print
(
env
,
"array('%c'"
,
o
->
typecode
);
if
(
o
->
len
>
0
)
{
print
(
env
,
", ["
,
o
->
typecode
);
print
(
env
,
", ["
);
for
(
int
i
=
0
;
i
<
o
->
len
;
i
++
)
{
if
(
i
>
0
)
{
print
(
env
,
", "
);
...
...
@@ -75,32 +75,37 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
}
STATIC
mp_obj_t
array_make_new
(
mp_obj_t
type_in
,
uint
n_args
,
uint
n_kw
,
const
mp_obj_t
*
args
)
{
if
(
n_args
<
1
||
n_args
>
2
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"unexpected # of arguments, %d given"
,
n_args
));
}
// TODO check args
mp_check_nargs
(
n_args
,
1
,
2
,
n_kw
,
false
);
// get typecode
uint
l
;
const
char
*
typecode
=
mp_obj_str_get_data
(
args
[
0
],
&
l
);
if
(
n_args
==
1
)
{
// 1 arg: make an empty array
return
array_new
(
*
typecode
,
0
);
}
else
{
// 2 args: construct the array from the given iterator
return
array_construct
(
*
typecode
,
args
[
1
]);
}
return
array_construct
(
*
typecode
,
args
[
1
]);
}
STATIC
mp_obj_t
bytearray_make_new
(
mp_obj_t
type_in
,
uint
n_args
,
uint
n_kw
,
const
mp_obj_t
*
args
)
{
if
(
n_args
>
1
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"unexpected # of arguments, %d given"
,
n_args
));
}
mp_check_nargs
(
n_args
,
0
,
1
,
n_kw
,
false
);
if
(
MP_OBJ_IS_SMALL_INT
(
args
[
0
]))
{
if
(
n_args
==
0
)
{
// no args: construct an empty bytearray
return
array_new
(
BYTEARRAY_TYPECODE
,
0
);
}
else
if
(
MP_OBJ_IS_SMALL_INT
(
args
[
0
]))
{
// 1 arg, an integer: construct a blank bytearray of that length
uint
len
=
MP_OBJ_SMALL_INT_VALUE
(
args
[
0
]);
mp_obj_array_t
*
o
=
array_new
(
BYTEARRAY_TYPECODE
,
len
);
memset
(
o
->
items
,
0
,
len
);
return
o
;
}
else
{
// 1 arg, an iterator: construct the bytearray from that
return
array_construct
(
BYTEARRAY_TYPECODE
,
args
[
0
]);
}
return
array_construct
(
BYTEARRAY_TYPECODE
,
args
[
0
]);
}
STATIC
mp_obj_t
array_unary_op
(
int
op
,
mp_obj_t
o_in
)
{
...
...
@@ -129,7 +134,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
STATIC
mp_obj_t
array_subscr
(
mp_obj_t
self_in
,
mp_obj_t
index_in
,
mp_obj_t
value
)
{
if
(
value
==
MP_OBJ_NULL
)
{
// delete item; does this need to be implemented?
// delete item
// TODO implement
return
MP_OBJ_NOT_SUPPORTED
;
}
else
{
mp_obj_array_t
*
o
=
self_in
;
...
...
@@ -183,12 +189,16 @@ const mp_obj_type_t mp_type_bytearray = {
};
STATIC
mp_obj_array_t
*
array_new
(
char
typecode
,
uint
n
)
{
int
typecode_size
=
mp_binary_get_size
(
typecode
);
if
(
typecode_size
<=
0
)
{
nlr_raise
(
mp_obj_new_exception_msg
(
&
mp_type_ValueError
,
"bad typecode"
));
}
mp_obj_array_t
*
o
=
m_new_obj
(
mp_obj_array_t
);
o
->
base
.
type
=
(
typecode
==
BYTEARRAY_TYPECODE
)
?
&
mp_type_bytearray
:
&
mp_type_array
;
o
->
typecode
=
typecode
;
o
->
free
=
0
;
o
->
len
=
n
;
o
->
items
=
m_malloc
(
mp_binary_get_size
(
typecode
)
*
o
->
len
);
o
->
items
=
m_malloc
(
typecode
_size
*
o
->
len
);
return
o
;
}
...
...
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