Skip to content

Commit c37d196

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 f481010 commit c37d196

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

Modules/LDAPObject.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ attrs_from_List(PyObject *attrlist, char ***attrsp)
280280
}
281281
else {
282282
PyObject *item = NULL;
283+
PyObject *bstr;
283284
Py_ssize_t i, len, strlen;
284-
285-
const char *str;
285+
char *str;
286286

287287
seq = PySequence_Fast(attrlist, "expected list of strings or None");
288288
if (seq == NULL)
@@ -303,18 +303,29 @@ attrs_from_List(PyObject *attrlist, char ***attrsp)
303303
if (!PyUnicode_Check(item)) {
304304
LDAPerror_TypeError
305305
("attrs_from_List(): expected string in list", item);
306+
Py_DECREF(item);
307+
goto error;
308+
}
309+
// PyUnicode_AsUTF8AndSize is not in limited API < 3.10.
310+
bstr = PyUnicode_AsUTF8String(item);
311+
Py_DECREF(item);
312+
if (bstr == NULL) {
313+
goto error;
314+
}
315+
if (PyBytes_AsStringAndSize(bstr, &str, &strlen) < 0) {
316+
Py_DECREF(bstr);
306317
goto error;
307318
}
308-
str = PyUnicode_AsUTF8AndSize(item, &strlen);
309319
/* Make a copy. PyBytes_AsString* / PyUnicode_AsUTF8* return
310-
* internal values that must be treated like const char. Python
311-
* 3.7 actually returns a const char.
320+
* internal values that must be treated like const char.
312321
*/
313322
attrs[i] = (char *)PyMem_NEW(char, strlen + 1);
314-
315-
if (attrs[i] == NULL)
323+
if (attrs[i] == NULL) {
324+
Py_DECREF(bstr);
316325
goto nomem;
326+
}
317327
memcpy(attrs[i], str, strlen + 1);
328+
Py_DECREF(bstr);
318329
}
319330
attrs[len] = NULL;
320331
Py_DECREF(seq);

Modules/ldapcontrol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ LDAPControls_to_List(LDAPControl **ldcs)
186186
Py_DECREF(res);
187187
return NULL;
188188
}
189-
PyList_SET_ITEM(res, i, pyctrl);
189+
PyList_SetItem(res, i, pyctrl);
190190
}
191191
return res;
192192
}

Modules/options.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ LDAP_set_option(LDAPObject *self, int option, PyObject *value)
183183
/* TypeError: mention either float or None is expected */
184184
PyErr_Clear();
185185
PyErr_Format(PyExc_TypeError,
186-
"A float or None is expected for timeout, got %.100s",
187-
Py_TYPE(value)->tp_name);
186+
"A float or None is expected for timeout, got %S",
187+
Py_TYPE(value));
188188
}
189189
return 0;
190190
}
@@ -302,9 +302,9 @@ LDAP_get_option(LDAPObject *self, int option)
302302
num_extensions++;
303303
extensions = PyTuple_New(num_extensions);
304304
for (i = 0; i < num_extensions; i++)
305-
PyTuple_SET_ITEM(extensions, i,
306-
PyUnicode_FromString(apiinfo.ldapai_extensions
307-
[i]));
305+
PyTuple_SetItem(extensions, i,
306+
PyUnicode_FromString(apiinfo.ldapai_extensions
307+
[i]));
308308

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

0 commit comments

Comments
 (0)