Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}


Expand Down
4 changes: 2 additions & 2 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down
3 changes: 1 addition & 2 deletions Modules/_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand Down
4 changes: 2 additions & 2 deletions Modules/_winapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions Modules/clinic/_cursesmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Modules/clinic/_winapi.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Modules/overlapped.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__) */
}

Expand Down