Browse Source

replaced MP_OBJ_IS_TYPE

pull/8/head
Zoltán Vörös 5 years ago
parent
commit
90e4102b3d
  1. 18
      docs/micropython-usermod.ipynb
  2. 2
      docs/source/conf.py
  3. 2
      docs/source/usermods_09.rst
  4. 8
      docs/source/usermods_10.rst
  5. 4
      snippets/sliceiterable/sliceiterable.c

18
docs/micropython-usermod.ipynb

@ -3157,7 +3157,7 @@
"\n",
"Now, in order to see, how we can work with this structure, we are going to define a new type that simply stores the three values. The module will also have a method called `length`, returning the absolute value of the vector. Also note that here we check the type of the argument, and bail out, if it is not a vector. The beauty of all this is that once the type is defined, the available micropython methods just work. Can you still recall the \n",
"```c\n",
"MP_OBJ_IS_TYPE(myobject, &my_type)\n",
"mp_obj_is_type(myobject, &my_type)\n",
"```\n",
"macro in Section [Type checking?](#Type-checking) I thought so."
]
@ -3464,7 +3464,7 @@
" print('something went terribly wrong`)\n",
"```\n",
"\n",
"construct, or you can inspect the type of the variable at the C level. Unfortunately, there does not seem to be a type identifier for iterables in general, so you have to check, whether the argument is a list, tuple, range, etc. This can be done by calling the `MP_OBJ_IS_TYPE` macro, and see which Boolean it returns, if you pass `&mp_type_tuple`, `&mp_type_list`, `&mp_type_range` etc. to it, as we discussed in the section [Object representation](#Object-representation). \n",
"construct, or you can inspect the type of the variable at the C level. Unfortunately, there does not seem to be a type identifier for iterables in general, so you have to check, whether the argument is a list, tuple, range, etc. This can be done by calling the `mp_obj_is_type` macro, and see which Boolean it returns, if you pass `&mp_type_tuple`, `&mp_type_list`, `&mp_type_range` etc. to it, as we discussed in the section [Object representation](#Object-representation). \n",
"\n",
"The complete code listing of `consumeiterable.c` follows below. If you ask me, this is a lot of code just to replace a python one-liner."
]
@ -4485,7 +4485,7 @@
" if (value == MP_OBJ_SENTINEL) { // simply return the values at index, no assignment\n",
"\n",
"#if MICROPY_PY_BUILTINS_SLICE\n",
" if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {\n",
" if (mp_obj_is_type(index, &mp_type_slice)) {\n",
" mp_bound_slice_t slice;\n",
" mp_seq_get_fast_slice_indexes(self->len, index, &slice);\n",
" uint16_t len = (slice.stop - slice.start) / slice.step;\n",
@ -4514,7 +4514,7 @@
"As advertised, we treat only the case, when `value` is empty, i.e., it is equal to an `MP_OBJ_SENTINEL`. Now, there is no point in trying to read out the parameters of a slice, if the slice object is not even defined, is there? This is the case for the minimal ports. So, in order to prevent nasty things from happening, we insert the `#if/#endif` macro with the parameter `MICROPY_PY_BUILTINS_SLICE`. Provided that `MICROPY_PY_BUILTINS_SLICE` is defined, we inspect the index, and find out if it is a slice by calling \n",
"\n",
"```c\n",
"MP_OBJ_IS_TYPE(index, &mp_type_slice)\n",
"mp_obj_is_type(index, &mp_type_slice)\n",
"```\n",
"\n",
"If so, we attempt to load the slice parameters into the `slice` object with\n",
@ -4528,11 +4528,11 @@
},
{
"cell_type": "code",
"execution_count": 104,
"execution_count": 72,
"metadata": {
"ExecuteTime": {
"end_time": "2019-08-07T04:57:30.535065Z",
"start_time": "2019-08-07T04:57:30.525381Z"
"end_time": "2020-01-02T07:40:11.299734Z",
"start_time": "2020-01-02T07:40:11.277483Z"
}
},
"outputs": [
@ -4540,7 +4540,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"written 4698 bytes to /sliceiterable/sliceiterable.c\n"
"written 4703 bytes to /sliceiterable/sliceiterable.c\n"
]
}
],
@ -4598,7 +4598,7 @@
" if (value == MP_OBJ_SENTINEL) { // simply return the values at index, no assignment\n",
"\n",
"#if MICROPY_PY_BUILTINS_SLICE\n",
" if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {\n",
" if (mp_obj_is_type(index, &mp_type_slice)) {\n",
" mp_bound_slice_t slice;\n",
" mp_seq_get_fast_slice_indexes(self->len, index, &slice);\n",
" printf(\"start: %ld, stop: %ld, step: %ld\\n\", slice.start, slice.stop, slice.step);\n",

2
docs/source/conf.py

@ -6,7 +6,7 @@ copyright = '2019-2020, Zoltán Vörös'
author = 'Zoltán Vörös'
# The full version, including alpha/beta/rc tags
release = '1.41'
release = '1.42'
# -- General configuration ---------------------------------------------------

2
docs/source/usermods_09.rst

@ -25,7 +25,7 @@ you still recall the
.. code:: c
MP_OBJ_IS_TYPE(myobject, &my_type)
mp_obj_is_type(myobject, &my_type)
macro in Section `Type checking? <#Type-checking>`__ I thought so.

8
docs/source/usermods_10.rst

@ -86,7 +86,7 @@ enclose your function in a
construct, or you can inspect the type of the variable at the C level.
Unfortunately, there does not seem to be a type identifier for iterables
in general, so you have to check, whether the argument is a list, tuple,
range, etc. This can be done by calling the ``MP_OBJ_IS_TYPE`` macro,
range, etc. This can be done by calling the ``mp_obj_is_type`` macro,
and see which Boolean it returns, if you pass ``&mp_type_tuple``,
``&mp_type_list``, ``&mp_type_range`` etc. to it, as we discussed in the
section `Object representation <#Object-representation>`__.
@ -850,7 +850,7 @@ snippet:
if (value == MP_OBJ_SENTINEL) { // simply return the values at index, no assignment
#if MICROPY_PY_BUILTINS_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
if (mp_obj_is_type(index, &mp_type_slice)) {
mp_bound_slice_t slice;
mp_seq_get_fast_slice_indexes(self->len, index, &slice);
uint16_t len = (slice.stop - slice.start) / slice.step;
@ -881,7 +881,7 @@ out if it is a slice by calling
.. code:: c
MP_OBJ_IS_TYPE(index, &mp_type_slice)
mp_obj_is_type(index, &mp_type_slice)
If so, we attempt to load the slice parameters into the ``slice`` object
with
@ -961,7 +961,7 @@ https://github.com/v923z/micropython-usermod/tree/master/snippets/sliceiterable/
if (value == MP_OBJ_SENTINEL) { // simply return the values at index, no assignment
#if MICROPY_PY_BUILTINS_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
if (mp_obj_is_type(index, &mp_type_slice)) {
mp_bound_slice_t slice;
mp_seq_get_fast_slice_indexes(self->len, index, &slice);
printf("start: %ld, stop: %ld, step: %ld\n", slice.start, slice.stop, slice.step);

4
snippets/sliceiterable/sliceiterable.c

@ -5,7 +5,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
* Copyright (c) 2019-2020 Zoltán Vörös
*/
#include <stdlib.h>
@ -59,7 +59,7 @@ STATIC mp_obj_t sliceitarray_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t v
if (value == MP_OBJ_SENTINEL) { // simply return the values at index, no assignment
#if MICROPY_PY_BUILTINS_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
if (mp_obj_is_type(index, &mp_type_slice)) {
mp_bound_slice_t slice;
mp_seq_get_fast_slice_indexes(self->len, index, &slice);
printf("start: %ld, stop: %ld, step: %ld\n", slice.start, slice.stop, slice.step);

Loading…
Cancel
Save