Skip to content

Commit 750fe8c

Browse files
pyldap contributorsencukou
authored andcommitted
Modules: Use Python 3- compatible module initialization
1 parent 83763d9 commit 750fe8c

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

Modules/ldapmodule.c

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,37 @@
88

99
#include "LDAPObject.h"
1010

11+
#if PY_MAJOR_VERSION >= 3
12+
PyMODINIT_FUNC PyInit__ldap(void);
13+
#else
14+
PyMODINIT_FUNC init_ldap(void);
15+
#endif
16+
1117
#define _STR(x) #x
1218
#define STR(x) _STR(x)
1319

1420
static char version_str[] = STR(LDAPMODULE_VERSION);
1521
static char author_str[] = STR(LDAPMODULE_AUTHOR);
1622
static char license_str[] = STR(LDAPMODULE_LICENSE);
1723

18-
void
19-
LDAPinit_pkginfo( PyObject* d )
24+
static void
25+
init_pkginfo( PyObject* m )
2026
{
21-
PyObject *version;
22-
PyObject *author;
23-
PyObject *license;
24-
25-
version = PyString_FromString(version_str);
26-
author = PyString_FromString(author_str);
27-
license = PyString_FromString(license_str);
28-
29-
PyDict_SetItemString( d, "__version__", version );
30-
PyDict_SetItemString(d, "__author__", author);
31-
PyDict_SetItemString(d, "__license__", license);
32-
33-
Py_DECREF(version);
34-
Py_DECREF(author);
35-
Py_DECREF(license);
27+
PyModule_AddStringConstant(m, "__version__", version_str);
28+
PyModule_AddStringConstant(m, "__author__", author_str);
29+
PyModule_AddStringConstant(m, "__license__", license_str);
3630
}
3731

38-
DL_EXPORT(void) init_ldap(void);
39-
4032
/* dummy module methods */
41-
4233
static PyMethodDef methods[] = {
4334
{ NULL, NULL }
4435
};
4536

4637
/* module initialisation */
4738

48-
DL_EXPORT(void)
49-
init_ldap()
39+
40+
/* Common initialization code */
41+
PyObject* init_ldap_module()
5042
{
5143
PyObject *m, *d;
5244

@@ -55,12 +47,26 @@ init_ldap()
5547
#endif
5648

5749
/* Create the module and add the functions */
50+
#if PY_MAJOR_VERSION >= 3
51+
static struct PyModuleDef ldap_moduledef = {
52+
PyModuleDef_HEAD_INIT,
53+
"_ldap", /* m_name */
54+
"", /* m_doc */
55+
-1, /* m_size */
56+
methods, /* m_methods */
57+
};
58+
m = PyModule_Create(&ldap_moduledef);
59+
#else
5860
m = Py_InitModule("_ldap", methods);
61+
#endif
62+
63+
PyType_Ready(&LDAP_Type);
5964

6065
/* Add some symbolic constants to the module */
6166
d = PyModule_GetDict(m);
6267

63-
LDAPinit_pkginfo(d);
68+
init_pkginfo(m);
69+
6470
LDAPinit_constants(d);
6571
LDAPinit_errors(d);
6672
LDAPinit_functions(d);
@@ -69,4 +75,17 @@ init_ldap()
6975
/* Check for errors */
7076
if (PyErr_Occurred())
7177
Py_FatalError("can't initialize module _ldap");
78+
79+
return m;
80+
}
81+
82+
83+
#if PY_MAJOR_VERSION < 3
84+
PyMODINIT_FUNC init_ldap() {
85+
init_ldap_module();
7286
}
87+
#else
88+
PyMODINIT_FUNC PyInit__ldap() {
89+
return init_ldap_module();
90+
}
91+
#endif

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