Skip to content

Commit 54cbb76

Browse files
committed
Module initialization ported to python3
1 parent 1f87e87 commit 54cbb76

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

Modules/_librabbitmq/connection.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
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 :------------------------------------------- */
1922
PyMODINIT_FUNC init_librabbitmq(void);
2023

@@ -2242,16 +2245,35 @@ static PyMethodDef PyRabbitMQ_functions[] = {
22422245
{NULL, NULL, 0, NULL}
22432246
};
22442247

2245-
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)
22462263
{
22472264
PyObject *module, *socket_module;
22482265

22492266
if (PyType_Ready(&PyRabbitMQ_ConnectionType) < 0) {
22502267
return;
22512268
}
22522269

2253-
module = Py_InitModule3("_librabbitmq", PyRabbitMQ_functions,
2254-
"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+
22552277
if (module == NULL) {
22562278
return;
22572279
}
@@ -2281,5 +2303,9 @@ PyMODINIT_FUNC init_librabbitmq(void)
22812303
"_librabbitmq.ChannelError", NULL, NULL);
22822304
PyModule_AddObject(module, "ChannelError",
22832305
(PyObject *)PyRabbitMQExc_ChannelError);
2306+
#if PY_MAJOR_VERSION >= 3
2307+
return module;
2308+
#else
22842309
return;
2310+
#endif
22852311
}

Modules/_librabbitmq/connection.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
# define TP_FLAGS (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE)
1515
#endif
1616

17+
#if PY_MAJOR_VERSION >= 3
18+
#define PYRABBITMQ_MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
19+
#else
20+
#define PYRABBITMQ_MOD_INIT(name) PyMODINIT_FUNC init##name(void)
21+
#endif
22+
1723

1824
#if PY_VERSION_HEX >= 0x03000000 /* 3.0 and up */
1925
# define BUILD_METHOD_NAME PyUnicode_FromString

0 commit comments

Comments
 (0)