@@ -67,7 +67,7 @@ static void Consumer_dealloc (Handle *self) {
67
67
CallState_end (self , & cs );
68
68
}
69
69
70
- Py_TYPE (self )-> tp_free ((PyObject * )self );
70
+ Py_TYPE (self )-> tp_free ((PyObject * )self );
71
71
}
72
72
73
73
static int Consumer_traverse (Handle * self ,
@@ -550,70 +550,6 @@ static PyMethodDef Consumer_methods[] = {
550
550
};
551
551
552
552
553
- static PyObject * Consumer_new (PyTypeObject * type , PyObject * args ,
554
- PyObject * kwargs );
555
-
556
- PyTypeObject ConsumerType = {
557
- PyVarObject_HEAD_INIT (NULL , 0 )
558
- "cimpl.Consumer" , /*tp_name*/
559
- sizeof (Handle ), /*tp_basicsize*/
560
- 0 , /*tp_itemsize*/
561
- (destructor )Consumer_dealloc , /*tp_dealloc*/
562
- 0 , /*tp_print*/
563
- 0 , /*tp_getattr*/
564
- 0 , /*tp_setattr*/
565
- 0 , /*tp_compare*/
566
- 0 , /*tp_repr*/
567
- 0 , /*tp_as_number*/
568
- 0 , /*tp_as_sequence*/
569
- 0 , /*tp_as_mapping*/
570
- 0 , /*tp_hash */
571
- 0 , /*tp_call*/
572
- 0 , /*tp_str*/
573
- 0 , /*tp_getattro*/
574
- 0 , /*tp_setattro*/
575
- 0 , /*tp_as_buffer*/
576
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC , /*tp_flags*/
577
- "High-level Kafka Consumer\n"
578
- "\n"
579
- ".. py:function:: Consumer(**kwargs)\n"
580
- "\n"
581
- " Create new Consumer instance using provided configuration dict.\n"
582
- "\n"
583
- " Special configuration properties:\n"
584
- " ``on_commit``: Optional callback will be called when a commit "
585
- "request has succeeded or failed.\n"
586
- "\n"
587
- "\n"
588
- ".. py:function:: on_commit(err, partitions)\n"
589
- "\n"
590
- " :param Consumer consumer: Consumer instance.\n"
591
- " :param KafkaError err: Commit error object, or None on success.\n"
592
- " :param list(TopicPartition) partitions: List of partitions with "
593
- "their committed offsets or per-partition errors.\n"
594
- "\n"
595
- "\n" , /*tp_doc*/
596
- (traverseproc )Consumer_traverse , /* tp_traverse */
597
- (inquiry )Consumer_clear , /* tp_clear */
598
- 0 , /* tp_richcompare */
599
- 0 , /* tp_weaklistoffset */
600
- 0 , /* tp_iter */
601
- 0 , /* tp_iternext */
602
- Consumer_methods , /* tp_methods */
603
- 0 , /* tp_members */
604
- 0 , /* tp_getset */
605
- 0 , /* tp_base */
606
- 0 , /* tp_dict */
607
- 0 , /* tp_descr_get */
608
- 0 , /* tp_descr_set */
609
- 0 , /* tp_dictoffset */
610
- 0 , /* tp_init */
611
- 0 , /* tp_alloc */
612
- Consumer_new /* tp_new */
613
- };
614
-
615
-
616
-
617
553
static void Consumer_rebalance_cb (rd_kafka_t * rk , rd_kafka_resp_err_t err ,
618
554
rd_kafka_topic_partition_list_t * c_parts ,
619
555
void * opaque ) {
@@ -726,37 +662,100 @@ static void Consumer_offset_commit_cb (rd_kafka_t *rk, rd_kafka_resp_err_t err,
726
662
727
663
728
664
729
- static PyObject * Consumer_new (PyTypeObject * type , PyObject * args ,
730
- PyObject * kwargs ) {
731
- Handle * self ;
732
- char errstr [256 ];
733
- rd_kafka_conf_t * conf ;
665
+ static int Consumer_init (PyObject * selfobj , PyObject * args , PyObject * kwargs ) {
666
+ Handle * self = (Handle * )selfobj ;
667
+ char errstr [256 ];
668
+ rd_kafka_conf_t * conf ;
734
669
735
- self = (Handle * )ConsumerType .tp_alloc (& ConsumerType , 0 );
736
- if (!self )
737
- return NULL ;
670
+ if (self -> rk ) {
671
+ PyErr_SetString (PyExc_RuntimeError ,
672
+ "Consumer already __init__:ialized" );
673
+ return -1 ;
674
+ }
738
675
739
- if (!(conf = common_conf_setup (RD_KAFKA_CONSUMER , self ,
740
- args , kwargs ))) {
741
- Py_DECREF (self );
742
- return NULL ;
743
- }
676
+ if (!(conf = common_conf_setup (RD_KAFKA_CONSUMER , self ,
677
+ args , kwargs )))
678
+ return -1 ; /* Exception raised by ..conf_setup() */
744
679
745
- rd_kafka_conf_set_rebalance_cb (conf , Consumer_rebalance_cb );
746
- rd_kafka_conf_set_offset_commit_cb (conf , Consumer_offset_commit_cb );
680
+ rd_kafka_conf_set_rebalance_cb (conf , Consumer_rebalance_cb );
681
+ rd_kafka_conf_set_offset_commit_cb (conf , Consumer_offset_commit_cb );
747
682
748
- self -> rk = rd_kafka_new (RD_KAFKA_CONSUMER , conf ,
749
- errstr , sizeof (errstr ));
750
- if (!self -> rk ) {
751
- cfl_PyErr_Format (rd_kafka_last_error (),
752
- "Failed to create consumer: %s" , errstr );
753
- Py_DECREF (self );
754
- return NULL ;
755
- }
683
+ self -> rk = rd_kafka_new (RD_KAFKA_CONSUMER , conf ,
684
+ errstr , sizeof (errstr ));
685
+ if (!self -> rk ) {
686
+ cfl_PyErr_Format (rd_kafka_last_error (),
687
+ "Failed to create consumer: %s" , errstr );
688
+ rd_kafka_conf_destroy (conf );
689
+ return -1 ;
690
+ }
691
+
692
+ rd_kafka_poll_set_consumer (self -> rk );
756
693
757
- rd_kafka_poll_set_consumer (self -> rk );
694
+ return 0 ;
695
+ }
758
696
759
- return (PyObject * )self ;
697
+ static PyObject * Consumer_new (PyTypeObject * type , PyObject * args ,
698
+ PyObject * kwargs ) {
699
+ return type -> tp_alloc (type , 0 );
760
700
}
761
701
762
702
703
+ PyTypeObject ConsumerType = {
704
+ PyVarObject_HEAD_INIT (NULL , 0 )
705
+ "cimpl.Consumer" , /*tp_name*/
706
+ sizeof (Handle ), /*tp_basicsize*/
707
+ 0 , /*tp_itemsize*/
708
+ (destructor )Consumer_dealloc , /*tp_dealloc*/
709
+ 0 , /*tp_print*/
710
+ 0 , /*tp_getattr*/
711
+ 0 , /*tp_setattr*/
712
+ 0 , /*tp_compare*/
713
+ 0 , /*tp_repr*/
714
+ 0 , /*tp_as_number*/
715
+ 0 , /*tp_as_sequence*/
716
+ 0 , /*tp_as_mapping*/
717
+ 0 , /*tp_hash */
718
+ 0 , /*tp_call*/
719
+ 0 , /*tp_str*/
720
+ 0 , /*tp_getattro*/
721
+ 0 , /*tp_setattro*/
722
+ 0 , /*tp_as_buffer*/
723
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
724
+ Py_TPFLAGS_HAVE_GC , /*tp_flags*/
725
+ "High-level Kafka Consumer\n"
726
+ "\n"
727
+ ".. py:function:: Consumer(**kwargs)\n"
728
+ "\n"
729
+ " Create new Consumer instance using provided configuration dict.\n"
730
+ "\n"
731
+ " Special configuration properties:\n"
732
+ " ``on_commit``: Optional callback will be called when a commit "
733
+ "request has succeeded or failed.\n"
734
+ "\n"
735
+ "\n"
736
+ ".. py:function:: on_commit(err, partitions)\n"
737
+ "\n"
738
+ " :param Consumer consumer: Consumer instance.\n"
739
+ " :param KafkaError err: Commit error object, or None on success.\n"
740
+ " :param list(TopicPartition) partitions: List of partitions with "
741
+ "their committed offsets or per-partition errors.\n"
742
+ "\n"
743
+ "\n" , /*tp_doc*/
744
+ (traverseproc )Consumer_traverse , /* tp_traverse */
745
+ (inquiry )Consumer_clear , /* tp_clear */
746
+ 0 , /* tp_richcompare */
747
+ 0 , /* tp_weaklistoffset */
748
+ 0 , /* tp_iter */
749
+ 0 , /* tp_iternext */
750
+ Consumer_methods , /* tp_methods */
751
+ 0 , /* tp_members */
752
+ 0 , /* tp_getset */
753
+ 0 , /* tp_base */
754
+ 0 , /* tp_dict */
755
+ 0 , /* tp_descr_get */
756
+ 0 , /* tp_descr_set */
757
+ 0 , /* tp_dictoffset */
758
+ Consumer_init , /* tp_init */
759
+ 0 , /* tp_alloc */
760
+ Consumer_new /* tp_new */
761
+ };
0 commit comments