8
8
9
9
/* the base exception class */
10
10
11
- PyObject *
12
- LDAPexception_class ;
11
+ PyObject * LDAPexception_class ;
13
12
14
13
/* list of exception classes */
15
14
16
15
#define LDAP_ERROR_MIN LDAP_REFERRAL_LIMIT_EXCEEDED
17
16
18
17
#ifdef LDAP_PROXIED_AUTHORIZATION_DENIED
19
- #define LDAP_ERROR_MAX LDAP_PROXIED_AUTHORIZATION_DENIED
18
+ #define LDAP_ERROR_MAX LDAP_PROXIED_AUTHORIZATION_DENIED
20
19
#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
26
25
#endif
27
26
28
27
#define LDAP_ERROR_OFFSET -LDAP_ERROR_MIN
29
28
30
- static PyObject * errobjects [ LDAP_ERROR_MAX - LDAP_ERROR_MIN + 1 ];
31
-
29
+ static PyObject * errobjects [LDAP_ERROR_MAX - LDAP_ERROR_MIN + 1 ];
32
30
33
31
/* Convert a bare LDAP error number into an exception */
34
- PyObject *
32
+ PyObject *
35
33
LDAPerr (int errnum )
36
34
{
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 ;
47
47
}
48
48
49
49
/* 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 )
52
52
{
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 ;
94
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 );
95
88
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 );
103
94
}
104
- ldap_memfree (matched );
105
- }
106
95
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 );
115
109
if (str )
116
- PyDict_SetItemString ( info , "info" , str );
110
+ PyDict_SetItemString (info , "info" , str );
117
111
Py_XDECREF (str );
118
112
}
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 ;
120
125
}
121
- PyErr_SetObject ( errobj , info );
122
- Py_DECREF (info );
123
- return NULL ;
124
- }
125
126
}
126
127
127
128
/* initialise the module constants */
128
129
129
130
int
130
- LDAPinit_constants ( PyObject * m )
131
+ LDAPinit_constants (PyObject * m )
131
132
{
132
133
PyObject * exc ;
133
134
134
135
/* simple constants */
135
136
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 ;
138
141
139
142
/* exceptions */
140
143
@@ -143,11 +146,13 @@ LDAPinit_constants( PyObject* m )
143
146
return -1 ;
144
147
}
145
148
146
- if (PyModule_AddObject (m , "LDAPError" , LDAPexception_class ) != 0 ) return -1 ;
149
+ if (PyModule_AddObject (m , "LDAPError" , LDAPexception_class ) != 0 )
150
+ return -1 ;
147
151
Py_INCREF (LDAPexception_class );
148
152
149
153
/* 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 ;
151
156
Py_INCREF (LDAPexception_class );
152
157
153
158
/* Generated constants -- see Lib/ldap/constants.py */
0 commit comments