Skip to content

refactor: Port to multi-phase module init #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Misc/python-ldap.supp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
fun:PyObject_Malloc
...
fun:PyErr_NewException
fun:LDAPinit_constants
fun:LDAPMod_init_constants
fun:init_ldap_module
...
}
10 changes: 10 additions & 0 deletions Modules/LDAPObject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1539,3 +1539,13 @@ PyTypeObject LDAP_Type = {
0, /*tp_members */
0, /*tp_getset */
};

int
LDAPMod_init_type(PyObject *m)
{
/* Initialize LDAP class */
if (PyType_Ready(&LDAP_Type) < 0) {
return -1;
}
return 0;
}
15 changes: 0 additions & 15 deletions Modules/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,6 @@

#include "pythonldap.h"

/* dynamically add the methods into the module dictionary d */

void
LDAPadd_methods(PyObject *d, PyMethodDef *methods)
{
PyMethodDef *meth;

for (meth = methods; meth->ml_meth; meth++) {
PyObject *f = PyCFunction_New(meth, NULL);

PyDict_SetItemString(d, meth->ml_name, f);
Py_DECREF(f);
}
}

/* Raise TypeError with custom message and object */
PyObject *
LDAPerror_TypeError(const char *msg, PyObject *obj)
Expand Down
2 changes: 1 addition & 1 deletion Modules/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ LDAPerror(LDAP *l)
/* initialise the module constants */

int
LDAPinit_constants(PyObject *m)
LDAPMod_init_constants(PyObject *m)
{
PyObject *exc, *nobj;
struct ldap_apifeature_info info = { 1, "X_OPENLDAP_THREAD_SAFE", 0 };
Expand Down
39 changes: 10 additions & 29 deletions Modules/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

/* ldap_initialize */

static PyObject *
l_ldap_initialize(PyObject *unused, PyObject *args)
PyObject *
LDAPMod_initialize(PyObject *module, PyObject *args)
{
char *uri;
LDAP *ld = NULL;
Expand All @@ -28,8 +28,8 @@ l_ldap_initialize(PyObject *unused, PyObject *args)
#ifdef HAVE_LDAP_INIT_FD
/* initialize_fd(fileno, url) */

static PyObject *
l_ldap_initialize_fd(PyObject *unused, PyObject *args)
PyObject *
LDAPMod_initialize_fd(PyObject *module, PyObject *args)
{
char *url;
LDAP *ld = NULL;
Expand Down Expand Up @@ -82,8 +82,8 @@ l_ldap_initialize_fd(PyObject *unused, PyObject *args)

/* ldap_str2dn */

static PyObject *
l_ldap_str2dn(PyObject *unused, PyObject *args)
PyObject *
LDAPMod_str2dn(PyObject *module, PyObject *args)
{
struct berval str;
LDAPDN dn;
Expand Down Expand Up @@ -157,8 +157,8 @@ l_ldap_str2dn(PyObject *unused, PyObject *args)

/* ldap_set_option (global options) */

static PyObject *
l_ldap_set_option(PyObject *self, PyObject *args)
PyObject *
LDAPMod_set_option(PyObject *module, PyObject *args)
{
PyObject *value;
int option;
Expand All @@ -173,8 +173,8 @@ l_ldap_set_option(PyObject *self, PyObject *args)

/* ldap_get_option (global options) */

static PyObject *
l_ldap_get_option(PyObject *self, PyObject *args)
PyObject *
LDAPMod_get_option(PyObject *module, PyObject *args)
{
int option;

Expand All @@ -184,22 +184,3 @@ l_ldap_get_option(PyObject *self, PyObject *args)
}

/* methods */

static PyMethodDef methods[] = {
{"initialize", (PyCFunction)l_ldap_initialize, METH_VARARGS},
#ifdef HAVE_LDAP_INIT_FD
{"initialize_fd", (PyCFunction)l_ldap_initialize_fd, METH_VARARGS},
#endif
{"str2dn", (PyCFunction)l_ldap_str2dn, METH_VARARGS},
{"set_option", (PyCFunction)l_ldap_set_option, METH_VARARGS},
{"get_option", (PyCFunction)l_ldap_get_option, METH_VARARGS},
{NULL, NULL}
};

/* initialisation */

void
LDAPinit_functions(PyObject *d)
{
LDAPadd_methods(d, methods);
}
30 changes: 8 additions & 22 deletions Modules/ldapcontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ LDAPControls_to_List(LDAPControl **ldcs)
/* --------------- en-/decoders ------------- */

/* Matched Values, aka, Values Return Filter */
static PyObject *
encode_rfc3876(PyObject *self, PyObject *args)
PyObject *
LDAPMod_encode_rfc3876(PyObject *module, PyObject *args)
{
PyObject *res = 0;
int err;
Expand Down Expand Up @@ -235,8 +235,8 @@ encode_rfc3876(PyObject *self, PyObject *args)
return res;
}

static PyObject *
encode_rfc2696(PyObject *self, PyObject *args)
PyObject *
LDAPMod_encode_rfc2696(PyObject *module, PyObject *args)
{
PyObject *res = 0;
BerElement *ber = 0;
Expand Down Expand Up @@ -291,8 +291,8 @@ encode_rfc2696(PyObject *self, PyObject *args)
return res;
}

static PyObject *
decode_rfc2696(PyObject *self, PyObject *args)
PyObject *
LDAPMod_decode_rfc2696(PyObject *module, PyObject *args)
{
PyObject *res = 0;
BerElement *ber = 0;
Expand Down Expand Up @@ -328,8 +328,8 @@ decode_rfc2696(PyObject *self, PyObject *args)
return res;
}

static PyObject *
encode_assertion_control(PyObject *self, PyObject *args)
PyObject *
LDAPMod_encode_assertion_control(PyObject *module, PyObject *args)
{
int err;
PyObject *res = 0;
Expand Down Expand Up @@ -374,17 +374,3 @@ encode_assertion_control(PyObject *self, PyObject *args)

return res;
}

static PyMethodDef methods[] = {
{"encode_page_control", encode_rfc2696, METH_VARARGS},
{"decode_page_control", decode_rfc2696, METH_VARARGS},
{"encode_valuesreturnfilter_control", encode_rfc3876, METH_VARARGS},
{"encode_assertion_control", encode_assertion_control, METH_VARARGS},
{NULL, NULL}
};

void
LDAPinit_control(PyObject *d)
{
LDAPadd_methods(d, methods);
}
77 changes: 37 additions & 40 deletions Modules/ldapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,55 @@ static char version_str[] = STR(LDAPMODULE_VERSION);
static char author_str[] = STR(LDAPMODULE_AUTHOR);
static char license_str[] = STR(LDAPMODULE_LICENSE);

static void
static int
init_pkginfo(PyObject *m)
{
PyModule_AddStringConstant(m, "__version__", version_str);
PyModule_AddStringConstant(m, "__author__", author_str);
PyModule_AddStringConstant(m, "__license__", license_str);
if (PyModule_AddStringConstant(m, "__version__", version_str) != 0)
return -1;
if (PyModule_AddStringConstant(m, "__author__", author_str) != 0)
return -1;
if (PyModule_AddStringConstant(m, "__license__", license_str) != 0)
return -1;
return 0;
}

/* dummy module methods */
static PyMethodDef methods[] = {
static PyMethodDef ldap_functions[] = {
// functions.c
{"initialize", LDAPMod_initialize, METH_VARARGS},
#ifdef HAVE_LDAP_INIT_FD
{"initialize_fd", LDAPMod_initialize_fd, METH_VARARGS},
#endif
{"str2dn", LDAPMod_str2dn, METH_VARARGS},
{"set_option", LDAPMod_set_option, METH_VARARGS},
{"get_option", LDAPMod_get_option, METH_VARARGS},
// ldapcontrol.c
{"encode_page_control", LDAPMod_encode_rfc2696, METH_VARARGS},
{"decode_page_control", LDAPMod_decode_rfc2696, METH_VARARGS},
{"encode_valuesreturnfilter_control", LDAPMod_encode_rfc3876,
METH_VARARGS},
{"encode_assertion_control", LDAPMod_encode_assertion_control,
METH_VARARGS},
{NULL, NULL}
};

/* module initialisation */
static PyModuleDef_Slot ldap_slots[] = {
{Py_mod_exec, LDAPMod_init_type},
{Py_mod_exec, LDAPMod_init_constants},
{Py_mod_exec, init_pkginfo},
{0, NULL}
};

static struct PyModuleDef ldap_moduledef = {
PyModuleDef_HEAD_INIT,
"_ldap", /* m_name */
"", /* m_doc */
-1, /* m_size */
methods, /* m_methods */
.m_name = "_ldap",
.m_size = 0,
.m_methods = ldap_functions,
.m_slots = ldap_slots,
};

/* module initialisation */

PyMODINIT_FUNC
PyInit__ldap()
{
PyObject *m, *d;

/* Create the module and add the functions */
m = PyModule_Create(&ldap_moduledef);

/* Initialize LDAP class */
if (PyType_Ready(&LDAP_Type) < 0) {
Py_DECREF(m);
return NULL;
}

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

init_pkginfo(m);

if (LDAPinit_constants(m) == -1) {
return NULL;
}

LDAPinit_functions(d);
LDAPinit_control(d);

/* Check for errors */
if (PyErr_Occurred())
Py_FatalError("can't initialize module _ldap");

return m;
return PyModuleDef_Init(&ldap_moduledef);
}
22 changes: 15 additions & 7 deletions Modules/pythonldap.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ LDAP_F(int) ldap_init_fd(ber_socket_t fd, int proto, LDAP_CONST char *url,

PYLDAP_FUNC(PyObject *) LDAPerror_TypeError(const char *, PyObject *);

PYLDAP_FUNC(void) LDAPadd_methods(PyObject *d, PyMethodDef *methods);

#define PyNone_Check(o) ((o) == Py_None)

/* *** berval *** */
PYLDAP_FUNC(PyObject *) LDAPberval_to_object(const struct berval *bv);
PYLDAP_FUNC(PyObject *) LDAPberval_to_unicode_object(const struct berval *bv);

/* *** constants *** */
PYLDAP_FUNC(int) LDAPinit_constants(PyObject *m);
PYLDAP_FUNC(int) LDAPMod_init_constants(PyObject *m);

PYLDAP_DATA(PyObject *) LDAPexception_class;
PYLDAP_FUNC(PyObject *) LDAPerror(LDAP *);
Expand All @@ -79,14 +77,14 @@ PYLDAP_FUNC(PyObject *) LDAPerr(int errnum);
#define LDAP_CONTROL_VALUESRETURNFILTER "1.2.826.0.1.3344810.2.3" /* RFC 3876 */
#endif /* !LDAP_CONTROL_VALUESRETURNFILTER */

/* *** functions *** */
PYLDAP_FUNC(void) LDAPinit_functions(PyObject *);

/* *** ldapcontrol *** */
PYLDAP_FUNC(void) LDAPinit_control(PyObject *d);
PYLDAP_FUNC(void) LDAPControl_List_DEL(LDAPControl **);
PYLDAP_FUNC(int) LDAPControls_from_object(PyObject *, LDAPControl ***);
PYLDAP_FUNC(PyObject *) LDAPControls_to_List(LDAPControl **ldcs);
PYLDAP_FUNC(PyObject *) LDAPMod_encode_rfc2696(PyObject *, PyObject *);
PYLDAP_FUNC(PyObject *) LDAPMod_decode_rfc2696(PyObject *, PyObject *);
PYLDAP_FUNC(PyObject *) LDAPMod_encode_rfc3876(PyObject *, PyObject *);
PYLDAP_FUNC(PyObject *) LDAPMod_encode_assertion_control(PyObject *, PyObject *);

/* *** ldapobject *** */
typedef struct {
Expand All @@ -97,6 +95,7 @@ typedef struct {

PYLDAP_DATA(PyTypeObject) LDAP_Type;
PYLDAP_FUNC(LDAPObject *) newLDAPObject(LDAP *);
PYLDAP_FUNC(int) LDAPMod_init_type(PyObject *module);

/* macros to allow thread saving in the context of an LDAP connection */

Expand Down Expand Up @@ -128,4 +127,13 @@ PYLDAP_FUNC(int) LDAP_set_option(LDAPObject *self, int option,
PYLDAP_FUNC(PyObject *) LDAP_get_option(LDAPObject *self, int option);
PYLDAP_FUNC(void) set_timeval_from_double(struct timeval *tv, double d);

/* *** functions *** */
PYLDAP_FUNC(PyObject *) LDAPMod_initialize(PyObject *, PyObject *);
#ifdef HAVE_LDAP_INIT_FD
PYLDAP_FUNC(PyObject *) LDAPMod_initialize_fd(PyObject *, PyObject *);
#endif
PYLDAP_FUNC(PyObject *) LDAPMod_str2dn(PyObject *, PyObject *);
PYLDAP_FUNC(PyObject *) LDAPMod_set_option(PyObject *, PyObject *);
PYLDAP_FUNC(PyObject *) LDAPMod_get_option(PyObject *, PyObject *);

#endif /* pythonldap_h */