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

This commit is contained in:
Serhiy Storchaka 2025-04-08 22:08:00 +03:00 committed by GitHub
parent 8421b648e9
commit f5f1ac84b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 324 additions and 142 deletions

View file

@ -23,7 +23,6 @@
#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
@ -516,7 +515,7 @@ mmap_read_method(PyObject *op, PyObject *args)
mmap_object *self = mmap_object_CAST(op);
CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "|O&:read", _Py_convert_optional_to_ssize_t, &num_bytes))
if (!PyArg_ParseTuple(args, "|n?:read", &num_bytes))
return NULL;
CHECK_VALID(NULL);
@ -1710,7 +1709,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 = Py_None;
PyObject *tagname = NULL;
DWORD dwErr = 0;
int fileno;
HANDLE fh = 0;
@ -1720,7 +1719,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
"tagname",
"access", "offset", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "in|OiL", keywords,
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "in|U?iL", keywords,
&fileno, &map_size,
&tagname, &access, &offset)) {
return NULL;
@ -1853,13 +1852,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
m_obj->weakreflist = NULL;
m_obj->exports = 0;
/* set the tag name */
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);
}
if (tagname != NULL) {
m_obj->tagname = PyUnicode_AsWideCharString(tagname, NULL);
if (m_obj->tagname == NULL) {
Py_DECREF(m_obj);