Skip to content

Commit 43b7ef3

Browse files
pythongh-110093: Replace trivial Py_BuildValue() with direct C API call
1 parent 05079d9 commit 43b7ef3

13 files changed

+33
-32
lines changed

Modules/_ctypes/callproc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ static PyObject *load_library(PyObject *self, PyObject *args)
14051405
#ifdef _WIN64
14061406
return PyLong_FromVoidPtr(hMod);
14071407
#else
1408-
return Py_BuildValue("i", hMod);
1408+
return PyLong_FromLong((int)hMod);
14091409
#endif
14101410
}
14111411

Modules/_cursesmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,7 +3673,7 @@ _curses_mousemask_impl(PyObject *module, unsigned long newmask)
36733673
#endif
36743674

36753675
/*[clinic input]
3676-
_curses.napms
3676+
_curses.napms -> int
36773677
36783678
ms: int
36793679
Duration in milliseconds.
@@ -3682,13 +3682,13 @@ _curses.napms
36823682
Sleep for specified time.
36833683
[clinic start generated code]*/
36843684

3685-
static PyObject *
3685+
static int
36863686
_curses_napms_impl(PyObject *module, int ms)
3687-
/*[clinic end generated code: output=a40a1da2e39ea438 input=20cd3af2b6900f56]*/
3687+
/*[clinic end generated code: output=5f292a6a724491bd input=c6d6e01f2f1df9f7]*/
36883688
{
36893689
PyCursesInitialised;
36903690

3691-
return Py_BuildValue("i", napms(ms));
3691+
return napms(ms);
36923692
}
36933693

36943694

Modules/_datetimemodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4046,8 +4046,8 @@ static PyObject *
40464046
timezone_getinitargs(PyDateTime_TimeZone *self, PyObject *Py_UNUSED(ignored))
40474047
{
40484048
if (self->name == NULL)
4049-
return Py_BuildValue("(O)", self->offset);
4050-
return Py_BuildValue("(OO)", self->offset, self->name);
4049+
return PyTuple_Pack(1, self->offset);
4050+
return PyTuple_Pack(2, self->offset, self->name);
40514051
}
40524052

40534053
static PyMethodDef timezone_methods[] = {

Modules/_opcode.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ _opcode_get_nb_ops_impl(PyObject *module)
244244
}
245245
#define ADD_NB_OP(NUM, STR) \
246246
do { \
247-
PyObject *pair = Py_BuildValue( \
248-
"NN", PyUnicode_FromString(#NUM), PyUnicode_FromString(STR)); \
247+
PyObject *pair = Py_BuildValue("ss", #NUM, STR); \
249248
if (pair == NULL) { \
250249
Py_DECREF(list); \
251250
return NULL; \

Modules/_winapi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class DWORD_return_converter(CReturnConverter):
212212
self.declare(data)
213213
self.err_occurred_if("_return_value == PY_DWORD_MAX", data)
214214
data.return_conversion.append(
215-
'return_value = Py_BuildValue("k", _return_value);\n')
215+
'return_value = PyLong_FromUnsignedLong(_return_value);\n')
216216
217217
class LPVOID_return_converter(CReturnConverter):
218218
type = 'LPVOID'
@@ -223,7 +223,7 @@ class LPVOID_return_converter(CReturnConverter):
223223
data.return_conversion.append(
224224
'return_value = HANDLE_TO_PYNUM(_return_value);\n')
225225
[python start generated code]*/
226-
/*[python end generated code: output=da39a3ee5e6b4b0d input=011ee0c3a2244bfe]*/
226+
/*[python end generated code: output=da39a3ee5e6b4b0d input=ef52a757a1830d92]*/
227227

228228
#include "clinic/_winapi.c.h"
229229

Modules/_zoneinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ zoneinfo_ZoneInfo__unpickle_impl(PyTypeObject *type, PyTypeObject *cls,
817817
/*[clinic end generated code: output=556712fc709deecb input=6ac8c73eed3de316]*/
818818
{
819819
if (from_cache) {
820-
PyObject *val_args = Py_BuildValue("(O)", key);
820+
PyObject *val_args = PyTuple_Pack(1, key);
821821
if (val_args == NULL) {
822822
return NULL;
823823
}

Modules/clinic/_cursesmodule.c.h

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/clinic/_winapi.c.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/overlapped.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,7 @@ _overlapped_FormatMessage_impl(PyObject *module, DWORD code)
599599
if (n) {
600600
while (iswspace(lpMsgBuf[n-1]))
601601
--n;
602-
lpMsgBuf[n] = L'\0';
603-
res = Py_BuildValue("u", lpMsgBuf);
602+
res = PyUnicode_FromWideChar(lpMsgBuf, n);
604603
} else {
605604
res = PyUnicode_FromFormat("unknown error code %u", code);
606605
}

Modules/posixmodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11018,9 +11018,9 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
1101811018

1101911019
done:
1102011020
#if !defined(HAVE_LARGEFILE_SUPPORT)
11021-
return Py_BuildValue("l", sbytes);
11021+
return PyLong_FromLong(sbytes);
1102211022
#else
11023-
return Py_BuildValue("L", sbytes);
11023+
return PyLong_FromLongLong(sbytes);
1102411024
#endif
1102511025

1102611026
#else
@@ -11033,7 +11033,7 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
1103311033
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1103411034
if (ret < 0)
1103511035
return (!async_err) ? posix_error() : NULL;
11036-
return Py_BuildValue("n", ret);
11036+
return PyLong_FromSsize_t(ret);
1103711037
}
1103811038
#endif
1103911039
off_t offset;
@@ -11054,7 +11054,7 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
1105411054
return (!async_err) ? posix_error() : NULL;
1105511055

1105611056
if (offset >= st.st_size) {
11057-
return Py_BuildValue("i", 0);
11057+
return PyLong_FromLong(0);
1105811058
}
1105911059

1106011060
// On illumos specifically sendfile() may perform a partial write but
@@ -11080,7 +11080,7 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
1108011080
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1108111081
if (ret < 0)
1108211082
return (!async_err) ? posix_error() : NULL;
11083-
return Py_BuildValue("n", ret);
11083+
return PyLong_FromSsize_t(ret);
1108411084
#endif
1108511085
}
1108611086
#endif /* HAVE_SENDFILE */

Modules/pyexpat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ static PyObject *
895895
pyexpat_xmlparser_GetBase_impl(xmlparseobject *self)
896896
/*[clinic end generated code: output=2886cb21f9a8739a input=918d71c38009620e]*/
897897
{
898-
return Py_BuildValue("z", XML_GetBase(self->itself));
898+
return conv_string_to_unicode(XML_GetBase(self->itself));
899899
}
900900

901901
/*[clinic input]
@@ -1585,7 +1585,7 @@ static PyObject *
15851585
pyexpat_ErrorString_impl(PyObject *module, long code)
15861586
/*[clinic end generated code: output=2feae50d166f2174 input=cc67de010d9e62b3]*/
15871587
{
1588-
return Py_BuildValue("z", XML_ErrorString((int)code));
1588+
return conv_string_to_unicode(XML_ErrorString((int)code));
15891589
}
15901590

15911591
/* List of methods defined in the module */

Modules/signalmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ signal_strsignal_impl(PyObject *module, int signalnum)
656656
Py_RETURN_NONE;
657657
#endif
658658

659-
return Py_BuildValue("s", res);
659+
return PyUnicode_FromString(res);
660660
}
661661

662662
#ifdef HAVE_SIGINTERRUPT

Modules/socketmodule.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,9 +1489,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
14891489
#if defined(__NetBSD__) || defined(__DragonFly__)
14901490
return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
14911491
#else /* __NetBSD__ || __DragonFly__ */
1492-
PyObject *ret = NULL;
1493-
ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1494-
return ret;
1492+
return PyLong_FromLong(_BT_HCI_MEMB(a, dev));
14951493
#endif /* !(__NetBSD__ || __DragonFly__) */
14961494
}
14971495

0 commit comments

Comments
 (0)