Skip to content

Commit 2b9155a

Browse files
serhiy-storchakaGlyphack
authored andcommitted
pythongh-110093: Replace trivial Py_BuildValue() with direct C API call (pythonGH-110094)
1 parent 5414527 commit 2b9155a

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
@@ -4060,8 +4060,8 @@ static PyObject *
40604060
timezone_getinitargs(PyDateTime_TimeZone *self, PyObject *Py_UNUSED(ignored))
40614061
{
40624062
if (self->name == NULL)
4063-
return Py_BuildValue("(O)", self->offset);
4064-
return Py_BuildValue("(OO)", self->offset, self->name);
4063+
return PyTuple_Pack(1, self->offset);
4064+
return PyTuple_Pack(2, self->offset, self->name);
40654065
}
40664066

40674067
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
@@ -816,7 +816,7 @@ zoneinfo_ZoneInfo__unpickle_impl(PyTypeObject *type, PyTypeObject *cls,
816816
/*[clinic end generated code: output=556712fc709deecb input=6ac8c73eed3de316]*/
817817
{
818818
if (from_cache) {
819-
PyObject *val_args = Py_BuildValue("(O)", key);
819+
PyObject *val_args = PyTuple_Pack(1, key);
820820
if (val_args == NULL) {
821821
return NULL;
822822
}

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
@@ -11276,9 +11276,9 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
1127611276

1127711277
done:
1127811278
#if !defined(HAVE_LARGEFILE_SUPPORT)
11279-
return Py_BuildValue("l", sbytes);
11279+
return PyLong_FromLong(sbytes);
1128011280
#else
11281-
return Py_BuildValue("L", sbytes);
11281+
return PyLong_FromLongLong(sbytes);
1128211282
#endif
1128311283

1128411284
#else
@@ -11291,7 +11291,7 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
1129111291
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1129211292
if (ret < 0)
1129311293
return (!async_err) ? posix_error() : NULL;
11294-
return Py_BuildValue("n", ret);
11294+
return PyLong_FromSsize_t(ret);
1129511295
}
1129611296
#endif
1129711297
off_t offset;
@@ -11312,7 +11312,7 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
1131211312
return (!async_err) ? posix_error() : NULL;
1131311313

1131411314
if (offset >= st.st_size) {
11315-
return Py_BuildValue("i", 0);
11315+
return PyLong_FromLong(0);
1131611316
}
1131711317

1131811318
// 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,
1133811338
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1133911339
if (ret < 0)
1134011340
return (!async_err) ? posix_error() : NULL;
11341-
return Py_BuildValue("n", ret);
11341+
return PyLong_FromSsize_t(ret);
1134211342
#endif
1134311343
}
1134411344
#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
@@ -657,7 +657,7 @@ signal_strsignal_impl(PyObject *module, int signalnum)
657657
Py_RETURN_NONE;
658658
#endif
659659

660-
return Py_BuildValue("s", res);
660+
return PyUnicode_FromString(res);
661661
}
662662

663663
#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)