From f69ab79ec8347c1f5ff0c6f31947ec06073fbd52 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 7 Jul 2017 11:47:38 +1000 Subject: [PATCH] py/objgenerator: Allow to hash generators and generator instances. Adds nothing to the code size, since it uses existing empty slots in the type structures. --- py/objgenerator.c | 2 ++ tests/basics/builtin_hash_gen.py | 7 +++++++ tests/run-tests | 1 + 3 files changed, 10 insertions(+) create mode 100644 tests/basics/builtin_hash_gen.py diff --git a/py/objgenerator.c b/py/objgenerator.c index 8cb0e60ccb..9d6e636b38 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -74,6 +74,7 @@ const mp_obj_type_t mp_type_gen_wrap = { { &mp_type_type }, .name = MP_QSTR_generator, .call = gen_wrap_call, + .unary_op = mp_generic_unary_op, }; mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) { @@ -235,6 +236,7 @@ const mp_obj_type_t mp_type_gen_instance = { { &mp_type_type }, .name = MP_QSTR_generator, .print = gen_instance_print, + .unary_op = mp_generic_unary_op, .getiter = mp_identity_getiter, .iternext = gen_instance_iternext, .locals_dict = (mp_obj_dict_t*)&gen_instance_locals_dict, diff --git a/tests/basics/builtin_hash_gen.py b/tests/basics/builtin_hash_gen.py new file mode 100644 index 0000000000..d42e5ebfbc --- /dev/null +++ b/tests/basics/builtin_hash_gen.py @@ -0,0 +1,7 @@ +# test builtin hash function, on generators + +def gen(): + yield + +print(type(hash(gen))) +print(type(hash(gen()))) diff --git a/tests/run-tests b/tests/run-tests index f651242862..bd4a1363cb 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -323,6 +323,7 @@ def run_tests(pyb, tests, args, base_path="."): skip_tests.update({'basics/%s.py' % t for t in 'with_break with_continue with_return'.split()}) # require complete with support skip_tests.add('basics/array_construct2.py') # requires generators skip_tests.add('basics/bool1.py') # seems to randomly fail + skip_tests.add('basics/builtin_hash_gen.py') # requires yield skip_tests.add('basics/class_bind_self.py') # requires yield skip_tests.add('basics/del_deref.py') # requires checking for unbound local skip_tests.add('basics/del_local.py') # requires checking for unbound local