gh-110093: Replace trivial Py_BuildValue() with direct C API call (GH-110094)

This commit is contained in:
Serhiy Storchaka 2023-10-20 18:08:41 +03:00 committed by GitHub
parent ff4e53cb74
commit 59ea0f523e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 33 additions and 32 deletions

View file

@ -1405,7 +1405,7 @@ static PyObject *load_library(PyObject *self, PyObject *args)
#ifdef _WIN64
return PyLong_FromVoidPtr(hMod);
#else
return Py_BuildValue("i", hMod);
return PyLong_FromLong((int)hMod);
#endif
}

View file

@ -3673,7 +3673,7 @@ _curses_mousemask_impl(PyObject *module, unsigned long newmask)
#endif
/*[clinic input]
_curses.napms
_curses.napms -> int
ms: int
Duration in milliseconds.
@ -3682,13 +3682,13 @@ _curses.napms
Sleep for specified time.
[clinic start generated code]*/
static PyObject *
static int
_curses_napms_impl(PyObject *module, int ms)
/*[clinic end generated code: output=a40a1da2e39ea438 input=20cd3af2b6900f56]*/
/*[clinic end generated code: output=5f292a6a724491bd input=c6d6e01f2f1df9f7]*/
{
PyCursesInitialised;
return Py_BuildValue("i", napms(ms));
return napms(ms);
}

View file

@ -4060,8 +4060,8 @@ static PyObject *
timezone_getinitargs(PyDateTime_TimeZone *self, PyObject *Py_UNUSED(ignored))
{
if (self->name == NULL)
return Py_BuildValue("(O)", self->offset);
return Py_BuildValue("(OO)", self->offset, self->name);
return PyTuple_Pack(1, self->offset);
return PyTuple_Pack(2, self->offset, self->name);
}
static PyMethodDef timezone_methods[] = {

View file

@ -244,8 +244,7 @@ _opcode_get_nb_ops_impl(PyObject *module)
}
#define ADD_NB_OP(NUM, STR) \
do { \
PyObject *pair = Py_BuildValue( \
"NN", PyUnicode_FromString(#NUM), PyUnicode_FromString(STR)); \
PyObject *pair = Py_BuildValue("ss", #NUM, STR); \
if (pair == NULL) { \
Py_DECREF(list); \
return NULL; \

View file

@ -212,7 +212,7 @@ class DWORD_return_converter(CReturnConverter):
self.declare(data)
self.err_occurred_if("_return_value == PY_DWORD_MAX", data)
data.return_conversion.append(
'return_value = Py_BuildValue("k", _return_value);\n')
'return_value = PyLong_FromUnsignedLong(_return_value);\n')
class LPVOID_return_converter(CReturnConverter):
type = 'LPVOID'
@ -223,7 +223,7 @@ class LPVOID_return_converter(CReturnConverter):
data.return_conversion.append(
'return_value = HANDLE_TO_PYNUM(_return_value);\n')
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=011ee0c3a2244bfe]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=ef52a757a1830d92]*/
#include "clinic/_winapi.c.h"

View file

@ -816,7 +816,7 @@ zoneinfo_ZoneInfo__unpickle_impl(PyTypeObject *type, PyTypeObject *cls,
/*[clinic end generated code: output=556712fc709deecb input=6ac8c73eed3de316]*/
{
if (from_cache) {
PyObject *val_args = Py_BuildValue("(O)", key);
PyObject *val_args = PyTuple_Pack(1, key);
if (val_args == NULL) {
return NULL;
}

View file

@ -3163,7 +3163,7 @@ PyDoc_STRVAR(_curses_napms__doc__,
#define _CURSES_NAPMS_METHODDEF \
{"napms", (PyCFunction)_curses_napms, METH_O, _curses_napms__doc__},
static PyObject *
static int
_curses_napms_impl(PyObject *module, int ms);
static PyObject *
@ -3171,12 +3171,17 @@ _curses_napms(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
int ms;
int _return_value;
ms = PyLong_AsInt(arg);
if (ms == -1 && PyErr_Occurred()) {
goto exit;
}
return_value = _curses_napms_impl(module, ms);
_return_value = _curses_napms_impl(module, ms);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyLong_FromLong((long)_return_value);
exit:
return return_value;
@ -4313,4 +4318,4 @@ _curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored
#ifndef _CURSES_USE_DEFAULT_COLORS_METHODDEF
#define _CURSES_USE_DEFAULT_COLORS_METHODDEF
#endif /* !defined(_CURSES_USE_DEFAULT_COLORS_METHODDEF) */
/*[clinic end generated code: output=24ad16254d1eef9c input=a9049054013a1b77]*/
/*[clinic end generated code: output=96887782374f070a input=a9049054013a1b77]*/

View file

@ -568,7 +568,7 @@ _winapi_GetExitCodeProcess(PyObject *module, PyObject *arg)
if ((_return_value == PY_DWORD_MAX) && PyErr_Occurred()) {
goto exit;
}
return_value = Py_BuildValue("k", _return_value);
return_value = PyLong_FromUnsignedLong(_return_value);
exit:
return return_value;
@ -595,7 +595,7 @@ _winapi_GetLastError(PyObject *module, PyObject *Py_UNUSED(ignored))
if ((_return_value == PY_DWORD_MAX) && PyErr_Occurred()) {
goto exit;
}
return_value = Py_BuildValue("k", _return_value);
return_value = PyLong_FromUnsignedLong(_return_value);
exit:
return return_value;
@ -1305,7 +1305,7 @@ _winapi_GetFileType(PyObject *module, PyObject *const *args, Py_ssize_t nargs, P
if ((_return_value == PY_DWORD_MAX) && PyErr_Occurred()) {
goto exit;
}
return_value = Py_BuildValue("k", _return_value);
return_value = PyLong_FromUnsignedLong(_return_value);
exit:
return return_value;
@ -1479,4 +1479,4 @@ exit:
return return_value;
}
/*[clinic end generated code: output=aaf29735c47f55fe input=a9049054013a1b77]*/
/*[clinic end generated code: output=e1a9908bb82a6379 input=a9049054013a1b77]*/

View file

@ -599,8 +599,7 @@ _overlapped_FormatMessage_impl(PyObject *module, DWORD code)
if (n) {
while (iswspace(lpMsgBuf[n-1]))
--n;
lpMsgBuf[n] = L'\0';
res = Py_BuildValue("u", lpMsgBuf);
res = PyUnicode_FromWideChar(lpMsgBuf, n);
} else {
res = PyUnicode_FromFormat("unknown error code %u", code);
}

View file

@ -11276,9 +11276,9 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
done:
#if !defined(HAVE_LARGEFILE_SUPPORT)
return Py_BuildValue("l", sbytes);
return PyLong_FromLong(sbytes);
#else
return Py_BuildValue("L", sbytes);
return PyLong_FromLongLong(sbytes);
#endif
#else
@ -11291,7 +11291,7 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
if (ret < 0)
return (!async_err) ? posix_error() : NULL;
return Py_BuildValue("n", ret);
return PyLong_FromSsize_t(ret);
}
#endif
off_t offset;
@ -11312,7 +11312,7 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
return (!async_err) ? posix_error() : NULL;
if (offset >= st.st_size) {
return Py_BuildValue("i", 0);
return PyLong_FromLong(0);
}
// On illumos specifically sendfile() may perform a partial write but
@ -11338,7 +11338,7 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
if (ret < 0)
return (!async_err) ? posix_error() : NULL;
return Py_BuildValue("n", ret);
return PyLong_FromSsize_t(ret);
#endif
}
#endif /* HAVE_SENDFILE */

View file

@ -895,7 +895,7 @@ static PyObject *
pyexpat_xmlparser_GetBase_impl(xmlparseobject *self)
/*[clinic end generated code: output=2886cb21f9a8739a input=918d71c38009620e]*/
{
return Py_BuildValue("z", XML_GetBase(self->itself));
return conv_string_to_unicode(XML_GetBase(self->itself));
}
/*[clinic input]
@ -1585,7 +1585,7 @@ static PyObject *
pyexpat_ErrorString_impl(PyObject *module, long code)
/*[clinic end generated code: output=2feae50d166f2174 input=cc67de010d9e62b3]*/
{
return Py_BuildValue("z", XML_ErrorString((int)code));
return conv_string_to_unicode(XML_ErrorString((int)code));
}
/* List of methods defined in the module */

View file

@ -657,7 +657,7 @@ signal_strsignal_impl(PyObject *module, int signalnum)
Py_RETURN_NONE;
#endif
return Py_BuildValue("s", res);
return PyUnicode_FromString(res);
}
#ifdef HAVE_SIGINTERRUPT

View file

@ -1489,9 +1489,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
#if defined(__NetBSD__) || defined(__DragonFly__)
return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
#else /* __NetBSD__ || __DragonFly__ */
PyObject *ret = NULL;
ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
return ret;
return PyLong_FromLong(_BT_HCI_MEMB(a, dev));
#endif /* !(__NetBSD__ || __DragonFly__) */
}