Skip to content

Commit bbc263e

Browse files
committed
Simplify argument parsing logic
Instead of having different flavours of PyArgs_ParseTuple we can instead use a single one where all arguments are optional, then check the type of the first argument (when present). The main difference in this new implementation is that instead of raising a TypeError if the argument is not a str object in the final case, we simply assert it, making it clear that the higher-level API that calls us ensures this is the case. Signed-off-by: Rodrigo Tobar <[email protected]>
1 parent 1c40835 commit bbc263e

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

src/c/_cffi_backend.c

+16-22
Original file line numberDiff line numberDiff line change
@@ -4433,32 +4433,30 @@ static void *b_do_dlopen(PyObject *args, const char **p_printable_filename,
44334433
the filename (utf-8-encoded). '*p_temp' will be set either to NULL or
44344434
to a temporary object that must be freed after looking at printable_filename.
44354435
*/
4436-
void *handle;
4436+
PyObject *filename = NULL;
44374437
int flags = 0;
44384438
*p_temp = NULL;
44394439
*auto_close = 1;
4440-
4441-
if (PyTuple_GET_SIZE(args) == 0 || PyTuple_GET_ITEM(args, 0) == Py_None) {
4442-
PyObject *dummy;
4443-
if (!PyArg_ParseTuple(args, "|Oi:load_library",
4444-
&dummy, &flags))
4445-
return NULL;
4440+
4441+
if (!PyArg_ParseTuple(args, "|Oi:load_library", &filename, &flags))
4442+
return NULL;
4443+
4444+
if (filename == NULL || filename == Py_None) {
44464445
*p_printable_filename = "<None>";
44474446
return b_do_dlopen_dispatch(NULL, flags, *p_printable_filename);
44484447
}
4449-
else if (CData_Check(PyTuple_GET_ITEM(args, 0)))
4448+
4449+
if (CData_Check(filename))
44504450
{
4451-
CDataObject *cd;
4452-
if (!PyArg_ParseTuple(args, "O|i:load_library", &cd, &flags))
4453-
return NULL;
4451+
CDataObject *cd = (CDataObject *)filename;
44544452
/* 'flags' is accepted but ignored in this case */
44554453
if ((cd->c_type->ct_flags & CT_IS_VOID_PTR) == 0) {
44564454
PyErr_Format(PyExc_TypeError,
44574455
"dlopen() takes a file name or 'void *' handle, not '%s'",
44584456
cd->c_type->ct_name);
44594457
return NULL;
44604458
}
4461-
handle = cd->c_data;
4459+
void *handle = cd->c_data;
44624460
if (handle == NULL) {
44634461
PyErr_Format(PyExc_RuntimeError, "cannot call dlopen(NULL)");
44644462
return NULL;
@@ -4468,17 +4466,13 @@ static void *b_do_dlopen(PyObject *args, const char **p_printable_filename,
44684466
*auto_close = 0;
44694467
return handle;
44704468
}
4471-
else
4472-
{
4473-
PyObject *filename_unicode;
4474-
if (!PyArg_ParseTuple(args, "U|i:load_library", &filename_unicode, &flags))
4475-
return NULL;
4476-
*p_printable_filename = PyUnicode_AsUTF8(filename_unicode);
4477-
if (*p_printable_filename == NULL) {
4478-
return NULL;
4479-
}
4480-
return b_do_dlopen_dispatch(filename_unicode, flags, *p_printable_filename);
4469+
4470+
assert(PyUnicode_Check(filename));
4471+
*p_printable_filename = PyUnicode_AsUTF8(filename);
4472+
if (*p_printable_filename == NULL) {
4473+
return NULL;
44814474
}
4475+
return b_do_dlopen_dispatch(filename, flags, *p_printable_filename);
44824476
}
44834477

44844478
static PyObject *b_load_library(PyObject *self, PyObject *args)

0 commit comments

Comments
 (0)