From 0ebf8534ab56c3958be335f0c4c6f857de41f71d Mon Sep 17 00:00:00 2001 From: xbe Date: Sat, 1 Feb 2014 19:00:41 -0800 Subject: [PATCH 1/2] Implement and add tests for the id() builtin function. --- py/builtin.c | 6 ++++++ py/builtin.h | 1 + py/qstrdefs.h | 1 + py/runtime.c | 1 + tests/basics/builtin_id.py | 2 ++ 5 files changed, 11 insertions(+) create mode 100644 tests/basics/builtin_id.py diff --git a/py/builtin.c b/py/builtin.c index 56cb49de72..333119bca6 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -364,3 +364,9 @@ static mp_obj_t mp_builtin_bytes(uint n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_bytes_obj, 1, 3, mp_builtin_bytes); + +static mp_obj_t mp_builtin_id(mp_obj_t o_in) { + return mp_obj_new_int_from_uint((machine_uint_t)o_in); +} + +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id); diff --git a/py/builtin.h b/py/builtin.h index 4257de5bdb..c6e453f339 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -11,6 +11,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_id_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_iter_obj); diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 50b426f1a4..8bf3936977 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -57,6 +57,7 @@ Q(eval) Q(filter) Q(float) Q(hash) +Q(id) Q(int) Q(isinstance) Q(issubclass) diff --git a/py/runtime.c b/py/runtime.c index a3970fe3dc..67bb90cb79 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -144,6 +144,7 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj); diff --git a/tests/basics/builtin_id.py b/tests/basics/builtin_id.py new file mode 100644 index 0000000000..de236945f0 --- /dev/null +++ b/tests/basics/builtin_id.py @@ -0,0 +1,2 @@ +print(id(1) == id(2)) +print(id(None) == id(None)) From 330cf6d04a5825d606fa5cb34eed0062a4e764b3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 2 Feb 2014 13:38:21 +0000 Subject: [PATCH 2/2] Change id to return signed integer. --- py/builtin.c | 2 +- tests/basics/builtin_id.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/py/builtin.c b/py/builtin.c index 333119bca6..4564d15756 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -366,7 +366,7 @@ static mp_obj_t mp_builtin_bytes(uint n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_bytes_obj, 1, 3, mp_builtin_bytes); static mp_obj_t mp_builtin_id(mp_obj_t o_in) { - return mp_obj_new_int_from_uint((machine_uint_t)o_in); + return mp_obj_new_int((machine_int_t)o_in); } MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id); diff --git a/tests/basics/builtin_id.py b/tests/basics/builtin_id.py index de236945f0..4874f355b4 100644 --- a/tests/basics/builtin_id.py +++ b/tests/basics/builtin_id.py @@ -1,2 +1,9 @@ print(id(1) == id(2)) print(id(None) == id(None)) +print(id([]) == id([])) + +l = [1, 2] +print(id(l) == id(l)) + +f = lambda:None +print(id(f) == id(f))