Skip to content

Commit 5ab5ac5

Browse files
committed
modbuiltins: Add NotImplemented builtin constant.
From https://docs.python.org/3/library/constants.html#NotImplemented : "Special value which should be returned by the binary special methods (e.g. __eq__(), __lt__(), __add__(), __rsub__(), etc.) to indicate that the operation is not implemented with respect to the other type; may be returned by the in-place binary special methods (e.g. __imul__(), __iand__(), etc.) for the same purpose. Its truth value is true." Some people however appear to abuse it to mean "no value" when None is a legitimate value (don't do that).
1 parent 3d3ef36 commit 5ab5ac5

5 files changed

Lines changed: 20 additions & 0 deletions

File tree

py/modbuiltins.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = {
634634

635635
// built-in objects
636636
{ MP_OBJ_NEW_QSTR(MP_QSTR_Ellipsis), (mp_obj_t)&mp_const_ellipsis_obj },
637+
#if MICROPY_PY_BUILTINS_NOTIMPLEMENTED
638+
{ MP_OBJ_NEW_QSTR(MP_QSTR_NotImplemented), MP_OBJ_SENTINEL },
639+
#endif
637640

638641
// built-in user functions
639642
{ MP_OBJ_NEW_QSTR(MP_QSTR_abs), (mp_obj_t)&mp_builtin_abs_obj },

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ typedef double mp_float_t;
479479
#define MICROPY_PY_BUILTINS_REVERSED (1)
480480
#endif
481481

482+
// Whether to define "NotImplemented" special constant
483+
#ifndef MICROPY_PY_BUILTINS_NOTIMPLEMENTED
484+
#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (0)
485+
#endif
486+
482487
// Whether to set __file__ for imported modules
483488
#ifndef MICROPY_PY___FILE__
484489
#define MICROPY_PY___FILE__ (1)

py/obj.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ 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+
6573
mp_obj_type_t *type = mp_obj_get_type(o_in);
6674
if (type->print != NULL) {
6775
type->print((mp_print_t*)print, o_in, kind);

py/qstrdefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ Q(builtins)
115115

116116
Q(Ellipsis)
117117
Q(StopIteration)
118+
#if MICROPY_PY_BUILTINS_NOTIMPLEMENTED
119+
Q(NotImplemented)
120+
#endif
118121

119122
Q(BaseException)
120123
Q(ArithmeticError)

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
6565
#define MICROPY_PY_BUILTINS_FROZENSET (1)
6666
#define MICROPY_PY_BUILTINS_COMPILE (1)
67+
#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1)
6768
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
6869
#define MICROPY_PY_ALL_SPECIAL_METHODS (1)
6970
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)

0 commit comments

Comments
 (0)