88
99/* the base exception class */
1010
11- PyObject *
12- LDAPexception_class ;
11+ PyObject * LDAPexception_class ;
1312
1413/* list of exception classes */
1514
1615#define LDAP_ERROR_MIN LDAP_REFERRAL_LIMIT_EXCEEDED
1716
1817#ifdef LDAP_PROXIED_AUTHORIZATION_DENIED
19- #define LDAP_ERROR_MAX LDAP_PROXIED_AUTHORIZATION_DENIED
18+ #define LDAP_ERROR_MAX LDAP_PROXIED_AUTHORIZATION_DENIED
2019#else
21- #ifdef LDAP_ASSERTION_FAILED
22- #define LDAP_ERROR_MAX LDAP_ASSERTION_FAILED
23- #else
24- #define LDAP_ERROR_MAX LDAP_OTHER
25- #endif
20+ #ifdef LDAP_ASSERTION_FAILED
21+ #define LDAP_ERROR_MAX LDAP_ASSERTION_FAILED
22+ #else
23+ #define LDAP_ERROR_MAX LDAP_OTHER
24+ #endif
2625#endif
2726
2827#define LDAP_ERROR_OFFSET -LDAP_ERROR_MIN
2928
30- static PyObject * errobjects [ LDAP_ERROR_MAX - LDAP_ERROR_MIN + 1 ];
31-
29+ static PyObject * errobjects [LDAP_ERROR_MAX - LDAP_ERROR_MIN + 1 ];
3230
3331/* Convert a bare LDAP error number into an exception */
34- PyObject *
32+ PyObject *
3533LDAPerr (int errnum )
3634{
37- if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX ) {
38- PyErr_SetNone (errobjects [errnum + LDAP_ERROR_OFFSET ]);
39- } else {
40- PyObject * args = Py_BuildValue ("{s:i}" , "errnum" , errnum );
41- if (args == NULL )
42- return NULL ;
43- PyErr_SetObject (LDAPexception_class , args );
44- Py_DECREF (args );
45- }
46- return NULL ;
35+ if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX ) {
36+ PyErr_SetNone (errobjects [errnum + LDAP_ERROR_OFFSET ]);
37+ }
38+ else {
39+ PyObject * args = Py_BuildValue ("{s:i}" , "errnum" , errnum );
40+
41+ if (args == NULL )
42+ return NULL ;
43+ PyErr_SetObject (LDAPexception_class , args );
44+ Py_DECREF (args );
45+ }
46+ return NULL ;
4747}
4848
4949/* Convert an LDAP error into an informative python exception */
50- PyObject *
51- LDAPerror ( LDAP * l , char * msg )
50+ PyObject *
51+ LDAPerror (LDAP * l , char * msg )
5252{
53- if (l == NULL ) {
54- PyErr_SetFromErrno ( LDAPexception_class );
55- return NULL ;
56- }
57- else {
58- int myerrno , errnum , opt_errnum ;
59- PyObject * errobj ;
60- PyObject * info ;
61- PyObject * str ;
62- PyObject * pyerrno ;
63- char * matched , * error ;
64-
65- /* at first save errno for later use before it gets overwritten by another call */
66- myerrno = errno ;
67-
68- opt_errnum = ldap_get_option (l , LDAP_OPT_ERROR_NUMBER , & errnum );
69- if (opt_errnum != LDAP_OPT_SUCCESS )
70- errnum = opt_errnum ;
71-
72- if (errnum == LDAP_NO_MEMORY )
73- return PyErr_NoMemory ();
74-
75- if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX )
76- errobj = errobjects [errnum + LDAP_ERROR_OFFSET ];
77- else
78- errobj = LDAPexception_class ;
79-
80- info = PyDict_New ();
81- if (info == NULL )
82- return NULL ;
83-
84- str = PyUnicode_FromString (ldap_err2string (errnum ));
85- if (str )
86- PyDict_SetItemString ( info , "desc" , str );
87- Py_XDECREF (str );
88-
89- if (myerrno != 0 ) {
90- pyerrno = PyInt_FromLong (myerrno );
91- if (pyerrno )
92- PyDict_SetItemString ( info , "errno" , pyerrno );
93- Py_XDECREF (pyerrno );
53+ if (l == NULL ) {
54+ PyErr_SetFromErrno (LDAPexception_class );
55+ return NULL ;
9456 }
57+ else {
58+ int myerrno , errnum , opt_errnum ;
59+ PyObject * errobj ;
60+ PyObject * info ;
61+ PyObject * str ;
62+ PyObject * pyerrno ;
63+ char * matched , * error ;
64+
65+ /* at first save errno for later use before it gets overwritten by another call */
66+ myerrno = errno ;
67+
68+ opt_errnum = ldap_get_option (l , LDAP_OPT_ERROR_NUMBER , & errnum );
69+ if (opt_errnum != LDAP_OPT_SUCCESS )
70+ errnum = opt_errnum ;
71+
72+ if (errnum == LDAP_NO_MEMORY )
73+ return PyErr_NoMemory ();
74+
75+ if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX )
76+ errobj = errobjects [errnum + LDAP_ERROR_OFFSET ];
77+ else
78+ errobj = LDAPexception_class ;
79+
80+ info = PyDict_New ();
81+ if (info == NULL )
82+ return NULL ;
83+
84+ str = PyUnicode_FromString (ldap_err2string (errnum ));
85+ if (str )
86+ PyDict_SetItemString (info , "desc" , str );
87+ Py_XDECREF (str );
9588
96- if (ldap_get_option (l , LDAP_OPT_MATCHED_DN , & matched ) >= 0
97- && matched != NULL ) {
98- if (* matched != '\0' ) {
99- str = PyUnicode_FromString (matched );
100- if (str )
101- PyDict_SetItemString ( info , "matched" , str );
102- Py_XDECREF (str );
89+ if (myerrno != 0 ) {
90+ pyerrno = PyInt_FromLong (myerrno );
91+ if (pyerrno )
92+ PyDict_SetItemString (info , "errno" , pyerrno );
93+ Py_XDECREF (pyerrno );
10394 }
104- ldap_memfree (matched );
105- }
10695
107- if (errnum == LDAP_REFERRAL ) {
108- str = PyUnicode_FromString (msg );
109- if (str )
110- PyDict_SetItemString ( info , "info" , str );
111- Py_XDECREF (str );
112- } else if (ldap_get_option (l , LDAP_OPT_ERROR_STRING , & error ) >= 0 ) {
113- if (error != NULL && * error != '\0' ) {
114- str = PyUnicode_FromString (error );
96+ if (ldap_get_option (l , LDAP_OPT_MATCHED_DN , & matched ) >= 0
97+ && matched != NULL ) {
98+ if (* matched != '\0' ) {
99+ str = PyUnicode_FromString (matched );
100+ if (str )
101+ PyDict_SetItemString (info , "matched" , str );
102+ Py_XDECREF (str );
103+ }
104+ ldap_memfree (matched );
105+ }
106+
107+ if (errnum == LDAP_REFERRAL ) {
108+ str = PyUnicode_FromString (msg );
115109 if (str )
116- PyDict_SetItemString ( info , "info" , str );
110+ PyDict_SetItemString (info , "info" , str );
117111 Py_XDECREF (str );
118112 }
119- ldap_memfree (error );
113+ else if (ldap_get_option (l , LDAP_OPT_ERROR_STRING , & error ) >= 0 ) {
114+ if (error != NULL && * error != '\0' ) {
115+ str = PyUnicode_FromString (error );
116+ if (str )
117+ PyDict_SetItemString (info , "info" , str );
118+ Py_XDECREF (str );
119+ }
120+ ldap_memfree (error );
121+ }
122+ PyErr_SetObject (errobj , info );
123+ Py_DECREF (info );
124+ return NULL ;
120125 }
121- PyErr_SetObject ( errobj , info );
122- Py_DECREF (info );
123- return NULL ;
124- }
125126}
126127
127128/* initialise the module constants */
128129
129130int
130- LDAPinit_constants ( PyObject * m )
131+ LDAPinit_constants (PyObject * m )
131132{
132133 PyObject * exc ;
133134
134135 /* simple constants */
135136
136- if (PyModule_AddIntConstant (m , "OPT_ON" , 1 ) != 0 ) return -1 ;
137- if (PyModule_AddIntConstant (m , "OPT_OFF" , 0 ) != 0 ) return -1 ;
137+ if (PyModule_AddIntConstant (m , "OPT_ON" , 1 ) != 0 )
138+ return -1 ;
139+ if (PyModule_AddIntConstant (m , "OPT_OFF" , 0 ) != 0 )
140+ return -1 ;
138141
139142 /* exceptions */
140143
@@ -143,11 +146,13 @@ LDAPinit_constants( PyObject* m )
143146 return -1 ;
144147 }
145148
146- if (PyModule_AddObject (m , "LDAPError" , LDAPexception_class ) != 0 ) return -1 ;
149+ if (PyModule_AddObject (m , "LDAPError" , LDAPexception_class ) != 0 )
150+ return -1 ;
147151 Py_INCREF (LDAPexception_class );
148152
149153 /* XXX - backward compatibility with pre-1.8 */
150- if (PyModule_AddObject (m , "error" , LDAPexception_class ) != 0 ) return -1 ;
154+ if (PyModule_AddObject (m , "error" , LDAPexception_class ) != 0 )
155+ return -1 ;
151156 Py_INCREF (LDAPexception_class );
152157
153158 /* Generated constants -- see Lib/ldap/constants.py */
0 commit comments