Skip to content

Commit 7667727

Browse files
committed
objsingleton: New home for Ellipsis and NotImplemented.
Having NotImplemented as MP_OBJ_SENTINEL turned out to be problematic (it needs to be checked for in a lot of places, otherwise it'll crash as would pass MP_OBJ_IS_OBJ()), so made a proper singleton value like Ellipsis, both of them sharing the same type.
1 parent e04aa96 commit 7667727

6 files changed

Lines changed: 61 additions & 31 deletions

File tree

py/modbuiltins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = {
635635
// built-in objects
636636
{ MP_OBJ_NEW_QSTR(MP_QSTR_Ellipsis), (mp_obj_t)&mp_const_ellipsis_obj },
637637
#if MICROPY_PY_BUILTINS_NOTIMPLEMENTED
638-
{ MP_OBJ_NEW_QSTR(MP_QSTR_NotImplemented), MP_OBJ_SENTINEL },
638+
{ MP_OBJ_NEW_QSTR(MP_QSTR_NotImplemented), (mp_obj_t)&mp_const_notimplemented_obj },
639639
#endif
640640

641641
// built-in user functions

py/obj.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
6262
return;
6363
}
6464
#endif
65-
66-
#if MICROPY_PY_BUILTINS_NOTIMPLEMENTED
67-
if (o_in == MP_OBJ_SENTINEL) {
68-
mp_printf(print, "%q", MP_QSTR_NotImplemented);
69-
return;
70-
}
71-
#endif
72-
7365
mp_obj_type_t *type = mp_obj_get_type(o_in);
7466
if (type->print != NULL) {
7567
type->print((mp_print_t*)print, o_in, kind);

py/obj.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ extern const struct _mp_obj_bool_t mp_const_false_obj;
449449
extern const struct _mp_obj_bool_t mp_const_true_obj;
450450
extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
451451
extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
452-
extern const struct _mp_obj_ellipsis_t mp_const_ellipsis_obj;
452+
extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj;
453+
extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj;
453454
extern const struct _mp_obj_exception_t mp_const_MemoryError_obj;
454455
extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
455456

py/objsingleton.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdlib.h>
28+
#include <assert.h>
29+
30+
#include "py/nlr.h"
31+
#include "py/obj.h"
32+
#include "py/runtime0.h"
33+
34+
/******************************************************************************/
35+
/* singleton objects defined by Python */
36+
37+
typedef struct _mp_obj_singleton_t {
38+
mp_obj_base_t base;
39+
qstr name;
40+
} mp_obj_singleton_t;
41+
42+
STATIC void singleton_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
43+
(void)kind;
44+
mp_obj_singleton_t *self = self_in;
45+
mp_printf(print, "%q", self->name);
46+
}
47+
48+
const mp_obj_type_t mp_type_singleton = {
49+
{ &mp_type_type },
50+
.name = MP_QSTR_,
51+
.print = singleton_print,
52+
};
53+
54+
const mp_obj_singleton_t mp_const_ellipsis_obj = {{&mp_type_singleton}, MP_QSTR_Ellipsis};
55+
#if MICROPY_PY_BUILTINS_NOTIMPLEMENTED
56+
const mp_obj_singleton_t mp_const_notimplemented_obj = {{&mp_type_singleton}, MP_QSTR_NotImplemented};
57+
#endif

py/objslice.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,6 @@
3131
#include "py/obj.h"
3232
#include "py/runtime0.h"
3333

34-
/******************************************************************************/
35-
/* ellipsis object, a singleton */
36-
37-
typedef struct _mp_obj_ellipsis_t {
38-
mp_obj_base_t base;
39-
} mp_obj_ellipsis_t;
40-
41-
STATIC void ellipsis_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
42-
(void)self_in;
43-
(void)kind;
44-
mp_print_str(print, "Ellipsis");
45-
}
46-
47-
const mp_obj_type_t mp_type_ellipsis = {
48-
{ &mp_type_type },
49-
.name = MP_QSTR_Ellipsis,
50-
.print = ellipsis_print,
51-
};
52-
53-
const mp_obj_ellipsis_t mp_const_ellipsis_obj = {{&mp_type_ellipsis}};
54-
5534
/******************************************************************************/
5635
/* slice object */
5736

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ PY_O_BASENAME = \
8282
objrange.o \
8383
objreversed.o \
8484
objset.o \
85+
objsingleton.o \
8586
objslice.o \
8687
objstr.o \
8788
objstrunicode.o \

0 commit comments

Comments
 (0)