Skip to content

Commit 60207bd

Browse files
Use _PyTuple_FromPair in Modules(2)
1 parent 473d2a3 commit 60207bd

File tree

10 files changed

+61
-93
lines changed

10 files changed

+61
-93
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ bytes(cdata)
111111
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
112112
#include "pycore_pyatomic_ft_wrappers.h"
113113
#include "pycore_object.h"
114+
#include "pycore_tuple.h" // _PyTuple_FromPair
114115
#ifdef MS_WIN32
115116
# include "pycore_modsupport.h" // _PyArg_NoKeywords()
116117
#endif
@@ -3511,7 +3512,7 @@ _PyCData_set(ctypes_state *st,
35113512
only it's object list. So we create a tuple, containing
35123513
b_objects list PLUS the array itself, and return that!
35133514
*/
3514-
return PyTuple_Pack(2, keep, value);
3515+
return _PyTuple_FromPair(keep, value);
35153516
}
35163517
PyErr_Format(PyExc_TypeError,
35173518
"incompatible types, %s instance instead of %s instance",
@@ -5332,8 +5333,7 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)
53325333
len = PyLong_FromSsize_t(length);
53335334
if (len == NULL)
53345335
return NULL;
5335-
key = PyTuple_Pack(2, itemtype, len);
5336-
Py_DECREF(len);
5336+
key = _PyTuple_FromPairSteal(Py_NewRef(itemtype), len);
53375337
if (!key)
53385338
return NULL;
53395339

Modules/_datetimemodule.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pycore_long.h" // _PyLong_GetOne()
1414
#include "pycore_object.h" // _PyObject_Init()
1515
#include "pycore_time.h" // _PyTime_ObjectToTime_t()
16+
#include "pycore_tuple.h" // _PyTuple_FromPair
1617
#include "pycore_unicodeobject.h" // _PyUnicode_Copy()
1718
#include "pycore_initconfig.h" // _PyStatus_OK()
1819
#include "pycore_pyatomic_ft_wrappers.h"
@@ -2692,7 +2693,7 @@ delta_divmod(PyObject *left, PyObject *right)
26922693
Py_DECREF(divmod);
26932694
return NULL;
26942695
}
2695-
result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta);
2696+
result = _PyTuple_FromPair(PyTuple_GET_ITEM(divmod, 0), delta);
26962697
Py_DECREF(delta);
26972698
Py_DECREF(divmod);
26982699
return result;
@@ -4496,7 +4497,7 @@ timezone_getinitargs(PyObject *op, PyObject *Py_UNUSED(dummy))
44964497
PyDateTime_TimeZone *self = PyTimeZone_CAST(op);
44974498
if (self->name == NULL)
44984499
return PyTuple_Pack(1, self->offset);
4499-
return PyTuple_Pack(2, self->offset, self->name);
4500+
return _PyTuple_FromPair(self->offset, self->name);
45004501
}
45014502

45024503
static PyMethodDef timezone_methods[] = {
@@ -5247,7 +5248,7 @@ time_getstate(PyDateTime_Time *self, int proto)
52475248
if (! HASTZINFO(self) || self->tzinfo == Py_None)
52485249
result = PyTuple_Pack(1, basestate);
52495250
else
5250-
result = PyTuple_Pack(2, basestate, self->tzinfo);
5251+
result = _PyTuple_FromPair(basestate, self->tzinfo);
52515252
Py_DECREF(basestate);
52525253
}
52535254
return result;
@@ -7169,7 +7170,7 @@ datetime_getstate(PyDateTime_DateTime *self, int proto)
71697170
if (! HASTZINFO(self) || self->tzinfo == Py_None)
71707171
result = PyTuple_Pack(1, basestate);
71717172
else
7172-
result = PyTuple_Pack(2, basestate, self->tzinfo);
7173+
result = _PyTuple_FromPair(basestate, self->tzinfo);
71737174
Py_DECREF(basestate);
71747175
}
71757176
return result;

Modules/_interpretersmodule.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "pycore_pybuffer.h" // _PyBuffer_ReleaseInInterpreterAndRawFree()
1616
#include "pycore_pylifecycle.h" // _PyInterpreterConfig_AsDict()
1717
#include "pycore_pystate.h" // _PyInterpreterState_IsRunningMain()
18+
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
1819

1920
#include "marshal.h" // PyMarshal_ReadObjectFromString()
2021

@@ -796,10 +797,7 @@ get_summary(PyInterpreterState *interp)
796797
Py_DECREF(idobj);
797798
return NULL;
798799
}
799-
PyObject *res = PyTuple_Pack(2, idobj, whenceobj);
800-
Py_DECREF(idobj);
801-
Py_DECREF(whenceobj);
802-
return res;
800+
return _PyTuple_FromPairSteal(idobj, whenceobj);
803801
}
804802

805803

Modules/_ssl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "pycore_long.h" // _PyLong_UnsignedLongLong_Converter()
3131
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
3232
#include "pycore_time.h" // _PyDeadline_Init()
33+
#include "pycore_tuple.h" // _PyTuple_FromPair
3334

3435
/* Include symbols from _socket module */
3536
#include "socketmodule.h"
@@ -6796,7 +6797,7 @@ do { \
67966797
}
67976798

67986799
/* ssl.CertificateError used to be a subclass of ValueError */
6799-
bases = PyTuple_Pack(2, state->PySSLErrorObject, PyExc_ValueError);
6800+
bases = _PyTuple_FromPair(state->PySSLErrorObject, PyExc_ValueError);
68006801
if (bases == NULL) {
68016802
goto error;
68026803
}

Modules/cjkcodecs/multibytecodec.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "multibytecodec.h"
1414
#include "clinic/multibytecodec.c.h"
15+
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
1516

1617
#include <stddef.h> // offsetof()
1718

@@ -102,26 +103,17 @@ static PyObject *multibytecodec_encode(const MultibyteCodec *,
102103
static PyObject *
103104
make_tuple(PyObject *object, Py_ssize_t len)
104105
{
105-
PyObject *v, *w;
106-
107-
if (object == NULL)
108-
return NULL;
109-
110-
v = PyTuple_New(2);
111-
if (v == NULL) {
112-
Py_DECREF(object);
106+
if (object == NULL) {
113107
return NULL;
114108
}
115-
PyTuple_SET_ITEM(v, 0, object);
116109

117-
w = PyLong_FromSsize_t(len);
110+
PyObject* w = PyLong_FromSsize_t(len);
118111
if (w == NULL) {
119-
Py_DECREF(v);
112+
Py_DECREF(object);
120113
return NULL;
121114
}
122-
PyTuple_SET_ITEM(v, 1, w);
123115

124-
return v;
116+
return _PyTuple_FromPairSteal(object, w);
125117
}
126118

127119
static PyObject *

Modules/overlapped.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#endif
1313

1414
#include "Python.h"
15+
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
1516

1617
#define WINDOWS_LEAN_AND_MEAN
1718
#include <winsock2.h>
@@ -964,18 +965,14 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
964965
}
965966

966967
// The result is a two item tuple: (message, address)
967-
self->read_from.result = PyTuple_New(2);
968+
// first item: message
969+
// second item: address
970+
self->read_from.result = _PyTuple_FromPairSteal(
971+
Py_NewRef(self->read_from.allocated_buffer), addr);
968972
if (self->read_from.result == NULL) {
969-
Py_CLEAR(addr);
970973
return NULL;
971974
}
972975

973-
// first item: message
974-
PyTuple_SET_ITEM(self->read_from.result, 0,
975-
Py_NewRef(self->read_from.allocated_buffer));
976-
// second item: address
977-
PyTuple_SET_ITEM(self->read_from.result, 1, addr);
978-
979976
return Py_NewRef(self->read_from.result);
980977
case TYPE_READ_FROM_INTO:
981978
// unparse the address
@@ -987,18 +984,14 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
987984
}
988985

989986
// The result is a two item tuple: (number of bytes read, address)
990-
self->read_from_into.result = PyTuple_New(2);
987+
// first item: number of bytes read
988+
// second item: address
989+
self->read_from_into.result = _PyTuple_FromPairSteal(
990+
PyLong_FromUnsignedLong((unsigned long)transferred), addr);
991991
if (self->read_from_into.result == NULL) {
992-
Py_CLEAR(addr);
993992
return NULL;
994993
}
995994

996-
// first item: number of bytes read
997-
PyTuple_SET_ITEM(self->read_from_into.result, 0,
998-
PyLong_FromUnsignedLong((unsigned long)transferred));
999-
// second item: address
1000-
PyTuple_SET_ITEM(self->read_from_into.result, 1, addr);
1001-
1002995
return Py_NewRef(self->read_from_into.result);
1003996
default:
1004997
return PyLong_FromUnsignedLong((unsigned long) transferred);

Modules/posixmodule.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "pycore_pystate.h" // _PyInterpreterState_GET()
2828
#include "pycore_signal.h" // Py_NSIG
2929
#include "pycore_time.h" // _PyLong_FromTime_t()
30+
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
3031
#include "pycore_typeobject.h" // _PyType_AddMethod()
3132

3233
#ifndef MS_WINDOWS
@@ -11288,9 +11289,7 @@ build_itimerspec(const struct itimerspec* curr_value)
1128811289
Py_DECREF(value);
1128911290
return NULL;
1129011291
}
11291-
PyObject *tuple = PyTuple_Pack(2, value, interval);
11292-
Py_DECREF(interval);
11293-
Py_DECREF(value);
11292+
PyObject *tuple = _PyTuple_FromPairSteal(value, interval);
1129411293
return tuple;
1129511294
}
1129611295

Modules/selectmodule.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "Python.h"
1616
#include "pycore_fileutils.h" // _Py_set_inheritable()
1717
#include "pycore_time.h" // _PyTime_FromSecondsObject()
18+
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
1819

1920
#include <stdbool.h>
2021
#include <stddef.h> // offsetof()
@@ -1075,9 +1076,7 @@ select_devpoll_poll_impl(devpollObject *self, PyObject *timeout_obj)
10751076
Py_XDECREF(num2);
10761077
goto error;
10771078
}
1078-
value = PyTuple_Pack(2, num1, num2);
1079-
Py_DECREF(num1);
1080-
Py_DECREF(num2);
1079+
value = _PyTuple_FromPairSteal(num1, num2);
10811080
if (value == NULL)
10821081
goto error;
10831082
PyList_SET_ITEM(result_list, i, value);

Modules/signalmodule.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "pycore_pystate.h" // _PyThreadState_GET()
1515
#include "pycore_signal.h" // _Py_RestoreSignals()
1616
#include "pycore_time.h" // _PyTime_FromSecondsObject()
17+
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
1718

1819
#ifndef MS_WINDOWS
1920
# include "posixmodule.h" // _PyLong_FromUid()
@@ -193,27 +194,16 @@ double_from_timeval(struct timeval *tv)
193194
static PyObject *
194195
itimer_retval(struct itimerval *iv)
195196
{
196-
PyObject *r, *v;
197-
198-
r = PyTuple_New(2);
199-
if (r == NULL)
200-
return NULL;
201-
202-
if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
203-
Py_DECREF(r);
197+
PyObject *value = PyFloat_FromDouble(double_from_timeval(&iv->it_value))
198+
if (value == NULL) {
204199
return NULL;
205200
}
206-
207-
PyTuple_SET_ITEM(r, 0, v);
208-
209-
if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
210-
Py_DECREF(r);
201+
PyObject *interval = PyFloat_FromDouble(double_from_timeval(&iv->it_interval));
202+
if (interval == NULL) {
203+
Py_DECREF(value);
211204
return NULL;
212205
}
213-
214-
PyTuple_SET_ITEM(r, 1, v);
215-
216-
return r;
206+
return _PyTuple_FromPairSteal(value, interval);
217207
}
218208
#endif
219209

0 commit comments

Comments
 (0)