Skip to content

Commit b7699fb

Browse files
C API: Add support of nullable arguments in PyArg_Parse
1 parent c3677be commit b7699fb

5 files changed

Lines changed: 76 additions & 72 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3620,8 +3620,7 @@ _validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags)
36203620
PyObject *name = Py_None;
36213621
PyObject *defval;
36223622
PyObject *typ;
3623-
if (!PyArg_ParseTuple(item, "i|OO", &flag, &name, &defval) ||
3624-
!(name == Py_None || PyUnicode_Check(name)))
3623+
if (!PyArg_ParseTuple(item, "i|?UO", &flag, &name, &defval))
36253624
{
36263625
PyErr_SetString(PyExc_TypeError,
36273626
"paramflags must be a sequence of (int [,string [,value]]) tuples");
@@ -3686,10 +3685,8 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
36863685
void *handle;
36873686
PyObject *paramflags = NULL;
36883687

3689-
if (!PyArg_ParseTuple(args, "O|O", &ftuple, &paramflags))
3688+
if (!PyArg_ParseTuple(args, "O|?O", &ftuple, &paramflags))
36903689
return NULL;
3691-
if (paramflags == Py_None)
3692-
paramflags = NULL;
36933690

36943691
ftuple = PySequence_Tuple(ftuple);
36953692
if (!ftuple)
@@ -3804,10 +3801,8 @@ PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds)
38043801
GUID *iid = NULL;
38053802
Py_ssize_t iid_len = 0;
38063803

3807-
if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, &paramflags, &iid, &iid_len))
3804+
if (!PyArg_ParseTuple(args, "is|?Oz#", &index, &name, &paramflags, &iid, &iid_len))
38083805
return NULL;
3809-
if (paramflags == Py_None)
3810-
paramflags = NULL;
38113806

38123807
ctypes_state *st = get_module_state_by_def(Py_TYPE(type));
38133808
if (!_validate_paramflags(st, type, paramflags)) {

Modules/_json.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,23 +1208,16 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
12081208
static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
12091209

12101210
PyEncoderObject *s;
1211-
PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1211+
PyObject *markers = Py_None, *defaultfn, *encoder, *indent, *key_separator;
12121212
PyObject *item_separator;
12131213
int sort_keys, skipkeys, allow_nan;
12141214

1215-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOUUppp:make_encoder", kwlist,
1216-
&markers, &defaultfn, &encoder, &indent,
1215+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "?O!OOOUUppp:make_encoder", kwlist,
1216+
&PyDict_Type, &markers, &defaultfn, &encoder, &indent,
12171217
&key_separator, &item_separator,
12181218
&sort_keys, &skipkeys, &allow_nan))
12191219
return NULL;
12201220

1221-
if (markers != Py_None && !PyDict_Check(markers)) {
1222-
PyErr_Format(PyExc_TypeError,
1223-
"make_encoder() argument 1 must be dict or None, "
1224-
"not %.200s", Py_TYPE(markers)->tp_name);
1225-
return NULL;
1226-
}
1227-
12281221
s = (PyEncoderObject *)type->tp_alloc(type, 0);
12291222
if (s == NULL)
12301223
return NULL;

Modules/_threadmodule.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,10 +1878,10 @@ thread_PyThread_start_joinable_thread(PyObject *module, PyObject *fargs,
18781878
PyObject *func = NULL;
18791879
int daemon = 1;
18801880
thread_module_state *state = get_thread_state(module);
1881-
PyObject *hobj = NULL;
1881+
PyObject *hobj = Py_None;
18821882
if (!PyArg_ParseTupleAndKeywords(fargs, fkwargs,
1883-
"O|Op:start_joinable_thread", keywords,
1884-
&func, &hobj, &daemon)) {
1883+
"O|?O!p:start_joinable_thread", keywords,
1884+
&func, state->thread_handle_type, &hobj, &daemon)) {
18851885
return NULL;
18861886
}
18871887

@@ -1891,14 +1891,6 @@ thread_PyThread_start_joinable_thread(PyObject *module, PyObject *fargs,
18911891
return NULL;
18921892
}
18931893

1894-
if (hobj == NULL) {
1895-
hobj = Py_None;
1896-
}
1897-
else if (hobj != Py_None && !Py_IS_TYPE(hobj, state->thread_handle_type)) {
1898-
PyErr_SetString(PyExc_TypeError, "'handle' must be a _ThreadHandle");
1899-
return NULL;
1900-
}
1901-
19021894
if (PySys_Audit("_thread.start_joinable_thread", "OiO", func, daemon,
19031895
hobj) < 0) {
19041896
return NULL;

Modules/mmapmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#endif
2424

2525
#include <Python.h>
26-
#include "pycore_abstract.h" // _Py_convert_optional_to_ssize_t()
2726
#include "pycore_bytesobject.h" // _PyBytes_Find()
2827
#include "pycore_fileutils.h" // _Py_stat_struct
2928

@@ -512,7 +511,7 @@ mmap_read_method(mmap_object *self,
512511
Py_ssize_t num_bytes = PY_SSIZE_T_MAX, remaining;
513512

514513
CHECK_VALID(NULL);
515-
if (!PyArg_ParseTuple(args, "|O&:read", _Py_convert_optional_to_ssize_t, &num_bytes))
514+
if (!PyArg_ParseTuple(args, "|?n:read", &num_bytes))
516515
return NULL;
517516
CHECK_VALID(NULL);
518517

0 commit comments

Comments
 (0)