Skip to content

Commit b45e0c3

Browse files
committed
refactor: Use functions from limited API
Replace unsafe macros and direct struct access with functions from the subset of limited API functions. * `PySequence_Fast_GET_ITEM` -> `PySequence_GetItem` * `PyTuple_SET_ITEM` -> `PyTuple_SetItem` * `PyList_SET_ITEM` -> `PyList_SetItem` * `PyUnicode_AsUTF8AndSize` -> `PyUnicode_AsUTF8String` + `PyBytes_AsStringAndSize`. The function `PyUnicode_AsUTF8AndSize` is not in limited API before Python 3.10. * `const char *tp_name` -> `Py_TYPE()` string representation See: #540 Signed-off-by: Christian Heimes <cheimes@redhat.com>
1 parent 2229d83 commit b45e0c3

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

Modules/LDAPObject.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,9 @@ attrs_from_List(PyObject *attrlist, char ***attrsp)
291291
}
292292
else {
293293
PyObject *item = NULL;
294+
PyObject *bstr;
294295
Py_ssize_t i, len, strlen;
295-
296-
#if PY_MAJOR_VERSION >= 3
297-
const char *str;
298-
#else
299296
char *str;
300-
#endif
301297

302298
seq = PySequence_Fast(attrlist, "expected list of strings or None");
303299
if (seq == NULL)
@@ -312,36 +308,36 @@ attrs_from_List(PyObject *attrlist, char ***attrsp)
312308

313309
for (i = 0; i < len; i++) {
314310
attrs[i] = NULL;
315-
item = PySequence_Fast_GET_ITEM(seq, i);
316-
if (item == NULL)
311+
item = PySequence_GetItem(seq, i);
312+
if (item == NULL) {
317313
goto error;
318-
#if PY_MAJOR_VERSION == 2
319-
/* Encoded in Python to UTF-8 */
320-
if (!PyBytes_Check(item)) {
314+
}
315+
if (!PyUnicode_Check(item)) {
321316
LDAPerror_TypeError
322-
("attrs_from_List(): expected bytes in list", item);
317+
("attrs_from_List(): expected string in list", item);
318+
Py_DECREF(item);
323319
goto error;
324320
}
325-
if (PyBytes_AsStringAndSize(item, &str, &strlen) == -1) {
321+
// PyUnicode_AsUTF8AndSize is not in limited API < 3.10.
322+
bstr = PyUnicode_AsUTF8String(item);
323+
Py_DECREF(item);
324+
if (bstr == NULL) {
326325
goto error;
327326
}
328-
#else
329-
if (!PyUnicode_Check(item)) {
330-
LDAPerror_TypeError
331-
("attrs_from_List(): expected string in list", item);
327+
if (PyBytes_AsStringAndSize(bstr, &str, &strlen) < 0) {
328+
Py_DECREF(bstr);
332329
goto error;
333330
}
334-
str = PyUnicode_AsUTF8AndSize(item, &strlen);
335-
#endif
336331
/* Make a copy. PyBytes_AsString* / PyUnicode_AsUTF8* return
337-
* internal values that must be treated like const char. Python
338-
* 3.7 actually returns a const char.
332+
* internal values that must be treated like const char.
339333
*/
340334
attrs[i] = (char *)PyMem_NEW(char, strlen + 1);
341-
342-
if (attrs[i] == NULL)
335+
if (attrs[i] == NULL) {
336+
Py_DECREF(bstr);
343337
goto nomem;
338+
}
344339
memcpy(attrs[i], str, strlen + 1);
340+
Py_DECREF(bstr);
345341
}
346342
attrs[len] = NULL;
347343
Py_DECREF(seq);

Modules/ldapcontrol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ LDAPControls_to_List(LDAPControl **ldcs)
190190
Py_DECREF(res);
191191
return NULL;
192192
}
193-
PyList_SET_ITEM(res, i, pyctrl);
193+
PyList_SetItem(res, i, pyctrl);
194194
}
195195
return res;
196196
}

Modules/options.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ LDAP_set_option(LDAPObject *self, int option, PyObject *value)
188188
/* TypeError: mention either float or None is expected */
189189
PyErr_Clear();
190190
PyErr_Format(PyExc_TypeError,
191-
"A float or None is expected for timeout, got %.100s",
192-
Py_TYPE(value)->tp_name);
191+
"A float or None is expected for timeout, got %S",
192+
Py_TYPE(value));
193193
}
194194
return 0;
195195
}
@@ -307,9 +307,9 @@ LDAP_get_option(LDAPObject *self, int option)
307307
num_extensions++;
308308
extensions = PyTuple_New(num_extensions);
309309
for (i = 0; i < num_extensions; i++)
310-
PyTuple_SET_ITEM(extensions, i,
311-
PyUnicode_FromString(apiinfo.ldapai_extensions
312-
[i]));
310+
PyTuple_SetItem(extensions, i,
311+
PyUnicode_FromString(apiinfo.ldapai_extensions
312+
[i]));
313313

314314
/* return api info as a dictionary */
315315
v = Py_BuildValue("{s:i, s:i, s:i, s:s, s:i, s:O}",

0 commit comments

Comments
 (0)