1515#define PYRABBITMQ_CONNECTION_ERROR 0x10
1616#define PYRABBITMQ_CHANNEL_ERROR 0x20
1717
18+ #define PYRABBITMQ_MODULE_NAME "_librabbitmq"
19+ #define PYRABBITMQ_MODULE_DESC "Hand-made wrapper for librabbitmq."
20+
1821/* ------: Private Prototypes :------------------------------------------- */
1922PyMODINIT_FUNC init_librabbitmq (void );
2023
@@ -248,8 +251,8 @@ PyDict_ToAMQTable(amqp_connection_state_t conn, PyObject *src, amqp_pool_t *pool
248251{
249252 PyObject * dkey = NULL ;
250253 PyObject * dvalue = NULL ;
251- PY_SIZE_TYPE size = 0 ;
252- PY_SIZE_TYPE pos = 0 ;
254+ Py_ssize_t size = 0 ;
255+ Py_ssize_t pos = 0 ;
253256 uint64_t clong_value = 0 ;
254257 double cdouble_value = 0.0 ;
255258 int is_unicode = 0 ;
@@ -315,7 +318,7 @@ PyDict_ToAMQTable(amqp_connection_state_t conn, PyObject *src, amqp_pool_t *pool
315318 else {
316319 /* String | Unicode */
317320 is_unicode = PyUnicode_Check (dvalue );
318- if (is_unicode || PyString_Check (dvalue )) {
321+ if (is_unicode || PyBytes_Check (dvalue )) {
319322 if (is_unicode ) {
320323 if ((dvalue = PyUnicode_AsASCIIString (dvalue )) == NULL )
321324 goto error ;
@@ -329,7 +332,7 @@ PyDict_ToAMQTable(amqp_connection_state_t conn, PyObject *src, amqp_pool_t *pool
329332 /* unsupported type */
330333 PyErr_Format (PyExc_ValueError ,
331334 "Table member %s is of an unsupported type" ,
332- PyString_AS_STRING (dkey ));
335+ PyBytes_AS_STRING (dkey ));
333336 goto error ;
334337 }
335338 }
@@ -343,12 +346,12 @@ PyDict_ToAMQTable(amqp_connection_state_t conn, PyObject *src, amqp_pool_t *pool
343346static amqp_array_t
344347PyIter_ToAMQArray (amqp_connection_state_t conn , PyObject * src , amqp_pool_t * pool )
345348{
346- PY_SIZE_TYPE pos = 0 ;
349+ Py_ssize_t pos = 0 ;
347350 uint64_t clong_value = 0 ;
348351 int is_unicode = 0 ;
349352 amqp_array_t dst = AMQP_EMPTY_ARRAY ;
350353
351- PY_SIZE_TYPE size = PySequence_Size (src );
354+ Py_ssize_t size = PySequence_Size (src );
352355 if (size == -1 ) return dst ;
353356
354357 PyObject * iterator = PyObject_GetIter (src );
@@ -382,7 +385,7 @@ PyIter_ToAMQArray(amqp_connection_state_t conn, PyObject *src, amqp_pool_t *pool
382385 else {
383386 /* String | Unicode */
384387 is_unicode = PyUnicode_Check (item );
385- if (is_unicode || PyString_Check (item )) {
388+ if (is_unicode || PyBytes_Check (item )) {
386389 if (is_unicode ) {
387390 if ((item = PyUnicode_AsASCIIString (item )) == NULL )
388391 goto item_error ;
@@ -393,8 +396,8 @@ PyIter_ToAMQArray(amqp_connection_state_t conn, PyObject *src, amqp_pool_t *pool
393396 else {
394397 /* unsupported type */
395398 PyErr_Format (PyExc_ValueError ,
396- "Array member at index %lu, %s , is of an unsupported type" ,
397- pos , PyObject_REPR ( item ) );
399+ "Array member at index %lu, %R , is of an unsupported type" ,
400+ pos , item );
398401 goto item_error ;
399402 }
400403 }
@@ -608,7 +611,7 @@ AMQArray_toPyList(amqp_array_t *array)
608611{
609612 register PyObject * value = NULL ;
610613 PyObject * list = NULL ;
611- list = PyList_New ((PY_SIZE_TYPE ) array -> num_entries );
614+ list = PyList_New ((Py_ssize_t ) array -> num_entries );
612615
613616 if (array ) {
614617 int i ;
@@ -951,7 +954,7 @@ PyRabbitMQ_ConnectionType_dealloc(PyRabbitMQ_Connection *self)
951954 Py_XDECREF (self -> callbacks );
952955 Py_XDECREF (self -> client_properties );
953956 Py_XDECREF (self -> server_properties );
954- self -> ob_type -> tp_free (self );
957+ Py_TYPE ( self ) -> tp_free (self );
955958}
956959
957960
@@ -1236,7 +1239,7 @@ PyRabbitMQ_ApplyCallback(PyRabbitMQ_Connection *self,
12361239 goto error ;
12371240
12381241 /* message = self.Message(channel, properties, delivery_info, body) */
1239- Message = PyString_FromString ("Message" );
1242+ Message = BUILD_METHOD_NAME ("Message" );
12401243 message = PyObject_CallMethodObjArgs ((PyObject * )self , Message ,
12411244 channelobj , propdict , delivery_info , view , NULL );
12421245 if (!message )
@@ -1304,8 +1307,8 @@ PyRabbitMQ_recv(PyRabbitMQ_Connection *self, PyObject *p,
13041307 amqp_frame_t frame ;
13051308 amqp_basic_deliver_t * deliver ;
13061309 amqp_basic_properties_t * props ;
1307- PY_SIZE_TYPE body_target ;
1308- PY_SIZE_TYPE body_received ;
1310+ Py_ssize_t body_target ;
1311+ Py_ssize_t body_received ;
13091312 PyObject * channel = NULL ;
13101313 PyObject * consumer_tag = NULL ;
13111314 PyObject * delivery_info = NULL ;
@@ -1385,23 +1388,21 @@ PyRabbitMQ_recv(PyRabbitMQ_Connection *self, PyObject *p,
13851388 body_received += frame .payload .body_fragment .len ;
13861389 if (!i ) {
13871390 if (body_received < body_target ) {
1388- payload = PyString_FromStringAndSize (NULL ,
1389- (PY_SIZE_TYPE )body_target );
1391+ payload = PyBytes_FromStringAndSize (NULL ,
1392+ (Py_ssize_t )body_target );
13901393 if (!payload )
13911394 goto finally ;
1392- buf = PyString_AsString (payload );
1395+ buf = PyBytes_AsString (payload );
13931396 if (!buf )
13941397 goto finally ;
1395- view = PyBuffer_FromObject (payload , 0 ,
1396- (PY_SIZE_TYPE )body_target );
1398+ view = PyMemoryView_FromObject (payload );
13971399 }
13981400 else {
13991401 if (p ) {
14001402 payload = PySTRING_FROM_AMQBYTES (
14011403 frame .payload .body_fragment );
14021404 } else {
1403- view = PyBuffer_FromMemory (bufp ,
1404- (PY_SIZE_TYPE )frame .payload .body_fragment .len );
1405+ view = buffer_toMemoryView (bufp , (Py_ssize_t )frame .payload .body_fragment .len );
14051406 }
14061407 break ;
14071408 }
@@ -1416,7 +1417,7 @@ PyRabbitMQ_recv(PyRabbitMQ_Connection *self, PyObject *p,
14161417 if (body_target ) goto error ;
14171418
14181419 /* did not expect content, return empty string */
1419- payload = PyString_FromStringAndSize (NULL , 0 );
1420+ payload = PyBytes_FromStringAndSize (NULL , 0 );
14201421 }
14211422
14221423 PyDict_SetItemString (p , "properties" , propdict );
@@ -1653,8 +1654,8 @@ PyRabbitMQ_Connection_queue_declare(PyRabbitMQ_Connection *self,
16531654 goto bail ;
16541655
16551656 if ((ret = PyTuple_New (3 )) == NULL ) goto bail ;
1656- PyTuple_SET_ITEM (ret , 0 , PyString_FromStringAndSize (ok -> queue .bytes ,
1657- (PY_SIZE_TYPE )ok -> queue .len ));
1657+ PyTuple_SET_ITEM (ret , 0 , PyBytes_FromStringAndSize (ok -> queue .bytes ,
1658+ (Py_ssize_t )ok -> queue .len ));
16581659 PyTuple_SET_ITEM (ret , 1 , PyInt_FromLong ((long )ok -> message_count ));
16591660 PyTuple_SET_ITEM (ret , 2 , PyInt_FromLong ((long )ok -> consumer_count ));
16601661 return ret ;
@@ -1808,7 +1809,7 @@ PyRabbitMQ_Connection_basic_publish(PyRabbitMQ_Connection *self,
18081809 unsigned int immediate = 0 ;
18091810
18101811 char * body_buf = NULL ;
1811- PY_SIZE_TYPE body_size = 0 ;
1812+ Py_ssize_t body_size = 0 ;
18121813
18131814 int ret = 0 ;
18141815 amqp_basic_properties_t props ;
@@ -1819,7 +1820,7 @@ PyRabbitMQ_Connection_basic_publish(PyRabbitMQ_Connection *self,
18191820 if (PyRabbitMQ_Not_Connected (self ))
18201821 goto bail ;
18211822
1822- if (!PyArg_ParseTuple (args , "It #OOO|II" ,
1823+ if (!PyArg_ParseTuple (args , "Is #OOO|II" ,
18231824 & channel , & body_buf , & body_size , & exchange , & routing_key ,
18241825 & propdict , & mandatory , & immediate ))
18251826 goto bail ;
@@ -1866,7 +1867,7 @@ static PyObject*
18661867PyRabbitMQ_Connection_basic_ack (PyRabbitMQ_Connection * self ,
18671868 PyObject * args )
18681869{
1869- PY_SIZE_TYPE delivery_tag = 0 ;
1870+ Py_ssize_t delivery_tag = 0 ;
18701871 unsigned int channel = 0 ;
18711872 unsigned int multiple = 0 ;
18721873 int ret = 0 ;
@@ -1899,7 +1900,7 @@ PyRabbitMQ_Connection_basic_ack(PyRabbitMQ_Connection *self,
18991900static PyObject * PyRabbitMQ_Connection_basic_reject (PyRabbitMQ_Connection * self ,
19001901 PyObject * args )
19011902{
1902- PY_SIZE_TYPE delivery_tag = 0 ;
1903+ Py_ssize_t delivery_tag = 0 ;
19031904 unsigned int channel = 0 ;
19041905 unsigned int multiple = 0 ;
19051906
@@ -2030,7 +2031,7 @@ PyRabbitMQ_Connection_basic_qos(PyRabbitMQ_Connection *self,
20302031 PyObject * args )
20312032{
20322033 unsigned int channel = 0 ;
2033- PY_SIZE_TYPE prefetch_size = 0 ;
2034+ Py_ssize_t prefetch_size = 0 ;
20342035 unsigned int prefetch_count = 0 ;
20352036 unsigned int _global = 0 ;
20362037
@@ -2244,16 +2245,35 @@ static PyMethodDef PyRabbitMQ_functions[] = {
22442245 {NULL , NULL , 0 , NULL }
22452246};
22462247
2247- PyMODINIT_FUNC init_librabbitmq (void )
2248+ #if PY_MAJOR_VERSION >= 3
2249+ static struct PyModuleDef PyRabbitMQ_moduledef = {
2250+ PyModuleDef_HEAD_INIT ,
2251+ PYRABBITMQ_MODULE_NAME , /* m_name */
2252+ PYRABBITMQ_MODULE_DESC , /* m_doc */
2253+ -1 , /* m_size */
2254+ PyRabbitMQ_functions , /* m_methods */
2255+ NULL , /* m_reload */
2256+ NULL , /* m_traverse */
2257+ NULL , /* m_clear */
2258+ NULL , /* m_free */
2259+ };
2260+ #endif
2261+
2262+ PYRABBITMQ_MOD_INIT (_librabbitmq )
22482263{
22492264 PyObject * module , * socket_module ;
22502265
22512266 if (PyType_Ready (& PyRabbitMQ_ConnectionType ) < 0 ) {
22522267 return ;
22532268 }
22542269
2255- module = Py_InitModule3 ("_librabbitmq" , PyRabbitMQ_functions ,
2256- "Hand-made wrapper for librabbitmq." );
2270+ #if PY_MAJOR_VERSION >= 3
2271+ module = PyModule_Create (& PyRabbitMQ_moduledef );
2272+ #else
2273+ module = Py_InitModule3 (PYRABBITMQ_MODULE_NAME , PyRabbitMQ_functions ,
2274+ PYRABBITMQ_MODULE_DESC );
2275+ #endif
2276+
22572277 if (module == NULL ) {
22582278 return ;
22592279 }
@@ -2283,5 +2303,9 @@ PyMODINIT_FUNC init_librabbitmq(void)
22832303 "_librabbitmq.ChannelError" , NULL , NULL );
22842304 PyModule_AddObject (module , "ChannelError" ,
22852305 (PyObject * )PyRabbitMQExc_ChannelError );
2306+ #if PY_MAJOR_VERSION >= 3
2307+ return module ;
2308+ #else
22862309 return ;
2310+ #endif
22872311}
0 commit comments