Skip to content

Commit f69ab79

Browse files
committed
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.
1 parent 145796f commit f69ab79

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

py/objgenerator.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const mp_obj_type_t mp_type_gen_wrap = {
7474
{ &mp_type_type },
7575
.name = MP_QSTR_generator,
7676
.call = gen_wrap_call,
77+
.unary_op = mp_generic_unary_op,
7778
};
7879

7980
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 = {
235236
{ &mp_type_type },
236237
.name = MP_QSTR_generator,
237238
.print = gen_instance_print,
239+
.unary_op = mp_generic_unary_op,
238240
.getiter = mp_identity_getiter,
239241
.iternext = gen_instance_iternext,
240242
.locals_dict = (mp_obj_dict_t*)&gen_instance_locals_dict,

tests/basics/builtin_hash_gen.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# test builtin hash function, on generators
2+
3+
def gen():
4+
yield
5+
6+
print(type(hash(gen)))
7+
print(type(hash(gen())))

tests/run-tests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ def run_tests(pyb, tests, args, base_path="."):
323323
skip_tests.update({'basics/%s.py' % t for t in 'with_break with_continue with_return'.split()}) # require complete with support
324324
skip_tests.add('basics/array_construct2.py') # requires generators
325325
skip_tests.add('basics/bool1.py') # seems to randomly fail
326+
skip_tests.add('basics/builtin_hash_gen.py') # requires yield
326327
skip_tests.add('basics/class_bind_self.py') # requires yield
327328
skip_tests.add('basics/del_deref.py') # requires checking for unbound local
328329
skip_tests.add('basics/del_local.py') # requires checking for unbound local

0 commit comments

Comments
 (0)