Revert "gh-112068: C API: Add support of nullable arguments in PyArg_Parse (GH-121303)" (#136991)

This commit is contained in:
Serhiy Storchaka 2025-07-22 16:39:50 +03:00 committed by GitHub
parent b31e5d6de1
commit 3a89dfe32b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 144 additions and 322 deletions

View file

@ -3937,7 +3937,9 @@ _validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags)
PyObject *name = Py_None;
PyObject *defval;
PyObject *typ;
if (!PyArg_ParseTuple(item, "i|U?O", &flag, &name, &defval)) {
if (!PyArg_ParseTuple(item, "i|OO", &flag, &name, &defval) ||
!(name == Py_None || PyUnicode_Check(name)))
{
PyErr_SetString(PyExc_TypeError,
"paramflags must be a sequence of (int [,string [,value]]) tuples");
return 0;
@ -4002,8 +4004,10 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
void *handle;
PyObject *paramflags = NULL;
if (!PyArg_ParseTuple(args, "O|O?", &ftuple, &paramflags))
if (!PyArg_ParseTuple(args, "O|O", &ftuple, &paramflags))
return NULL;
if (paramflags == Py_None)
paramflags = NULL;
ftuple = PySequence_Tuple(ftuple);
if (!ftuple)
@ -4135,8 +4139,10 @@ PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds)
GUID *iid = NULL;
Py_ssize_t iid_len = 0;
if (!PyArg_ParseTuple(args, "is|O?z#", &index, &name, &paramflags, &iid, &iid_len))
if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, &paramflags, &iid, &iid_len))
return NULL;
if (paramflags == Py_None)
paramflags = NULL;
ctypes_state *st = get_module_state_by_def(Py_TYPE(type));
if (!_validate_paramflags(st, type, paramflags)) {

View file

@ -1415,11 +1415,14 @@ interp_get_config(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *idobj = NULL;
int restricted = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O?|$p:get_config", kwlist,
"O|$p:get_config", kwlist,
&idobj, &restricted))
{
return NULL;
}
if (idobj == Py_None) {
idobj = NULL;
}
int reqready = 0;
PyInterpreterState *interp = \
@ -1536,14 +1539,14 @@ capture_exception(PyObject *self, PyObject *args, PyObject *kwds)
static char *kwlist[] = {"exc", NULL};
PyObject *exc_arg = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"|O?:capture_exception", kwlist,
"|O:capture_exception", kwlist,
&exc_arg))
{
return NULL;
}
PyObject *exc = exc_arg;
if (exc == NULL) {
if (exc == NULL || exc == Py_None) {
exc = PyErr_GetRaisedException();
if (exc == NULL) {
Py_RETURN_NONE;

View file

@ -1222,16 +1222,23 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
PyEncoderObject *s;
PyObject *markers = Py_None, *defaultfn, *encoder, *indent, *key_separator;
PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
PyObject *item_separator;
int sort_keys, skipkeys, allow_nan;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!?OOOUUppp:make_encoder", kwlist,
&PyDict_Type, &markers, &defaultfn, &encoder, &indent,
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOUUppp:make_encoder", kwlist,
&markers, &defaultfn, &encoder, &indent,
&key_separator, &item_separator,
&sort_keys, &skipkeys, &allow_nan))
return NULL;
if (markers != Py_None && !PyDict_Check(markers)) {
PyErr_Format(PyExc_TypeError,
"make_encoder() argument 1 must be dict or None, "
"not %.200s", Py_TYPE(markers)->tp_name);
return NULL;
}
s = (PyEncoderObject *)type->tp_alloc(type, 0);
if (s == NULL)
return NULL;

View file

@ -681,12 +681,12 @@ PyThreadHandleObject_join(PyObject *op, PyObject *args)
PyThreadHandleObject *self = PyThreadHandleObject_CAST(op);
PyObject *timeout_obj = NULL;
if (!PyArg_ParseTuple(args, "|O?:join", &timeout_obj)) {
if (!PyArg_ParseTuple(args, "|O:join", &timeout_obj)) {
return NULL;
}
PyTime_t timeout_ns = -1;
if (timeout_obj != NULL) {
if (timeout_obj != NULL && timeout_obj != Py_None) {
if (_PyTime_FromSecondsObject(&timeout_ns, timeout_obj,
_PyTime_ROUND_TIMEOUT) < 0) {
return NULL;
@ -1957,10 +1957,10 @@ thread_PyThread_start_joinable_thread(PyObject *module, PyObject *fargs,
PyObject *func = NULL;
int daemon = 1;
thread_module_state *state = get_thread_state(module);
PyObject *hobj = Py_None;
PyObject *hobj = NULL;
if (!PyArg_ParseTupleAndKeywords(fargs, fkwargs,
"O|O!?p:start_joinable_thread", keywords,
&func, state->thread_handle_type, &hobj, &daemon)) {
"O|Op:start_joinable_thread", keywords,
&func, &hobj, &daemon)) {
return NULL;
}
@ -1970,6 +1970,14 @@ thread_PyThread_start_joinable_thread(PyObject *module, PyObject *fargs,
return NULL;
}
if (hobj == NULL) {
hobj = Py_None;
}
else if (hobj != Py_None && !Py_IS_TYPE(hobj, state->thread_handle_type)) {
PyErr_SetString(PyExc_TypeError, "'handle' must be a _ThreadHandle");
return NULL;
}
if (PySys_Audit("_thread.start_joinable_thread", "OiO", func, daemon,
hobj) < 0) {
return NULL;

View file

@ -23,6 +23,7 @@
#endif
#include <Python.h>
#include "pycore_abstract.h" // _Py_convert_optional_to_ssize_t()
#include "pycore_bytesobject.h" // _PyBytes_Find()
#include "pycore_fileutils.h" // _Py_stat_struct
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
@ -529,7 +530,7 @@ mmap_read_method(PyObject *op, PyObject *args)
mmap_object *self = mmap_object_CAST(op);
CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "|n?:read", &num_bytes))
if (!PyArg_ParseTuple(args, "|O&:read", _Py_convert_optional_to_ssize_t, &num_bytes))
return NULL;
CHECK_VALID(NULL);
@ -1723,7 +1724,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
DWORD off_lo; /* lower 32 bits of offset */
DWORD size_hi; /* upper 32 bits of size */
DWORD size_lo; /* lower 32 bits of size */
PyObject *tagname = NULL;
PyObject *tagname = Py_None;
DWORD dwErr = 0;
int fileno;
HANDLE fh = 0;
@ -1733,7 +1734,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
"tagname",
"access", "offset", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "in|U?iL", keywords,
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "in|OiL", keywords,
&fileno, &map_size,
&tagname, &access, &offset)) {
return NULL;
@ -1866,7 +1867,13 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
m_obj->weakreflist = NULL;
m_obj->exports = 0;
/* set the tag name */
if (tagname != NULL) {
if (!Py_IsNone(tagname)) {
if (!PyUnicode_Check(tagname)) {
Py_DECREF(m_obj);
return PyErr_Format(PyExc_TypeError, "expected str or None for "
"'tagname', not %.200s",
Py_TYPE(tagname)->tp_name);
}
m_obj->tagname = PyUnicode_AsWideCharString(tagname, NULL);
if (m_obj->tagname == NULL) {
Py_DECREF(m_obj);