Skip to content

Commit 16b7747

Browse files
Implement nb_bool for Producer which makes bool(Producer(...)) return True irrespective of the length of the queue (confluentinc#1445)
1 parent 1828db3 commit 16b7747

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/confluent_kafka/src/Producer.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,23 @@ static PySequenceMethods Producer_seq_methods = {
824824
(lenfunc)Producer__len__ /* sq_length */
825825
};
826826

827+
static int Producer__bool__ (Handle *self) {
828+
return 1;
829+
}
830+
831+
static PyNumberMethods Producer_num_methods = {
832+
0, // nb_add
833+
0, // nb_subtract
834+
0, // nb_multiply
835+
0, // nb_remainder
836+
0, // nb_divmod
837+
0, // nb_power
838+
0, // nb_negative
839+
0, // nb_positive
840+
0, // nb_absolute
841+
(inquiry)Producer__bool__ // nb_bool
842+
};
843+
827844

828845
static int Producer_init (PyObject *selfobj, PyObject *args, PyObject *kwargs) {
829846
Handle *self = (Handle *)selfobj;
@@ -879,8 +896,8 @@ PyTypeObject ProducerType = {
879896
0, /*tp_setattr*/
880897
0, /*tp_compare*/
881898
0, /*tp_repr*/
882-
0, /*tp_as_number*/
883-
&Producer_seq_methods, /*tp_as_sequence*/
899+
&Producer_num_methods, /*tp_as_number*/
900+
&Producer_seq_methods, /*tp_as_sequence*/
884901
0, /*tp_as_mapping*/
885902
0, /*tp_hash */
886903
0, /*tp_call*/

tests/test_Producer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,13 @@ def on_delivery(err, msg):
271271
p.purge()
272272
p.flush(0.002)
273273
assert cb_detector["on_delivery_called"]
274+
275+
276+
def test_producer_bool_value():
277+
"""
278+
Make sure producer has a truth-y bool value
279+
See https://github.com/confluentinc/confluent-kafka-python/issues/1427
280+
"""
281+
282+
p = Producer({})
283+
assert bool(p)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy