Skip to content

Reformat all C code with indent #182

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

Merged
merged 1 commit into from
Mar 14, 2018
Merged
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
1,076 changes: 586 additions & 490 deletions Modules/LDAPObject.c

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions Modules/LDAPObject.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* See https://www.python-ldap.org/ for details. */

#ifndef __h_LDAPObject
#define __h_LDAPObject
#ifndef __h_LDAPObject
#define __h_LDAPObject

#include "common.h"

Expand All @@ -12,22 +12,22 @@
#endif

#if PYTHON_API_VERSION < 1007
typedef PyObject* _threadstate;
typedef PyObject *_threadstate;
#else
typedef PyThreadState* _threadstate;
typedef PyThreadState *_threadstate;
#endif

typedef struct {
PyObject_HEAD
LDAP* ldap;
_threadstate _save; /* for thread saving on referrals */
int valid;
PyObject_HEAD LDAP *ldap;
_threadstate _save; /* for thread saving on referrals */
int valid;
} LDAPObject;

extern PyTypeObject LDAP_Type;

#define LDAPObject_Check(v) (Py_TYPE(v) == &LDAP_Type)

extern LDAPObject *newLDAPObject( LDAP* );
extern LDAPObject *newLDAPObject(LDAP *);

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

Expand All @@ -48,4 +48,3 @@ extern LDAPObject *newLDAPObject( LDAP* );
}

#endif /* __h_LDAPObject */

4 changes: 2 additions & 2 deletions Modules/berval.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* See https://www.python-ldap.org/ for details. */

#ifndef __h_berval
#define __h_berval
#ifndef __h_berval
#define __h_berval

#include "common.h"
#include "lber.h"
Expand Down
15 changes: 9 additions & 6 deletions Modules/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
/* dynamically add the methods into the module dictionary d */

void
LDAPadd_methods( PyObject* d, PyMethodDef* methods )
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 );
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) {
PyObject *
LDAPerror_TypeError(const char *msg, PyObject *obj)
{
PyObject *args = Py_BuildValue("sO", msg, obj);

if (args == NULL) {
return NULL;
}
Expand Down
10 changes: 5 additions & 5 deletions Modules/common.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* common utility macros
* See https://www.python-ldap.org/ for details. */

#ifndef __h_common
#define __h_common
#ifndef __h_common
#define __h_common

#define PY_SSIZE_T_CLEAN

Expand All @@ -24,9 +24,10 @@
#define streq( a, b ) \
( (*(a)==*(b)) && 0==strcmp(a,b) )

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

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

void LDAPadd_methods( PyObject*d, PyMethodDef*methods );
#define PyNone_Check(o) ((o) == Py_None)

/* Py2/3 compatibility */
Expand All @@ -36,4 +37,3 @@ void LDAPadd_methods( PyObject*d, PyMethodDef*methods );
#endif

#endif /* __h_common_ */

189 changes: 97 additions & 92 deletions Modules/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,133 +8,136 @@

/* the base exception class */

PyObject*
LDAPexception_class;
PyObject *LDAPexception_class;

/* list of exception classes */

#define LDAP_ERROR_MIN LDAP_REFERRAL_LIMIT_EXCEEDED

#ifdef LDAP_PROXIED_AUTHORIZATION_DENIED
#define LDAP_ERROR_MAX LDAP_PROXIED_AUTHORIZATION_DENIED
#define LDAP_ERROR_MAX LDAP_PROXIED_AUTHORIZATION_DENIED
#else
#ifdef LDAP_ASSERTION_FAILED
#define LDAP_ERROR_MAX LDAP_ASSERTION_FAILED
#else
#define LDAP_ERROR_MAX LDAP_OTHER
#endif
#ifdef LDAP_ASSERTION_FAILED
#define LDAP_ERROR_MAX LDAP_ASSERTION_FAILED
#else
#define LDAP_ERROR_MAX LDAP_OTHER
#endif
#endif

#define LDAP_ERROR_OFFSET -LDAP_ERROR_MIN

static PyObject* errobjects[ LDAP_ERROR_MAX-LDAP_ERROR_MIN+1 ];

static PyObject *errobjects[LDAP_ERROR_MAX - LDAP_ERROR_MIN + 1];

/* Convert a bare LDAP error number into an exception */
PyObject*
PyObject *
LDAPerr(int errnum)
{
if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX) {
PyErr_SetNone(errobjects[errnum+LDAP_ERROR_OFFSET]);
} else {
PyObject *args = Py_BuildValue("{s:i}", "errnum", errnum);
if (args == NULL)
return NULL;
PyErr_SetObject(LDAPexception_class, args);
Py_DECREF(args);
}
return NULL;
if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX) {
PyErr_SetNone(errobjects[errnum + LDAP_ERROR_OFFSET]);
}
else {
PyObject *args = Py_BuildValue("{s:i}", "errnum", errnum);

if (args == NULL)
return NULL;
PyErr_SetObject(LDAPexception_class, args);
Py_DECREF(args);
}
return NULL;
}

/* Convert an LDAP error into an informative python exception */
PyObject*
LDAPerror( LDAP *l, char *msg )
PyObject *
LDAPerror(LDAP *l, char *msg)
{
if (l == NULL) {
PyErr_SetFromErrno( LDAPexception_class );
return NULL;
}
else {
int myerrno, errnum, opt_errnum;
PyObject *errobj;
PyObject *info;
PyObject *str;
PyObject *pyerrno;
char *matched, *error;

/* at first save errno for later use before it gets overwritten by another call */
myerrno = errno;

opt_errnum = ldap_get_option(l, LDAP_OPT_ERROR_NUMBER, &errnum);
if (opt_errnum != LDAP_OPT_SUCCESS)
errnum = opt_errnum;

if (errnum == LDAP_NO_MEMORY)
return PyErr_NoMemory();

if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX)
errobj = errobjects[errnum+LDAP_ERROR_OFFSET];
else
errobj = LDAPexception_class;

info = PyDict_New();
if (info == NULL)
return NULL;

str = PyUnicode_FromString(ldap_err2string(errnum));
if (str)
PyDict_SetItemString( info, "desc", str );
Py_XDECREF(str);

if (myerrno != 0) {
pyerrno = PyInt_FromLong(myerrno);
if (pyerrno)
PyDict_SetItemString( info, "errno", pyerrno );
Py_XDECREF(pyerrno);
if (l == NULL) {
PyErr_SetFromErrno(LDAPexception_class);
return NULL;
}
else {
int myerrno, errnum, opt_errnum;
PyObject *errobj;
PyObject *info;
PyObject *str;
PyObject *pyerrno;
char *matched, *error;

/* at first save errno for later use before it gets overwritten by another call */
myerrno = errno;

opt_errnum = ldap_get_option(l, LDAP_OPT_ERROR_NUMBER, &errnum);
if (opt_errnum != LDAP_OPT_SUCCESS)
errnum = opt_errnum;

if (errnum == LDAP_NO_MEMORY)
return PyErr_NoMemory();

if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX)
errobj = errobjects[errnum + LDAP_ERROR_OFFSET];
else
errobj = LDAPexception_class;

info = PyDict_New();
if (info == NULL)
return NULL;

str = PyUnicode_FromString(ldap_err2string(errnum));
if (str)
PyDict_SetItemString(info, "desc", str);
Py_XDECREF(str);

if (ldap_get_option(l, LDAP_OPT_MATCHED_DN, &matched) >= 0
&& matched != NULL) {
if (*matched != '\0') {
str = PyUnicode_FromString(matched);
if (str)
PyDict_SetItemString( info, "matched", str );
Py_XDECREF(str);
if (myerrno != 0) {
pyerrno = PyInt_FromLong(myerrno);
if (pyerrno)
PyDict_SetItemString(info, "errno", pyerrno);
Py_XDECREF(pyerrno);
}
ldap_memfree(matched);
}

if (errnum == LDAP_REFERRAL) {
str = PyUnicode_FromString(msg);
if (str)
PyDict_SetItemString( info, "info", str );
Py_XDECREF(str);
} else if (ldap_get_option(l, LDAP_OPT_ERROR_STRING, &error) >= 0) {
if (error != NULL && *error != '\0') {
str = PyUnicode_FromString(error);
if (ldap_get_option(l, LDAP_OPT_MATCHED_DN, &matched) >= 0
&& matched != NULL) {
if (*matched != '\0') {
str = PyUnicode_FromString(matched);
if (str)
PyDict_SetItemString(info, "matched", str);
Py_XDECREF(str);
}
ldap_memfree(matched);
}

if (errnum == LDAP_REFERRAL) {
str = PyUnicode_FromString(msg);
if (str)
PyDict_SetItemString( info, "info", str );
PyDict_SetItemString(info, "info", str);
Py_XDECREF(str);
}
ldap_memfree(error);
else if (ldap_get_option(l, LDAP_OPT_ERROR_STRING, &error) >= 0) {
if (error != NULL && *error != '\0') {
str = PyUnicode_FromString(error);
if (str)
PyDict_SetItemString(info, "info", str);
Py_XDECREF(str);
}
ldap_memfree(error);
}
PyErr_SetObject(errobj, info);
Py_DECREF(info);
return NULL;
}
PyErr_SetObject( errobj, info );
Py_DECREF(info);
return NULL;
}
}

/* initialise the module constants */

int
LDAPinit_constants( PyObject* m )
LDAPinit_constants(PyObject *m)
{
PyObject *exc;

/* simple constants */

if (PyModule_AddIntConstant(m, "OPT_ON", 1) != 0) return -1;
if (PyModule_AddIntConstant(m, "OPT_OFF", 0) != 0) return -1;
if (PyModule_AddIntConstant(m, "OPT_ON", 1) != 0)
return -1;
if (PyModule_AddIntConstant(m, "OPT_OFF", 0) != 0)
return -1;

/* exceptions */

Expand All @@ -143,11 +146,13 @@ LDAPinit_constants( PyObject* m )
return -1;
}

if (PyModule_AddObject(m, "LDAPError", LDAPexception_class) != 0) return -1;
if (PyModule_AddObject(m, "LDAPError", LDAPexception_class) != 0)
return -1;
Py_INCREF(LDAPexception_class);

/* XXX - backward compatibility with pre-1.8 */
if (PyModule_AddObject(m, "error", LDAPexception_class) != 0) return -1;
if (PyModule_AddObject(m, "error", LDAPexception_class) != 0)
return -1;
Py_INCREF(LDAPexception_class);

/* Generated constants -- see Lib/ldap/constants.py */
Expand Down
12 changes: 6 additions & 6 deletions Modules/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
#include "lber.h"
#include "ldap.h"

extern int LDAPinit_constants( PyObject* m );
extern PyObject* LDAPconstant( int );
extern int LDAPinit_constants(PyObject *m);
extern PyObject *LDAPconstant(int);

extern PyObject* LDAPexception_class;
extern PyObject* LDAPerror( LDAP*, char*msg );
PyObject* LDAPerr(int errnum);
extern PyObject *LDAPexception_class;
extern PyObject *LDAPerror(LDAP *, char *msg);
PyObject *LDAPerr(int errnum);

#ifndef LDAP_CONTROL_PAGE_OID
#define LDAP_CONTROL_PAGE_OID "1.2.840.113556.1.4.319"
#endif /* !LDAP_CONTROL_PAGE_OID */

#ifndef LDAP_CONTROL_VALUESRETURNFILTER
#define LDAP_CONTROL_VALUESRETURNFILTER "1.2.826.0.1.3344810.2.3" /* RFC 3876 */
#define LDAP_CONTROL_VALUESRETURNFILTER "1.2.826.0.1.3344810.2.3" /* RFC 3876 */
#endif /* !LDAP_CONTROL_VALUESRETURNFILTER */

#endif /* __h_constants_ */
Loading