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
4dcb605a
Commit
4dcb605a
authored
11 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
py: Make bytearray a proper type.
parent
134c10e7
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
py/builtintables.c
+1
-1
1 addition, 1 deletion
py/builtintables.c
py/obj.h
+1
-0
1 addition, 0 deletions
py/obj.h
py/objarray.c
+23
-9
23 additions, 9 deletions
py/objarray.c
py/objarray.h
+0
-2
0 additions, 2 deletions
py/objarray.h
tests/basics/bytearray1.py
+1
-0
1 addition, 0 deletions
tests/basics/bytearray1.py
with
26 additions
and
12 deletions
py/builtintables.c
+
1
−
1
View file @
4dcb605a
...
@@ -17,6 +17,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
...
@@ -17,6 +17,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
// built-in types
// built-in types
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_bool
),
(
mp_obj_t
)
&
mp_type_bool
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_bool
),
(
mp_obj_t
)
&
mp_type_bool
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_bytes
),
(
mp_obj_t
)
&
mp_type_bytes
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_bytes
),
(
mp_obj_t
)
&
mp_type_bytes
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_bytearray
),
(
mp_obj_t
)
&
mp_type_bytearray
},
#if MICROPY_ENABLE_FLOAT
#if MICROPY_ENABLE_FLOAT
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_complex
),
(
mp_obj_t
)
&
mp_type_complex
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_complex
),
(
mp_obj_t
)
&
mp_type_complex
},
#endif
#endif
...
@@ -72,7 +73,6 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
...
@@ -72,7 +73,6 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_repr
),
(
mp_obj_t
)
&
mp_builtin_repr_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_repr
),
(
mp_obj_t
)
&
mp_builtin_repr_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_sorted
),
(
mp_obj_t
)
&
mp_builtin_sorted_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_sorted
),
(
mp_obj_t
)
&
mp_builtin_sorted_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_sum
),
(
mp_obj_t
)
&
mp_builtin_sum_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_sum
),
(
mp_obj_t
)
&
mp_builtin_sum_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_bytearray
),
(
mp_obj_t
)
&
mp_builtin_bytearray_obj
},
// built-in exceptions
// built-in exceptions
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_BaseException
),
(
mp_obj_t
)
&
mp_type_BaseException
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_BaseException
),
(
mp_obj_t
)
&
mp_type_BaseException
},
...
...
This diff is collapsed.
Click to expand it.
py/obj.h
+
1
−
0
View file @
4dcb605a
...
@@ -259,6 +259,7 @@ extern const mp_obj_type_t mp_type_bool;
...
@@ -259,6 +259,7 @@ extern const mp_obj_type_t mp_type_bool;
extern
const
mp_obj_type_t
mp_type_int
;
extern
const
mp_obj_type_t
mp_type_int
;
extern
const
mp_obj_type_t
mp_type_str
;
extern
const
mp_obj_type_t
mp_type_str
;
extern
const
mp_obj_type_t
mp_type_bytes
;
extern
const
mp_obj_type_t
mp_type_bytes
;
extern
const
mp_obj_type_t
mp_type_bytearray
;
extern
const
mp_obj_type_t
mp_type_float
;
extern
const
mp_obj_type_t
mp_type_float
;
extern
const
mp_obj_type_t
mp_type_complex
;
extern
const
mp_obj_type_t
mp_type_complex
;
extern
const
mp_obj_type_t
mp_type_tuple
;
extern
const
mp_obj_type_t
mp_type_tuple
;
...
...
This diff is collapsed.
Click to expand it.
py/objarray.c
+
23
−
9
View file @
4dcb605a
...
@@ -88,19 +88,20 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
...
@@ -88,19 +88,20 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
return
array_construct
(
*
typecode
,
args
[
1
]);
return
array_construct
(
*
typecode
,
args
[
1
]);
}
}
// This is top-level factory function, not virtual method
STATIC
mp_obj_t
bytearray_make_new
(
mp_obj_t
type_in
,
uint
n_args
,
uint
n_kw
,
const
mp_obj_t
*
args
)
{
// TODO: "bytearray" really should be type, not function
if
(
n_args
>
1
)
{
STATIC
mp_obj_t
mp_builtin_bytearray
(
mp_obj_t
arg
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"unexpected # of arguments, %d given"
,
n_args
));
if
(
MP_OBJ_IS_SMALL_INT
(
arg
))
{
}
uint
len
=
MP_OBJ_SMALL_INT_VALUE
(
arg
);
if
(
MP_OBJ_IS_SMALL_INT
(
args
[
0
]))
{
uint
len
=
MP_OBJ_SMALL_INT_VALUE
(
args
[
0
]);
mp_obj_array_t
*
o
=
array_new
(
BYTEARRAY_TYPECODE
,
len
);
mp_obj_array_t
*
o
=
array_new
(
BYTEARRAY_TYPECODE
,
len
);
memset
(
o
->
items
,
0
,
len
);
memset
(
o
->
items
,
0
,
len
);
return
o
;
return
o
;
}
}
return
array_construct
(
BYTEARRAY_TYPECODE
,
arg
);
return
array_construct
(
BYTEARRAY_TYPECODE
,
arg
s
[
0
]
);
}
}
MP_DEFINE_CONST_FUN_OBJ_1
(
mp_builtin_bytearray_obj
,
mp_builtin_bytearray
);
STATIC
mp_obj_t
array_unary_op
(
int
op
,
mp_obj_t
o_in
)
{
STATIC
mp_obj_t
array_unary_op
(
int
op
,
mp_obj_t
o_in
)
{
mp_obj_array_t
*
o
=
o_in
;
mp_obj_array_t
*
o
=
o_in
;
...
@@ -127,7 +128,7 @@ STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
...
@@ -127,7 +128,7 @@ STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
}
}
STATIC
mp_obj_t
array_append
(
mp_obj_t
self_in
,
mp_obj_t
arg
)
{
STATIC
mp_obj_t
array_append
(
mp_obj_t
self_in
,
mp_obj_t
arg
)
{
assert
(
MP_OBJ_IS_TYPE
(
self_in
,
&
mp_type_array
));
assert
(
MP_OBJ_IS_TYPE
(
self_in
,
&
mp_type_array
)
||
MP_OBJ_IS_TYPE
(
self_in
,
&
mp_type_bytearray
)
);
mp_obj_array_t
*
self
=
self_in
;
mp_obj_array_t
*
self
=
self_in
;
if
(
self
->
free
==
0
)
{
if
(
self
->
free
==
0
)
{
int
item_sz
=
mp_binary_get_size
(
self
->
typecode
);
int
item_sz
=
mp_binary_get_size
(
self
->
typecode
);
...
@@ -174,9 +175,22 @@ const mp_obj_type_t mp_type_array = {
...
@@ -174,9 +175,22 @@ const mp_obj_type_t mp_type_array = {
.
locals_dict
=
(
mp_obj_t
)
&
array_locals_dict
,
.
locals_dict
=
(
mp_obj_t
)
&
array_locals_dict
,
};
};
const
mp_obj_type_t
mp_type_bytearray
=
{
{
&
mp_type_type
},
.
name
=
MP_QSTR_bytearray
,
.
print
=
array_print
,
.
make_new
=
bytearray_make_new
,
.
getiter
=
array_iterator_new
,
.
unary_op
=
array_unary_op
,
.
binary_op
=
array_binary_op
,
.
store_item
=
array_store_item
,
.
buffer_p
=
{
.
get_buffer
=
array_get_buffer
},
.
locals_dict
=
(
mp_obj_t
)
&
array_locals_dict
,
};
STATIC
mp_obj_array_t
*
array_new
(
char
typecode
,
uint
n
)
{
STATIC
mp_obj_array_t
*
array_new
(
char
typecode
,
uint
n
)
{
mp_obj_array_t
*
o
=
m_new_obj
(
mp_obj_array_t
);
mp_obj_array_t
*
o
=
m_new_obj
(
mp_obj_array_t
);
o
->
base
.
type
=
&
mp_type_array
;
o
->
base
.
type
=
(
typecode
==
BYTEARRAY_TYPECODE
)
?
&
mp_type_bytearray
:
&
mp_type_array
;
o
->
typecode
=
typecode
;
o
->
typecode
=
typecode
;
o
->
free
=
0
;
o
->
free
=
0
;
o
->
len
=
n
;
o
->
len
=
n
;
...
...
This diff is collapsed.
Click to expand it.
py/objarray.h
+
0
−
2
View file @
4dcb605a
MP_DECLARE_CONST_FUN_OBJ
(
mp_builtin_bytearray_obj
);
mp_obj_t
mp_obj_new_bytearray
(
uint
n
,
void
*
items
);
mp_obj_t
mp_obj_new_bytearray
(
uint
n
,
void
*
items
);
This diff is collapsed.
Click to expand it.
tests/basics/bytearray1.py
+
1
−
0
View file @
4dcb605a
print
(
bytearray
(
4
))
print
(
bytearray
(
4
))
a
=
bytearray
([
1
,
2
,
200
])
a
=
bytearray
([
1
,
2
,
200
])
print
(
type
(
a
))
print
(
a
[
0
],
a
[
2
])
print
(
a
[
0
],
a
[
2
])
print
(
a
[
-
1
])
print
(
a
[
-
1
])
print
(
a
)
print
(
a
)
...
...
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