Skip to content

Commit f481010

Browse files
committed
refactor: Remove Python 2 vestiges
The C code had a few version checks for Python 2. python-ldap requires Python >= 3.6. Signed-off-by: Christian Heimes <cheimes@redhat.com>
1 parent 75a765f commit f481010

File tree

5 files changed

+28
-84
lines changed

5 files changed

+28
-84
lines changed

Modules/LDAPObject.c

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,8 @@ attrs_from_List(PyObject *attrlist, char ***attrsp)
270270

271271
if (attrlist == Py_None) {
272272
/* None means a NULL attrlist */
273-
#if PY_MAJOR_VERSION == 2
274-
}
275-
else if (PyBytes_Check(attrlist)) {
276-
#else
277273
}
278274
else if (PyUnicode_Check(attrlist)) {
279-
#endif
280275
/* caught by John Benninghoff <johnb@netscape.com> */
281276
LDAPerror_TypeError
282277
("attrs_from_List(): expected *list* of strings, not a string",
@@ -287,11 +282,7 @@ attrs_from_List(PyObject *attrlist, char ***attrsp)
287282
PyObject *item = NULL;
288283
Py_ssize_t i, len, strlen;
289284

290-
#if PY_MAJOR_VERSION >= 3
291285
const char *str;
292-
#else
293-
char *str;
294-
#endif
295286

296287
seq = PySequence_Fast(attrlist, "expected list of strings or None");
297288
if (seq == NULL)
@@ -309,24 +300,12 @@ attrs_from_List(PyObject *attrlist, char ***attrsp)
309300
item = PySequence_Fast_GET_ITEM(seq, i);
310301
if (item == NULL)
311302
goto error;
312-
#if PY_MAJOR_VERSION == 2
313-
/* Encoded in Python to UTF-8 */
314-
if (!PyBytes_Check(item)) {
315-
LDAPerror_TypeError
316-
("attrs_from_List(): expected bytes in list", item);
317-
goto error;
318-
}
319-
if (PyBytes_AsStringAndSize(item, &str, &strlen) == -1) {
320-
goto error;
321-
}
322-
#else
323303
if (!PyUnicode_Check(item)) {
324304
LDAPerror_TypeError
325305
("attrs_from_List(): expected string in list", item);
326306
goto error;
327307
}
328308
str = PyUnicode_AsUTF8AndSize(item, &strlen);
329-
#endif
330309
/* Make a copy. PyBytes_AsString* / PyUnicode_AsUTF8* return
331310
* internal values that must be treated like const char. Python
332311
* 3.7 actually returns a const char.
@@ -515,7 +494,7 @@ l_ldap_add_ext(LDAPObject *self, PyObject *args)
515494
if (ldaperror != LDAP_SUCCESS)
516495
return LDAPerror(self->ldap);
517496

518-
return PyInt_FromLong(msgid);
497+
return PyLong_FromLong(msgid);
519498
}
520499

521500
/* ldap_simple_bind */
@@ -566,7 +545,7 @@ l_ldap_simple_bind(LDAPObject *self, PyObject *args)
566545
if (ldaperror != LDAP_SUCCESS)
567546
return LDAPerror(self->ldap);
568547

569-
return PyInt_FromLong(msgid);
548+
return PyLong_FromLong(msgid);
570549
}
571550

572551
#ifdef HAVE_SASL
@@ -724,7 +703,7 @@ l_ldap_sasl_bind_s(LDAPObject *self, PyObject *args)
724703
}
725704
else if (ldaperror != LDAP_SUCCESS)
726705
return LDAPerror(self->ldap);
727-
return PyInt_FromLong(ldaperror);
706+
return PyLong_FromLong(ldaperror);
728707
}
729708

730709
static PyObject *
@@ -751,15 +730,9 @@ l_ldap_sasl_interactive_bind_s(LDAPObject *self, PyObject *args)
751730
* unsigned int, we need to use the "I" flag if we're running Python 2.3+ and a
752731
* "i" otherwise.
753732
*/
754-
#if (PY_MAJOR_VERSION == 2) && (PY_MINOR_VERSION < 3)
755-
if (!PyArg_ParseTuple
756-
(args, "sOOOi:sasl_interactive_bind_s", &who, &SASLObject,
757-
&serverctrls, &clientctrls, &sasl_flags))
758-
#else
759733
if (!PyArg_ParseTuple
760734
(args, "sOOOI:sasl_interactive_bind_s", &who, &SASLObject,
761735
&serverctrls, &clientctrls, &sasl_flags))
762-
#endif
763736
return NULL;
764737

765738
if (not_valid(self))
@@ -803,7 +776,7 @@ l_ldap_sasl_interactive_bind_s(LDAPObject *self, PyObject *args)
803776

804777
if (msgid != LDAP_SUCCESS)
805778
return LDAPerror(self->ldap);
806-
return PyInt_FromLong(msgid);
779+
return PyLong_FromLong(msgid);
807780
}
808781
#endif
809782

@@ -852,7 +825,7 @@ l_ldap_cancel(LDAPObject *self, PyObject *args)
852825
if (ldaperror != LDAP_SUCCESS)
853826
return LDAPerror(self->ldap);
854827

855-
return PyInt_FromLong(msgid);
828+
return PyLong_FromLong(msgid);
856829
}
857830

858831
#endif
@@ -906,7 +879,7 @@ l_ldap_compare_ext(LDAPObject *self, PyObject *args)
906879
if (ldaperror != LDAP_SUCCESS)
907880
return LDAPerror(self->ldap);
908881

909-
return PyInt_FromLong(msgid);
882+
return PyLong_FromLong(msgid);
910883
}
911884

912885
/* ldap_delete_ext */
@@ -952,7 +925,7 @@ l_ldap_delete_ext(LDAPObject *self, PyObject *args)
952925
if (ldaperror != LDAP_SUCCESS)
953926
return LDAPerror(self->ldap);
954927

955-
return PyInt_FromLong(msgid);
928+
return PyLong_FromLong(msgid);
956929
}
957930

958931
/* ldap_modify_ext */
@@ -1009,7 +982,7 @@ l_ldap_modify_ext(LDAPObject *self, PyObject *args)
1009982
if (ldaperror != LDAP_SUCCESS)
1010983
return LDAPerror(self->ldap);
1011984

1012-
return PyInt_FromLong(msgid);
985+
return PyLong_FromLong(msgid);
1013986
}
1014987

1015988
/* ldap_rename */
@@ -1059,7 +1032,7 @@ l_ldap_rename(LDAPObject *self, PyObject *args)
10591032
if (ldaperror != LDAP_SUCCESS)
10601033
return LDAPerror(self->ldap);
10611034

1062-
return PyInt_FromLong(msgid);
1035+
return PyLong_FromLong(msgid);
10631036
}
10641037

10651038
/* ldap_result4 */
@@ -1275,7 +1248,7 @@ l_ldap_search_ext(LDAPObject *self, PyObject *args)
12751248
if (ldaperror != LDAP_SUCCESS)
12761249
return LDAPerror(self->ldap);
12771250

1278-
return PyInt_FromLong(msgid);
1251+
return PyLong_FromLong(msgid);
12791252
}
12801253

12811254
/* ldap_whoami_s (available since OpenLDAP 2.1.13) */
@@ -1445,7 +1418,7 @@ l_ldap_passwd(LDAPObject *self, PyObject *args)
14451418
if (ldaperror != LDAP_SUCCESS)
14461419
return LDAPerror(self->ldap);
14471420

1448-
return PyInt_FromLong(msgid);
1421+
return PyLong_FromLong(msgid);
14491422
}
14501423

14511424
/* ldap_extended_operation */
@@ -1496,7 +1469,7 @@ l_ldap_extended_operation(LDAPObject *self, PyObject *args)
14961469
if (ldaperror != LDAP_SUCCESS)
14971470
return LDAPerror(self->ldap);
14981471

1499-
return PyInt_FromLong(msgid);
1472+
return PyLong_FromLong(msgid);
15001473
}
15011474

15021475
/* methods */

Modules/constants.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,20 @@ LDAPraise_for_message(LDAP *l, LDAPMessage *m)
105105
}
106106

107107
if (msgtype > 0) {
108-
pyresult = PyInt_FromLong(msgtype);
108+
pyresult = PyLong_FromLong(msgtype);
109109
if (pyresult)
110110
PyDict_SetItemString(info, "msgtype", pyresult);
111111
Py_XDECREF(pyresult);
112112
}
113113

114114
if (msgid >= 0) {
115-
pyresult = PyInt_FromLong(msgid);
115+
pyresult = PyLong_FromLong(msgid);
116116
if (pyresult)
117117
PyDict_SetItemString(info, "msgid", pyresult);
118118
Py_XDECREF(pyresult);
119119
}
120120

121-
pyresult = PyInt_FromLong(errnum);
121+
pyresult = PyLong_FromLong(errnum);
122122
if (pyresult)
123123
PyDict_SetItemString(info, "result", pyresult);
124124
Py_XDECREF(pyresult);
@@ -129,7 +129,7 @@ LDAPraise_for_message(LDAP *l, LDAPMessage *m)
129129
Py_XDECREF(str);
130130

131131
if (myerrno != 0) {
132-
pyerrno = PyInt_FromLong(myerrno);
132+
pyerrno = PyLong_FromLong(myerrno);
133133
if (pyerrno)
134134
PyDict_SetItemString(info, "errno", pyerrno);
135135
Py_XDECREF(pyerrno);

Modules/ldapmodule.c

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
#include "pythonldap.h"
44

5-
#if PY_MAJOR_VERSION >= 3
6-
PyMODINIT_FUNC PyInit__ldap(void);
7-
#else
8-
PyMODINIT_FUNC init_ldap(void);
9-
#endif
10-
115
#define _STR(x) #x
126
#define STR(x) _STR(x)
137

@@ -28,27 +22,24 @@ static PyMethodDef methods[] = {
2822
{NULL, NULL}
2923
};
3024

25+
static struct PyModuleDef ldap_moduledef = {
26+
PyModuleDef_HEAD_INIT,
27+
"_ldap", /* m_name */
28+
"", /* m_doc */
29+
-1, /* m_size */
30+
methods, /* m_methods */
31+
};
32+
3133
/* module initialisation */
3234

33-
/* Common initialization code */
34-
PyObject *
35-
init_ldap_module(void)
35+
PyMODINIT_FUNC
36+
PyInit__ldap()
3637
{
3738
PyObject *m, *d;
3839

3940
/* Create the module and add the functions */
40-
#if PY_MAJOR_VERSION >= 3
41-
static struct PyModuleDef ldap_moduledef = {
42-
PyModuleDef_HEAD_INIT,
43-
"_ldap", /* m_name */
44-
"", /* m_doc */
45-
-1, /* m_size */
46-
methods, /* m_methods */
47-
};
4841
m = PyModule_Create(&ldap_moduledef);
49-
#else
50-
m = Py_InitModule("_ldap", methods);
51-
#endif
42+
5243
/* Initialize LDAP class */
5344
if (PyType_Ready(&LDAP_Type) < 0) {
5445
Py_DECREF(m);
@@ -73,17 +64,3 @@ init_ldap_module(void)
7364

7465
return m;
7566
}
76-
77-
#if PY_MAJOR_VERSION < 3
78-
PyMODINIT_FUNC
79-
init_ldap()
80-
{
81-
init_ldap_module();
82-
}
83-
#else
84-
PyMODINIT_FUNC
85-
PyInit__ldap()
86-
{
87-
return init_ldap_module();
88-
}
89-
#endif

Modules/options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ LDAP_get_option(LDAPObject *self, int option)
368368
res = LDAP_int_get_option(self, option, &intval);
369369
if (res != LDAP_OPT_SUCCESS)
370370
return option_error(res, "ldap_get_option");
371-
return PyInt_FromLong(intval);
371+
return PyLong_FromLong(intval);
372372

373373
#ifdef LDAP_OPT_TCP_USER_TIMEOUT
374374
case LDAP_OPT_TCP_USER_TIMEOUT:

Modules/pythonldap.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
#include <ldap.h>
1818
#include <ldap_features.h>
1919

20-
/* Py2/3 compatibility */
21-
#if PY_VERSION_HEX >= 0x03000000
22-
/* In Python 3, alias PyInt to PyLong */
23-
#define PyInt_FromLong PyLong_FromLong
24-
#endif
25-
2620
#if LDAP_VENDOR_VERSION < 20400
2721
#error Current python-ldap requires OpenLDAP 2.4.x
2822
#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