Make {Queue,SizedQueue}#freeze raise TypeError

Fixes [Bug #17146]
This commit is contained in:
Jeremy Evans 2023-09-26 15:31:55 -07:00
Родитель 84312e688f
Коммит 62181e17da
2 изменённых файлов: 26 добавлений и 0 удалений

Просмотреть файл

@ -19,6 +19,15 @@ class TestThreadQueue < Test::Unit::TestCase
}
end
def test_freeze
assert_raise(TypeError) {
Queue.new.freeze
}
assert_raise(TypeError) {
SizedQueue.new(5).freeze
}
end
def test_queue
grind(5, 1000, 15, Queue)
end

Просмотреть файл

@ -1138,6 +1138,21 @@ rb_queue_length(VALUE self)
return LONG2NUM(queue_length(self, queue_ptr(self)));
}
NORETURN(static VALUE rb_queue_freeze(VALUE self));
/*
* call-seq:
* freeze
*
* Raises an exception:
* Queue.new.freeze # Raises TypeError (cannot freeze #<Thread::Queue:0x...>)
*/
static VALUE
rb_queue_freeze(VALUE self)
{
rb_raise(rb_eTypeError, "cannot freeze " "%+"PRIsVALUE, self);
UNREACHABLE_RETURN(self);
}
/*
* Document-method: Thread::Queue#num_waiting
*
@ -1599,6 +1614,7 @@ Init_thread_sync(void)
rb_define_method(rb_cQueue, "clear", rb_queue_clear, 0);
rb_define_method(rb_cQueue, "length", rb_queue_length, 0);
rb_define_method(rb_cQueue, "num_waiting", rb_queue_num_waiting, 0);
rb_define_method(rb_cQueue, "freeze", rb_queue_freeze, 0);
rb_define_alias(rb_cQueue, "enq", "push");
rb_define_alias(rb_cQueue, "<<", "push");
@ -1615,6 +1631,7 @@ Init_thread_sync(void)
rb_define_method(rb_cSizedQueue, "clear", rb_szqueue_clear, 0);
rb_define_method(rb_cSizedQueue, "length", rb_szqueue_length, 0);
rb_define_method(rb_cSizedQueue, "num_waiting", rb_szqueue_num_waiting, 0);
rb_define_method(rb_cSizedQueue, "freeze", rb_queue_freeze, 0);
rb_define_alias(rb_cSizedQueue, "size", "length");
/* CVar */