Skip to content

Commit

Permalink
py: Implement basic typing module.
Browse files Browse the repository at this point in the history
Signed-off-by: stijn <[email protected]>
  • Loading branch information
stinos committed Sep 5, 2024
1 parent 185116e commit fa3eecb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions py/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ extern const mp_obj_dict_t mp_module_builtins_globals;
extern const mp_obj_module_t mp_module___main__;
extern const mp_obj_module_t mp_module_builtins;
extern const mp_obj_module_t mp_module_sys;
extern const mp_obj_module_t mp_module_typing;

// Modules needed by the parser when MICROPY_COMP_MODULE_CONST is enabled.
extern const mp_obj_module_t mp_module_errno;
Expand Down
77 changes: 77 additions & 0 deletions py/modsys.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,81 @@ MP_REGISTER_ROOT_POINTER(mp_obj_t sys_mutable[MP_SYS_MUTABLE_NUM]);
MP_REGISTER_MODULE_DELEGATION(mp_module_sys, mp_module_sys_attr);
#endif

static const mp_obj_type_t type_ignore_type;

typedef struct _mp_obj_type_ignore_type_t
{
mp_obj_base_t base;
} mp_obj_type_ignore_type_t;

static void type_ignore_attr( mp_obj_t self_in, qstr attr, mp_obj_t* dest )
{
if( dest[ 0 ] == MP_OBJ_NULL )
{
dest[ 0 ] = self_in;
}
}

static mp_obj_t type_ignore_subscr( mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value )
{
if( value == MP_OBJ_NULL )
{
return MP_OBJ_NULL;
}
return self_in;
}

static mp_obj_t type_ignore_call( mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t* args )
{
return self_in;
}

extern mp_obj_type_ignore_type_t type_ignore_obj;

static mp_obj_t type_ignore_new( const mp_obj_type_t* type, size_t n_args, size_t n_kw, const mp_obj_t* args )
{
return MP_OBJ_FROM_PTR( & type_ignore_obj );
}

static MP_DEFINE_CONST_OBJ_TYPE(
mp_type_ignore_type_t,
MP_QSTR_type_ignore,
MP_TYPE_FLAG_NONE,
make_new,
type_ignore_new,
attr,
type_ignore_attr,
subscr,
type_ignore_subscr,
call,
type_ignore_call
);


mp_obj_type_ignore_type_t type_ignore_obj = { { &mp_type_ignore_type_t } };


static const mp_rom_map_elem_t mp_module_typing_globals_table[] = {
{ MP_ROM_QSTR( MP_QSTR___name__ ), MP_ROM_QSTR( MP_QSTR_typing ) },
{ MP_ROM_QSTR( MP_QSTR__type_ignore_instance ), MP_ROM_PTR( &type_ignore_obj ) },
};

static MP_DEFINE_CONST_DICT( mp_module_typing_globals, mp_module_typing_globals_table );

const mp_obj_module_t mp_module_typing = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*) &mp_module_typing_globals,
};

void mp_module_typing_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
*dest = &type_ignore_obj;
}

//TODO should be extensible so that it can just be overridden
//MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_typing, mp_module_typing);
MP_REGISTER_MODULE(MP_QSTR_typing, mp_module_typing);
MP_REGISTER_MODULE_DELEGATION(mp_module_typing, mp_module_typing_attr);



#endif // MICROPY_PY_SYS

0 comments on commit fa3eecb

Please sign in to comment.