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>
@@ -896,6 +897,7 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
896897 BOOL ret ;
897898 DWORD err ;
898899 PyObject * addr ;
900+ PyObject * transferred_obj ;
899901
900902 if (self -> type == TYPE_NONE ) {
901903 PyErr_SetString (PyExc_ValueError , "operation not yet attempted" );
@@ -964,18 +966,12 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
964966 }
965967
966968 // The result is a two item tuple: (message, address)
967- self -> read_from .result = PyTuple_New (2 );
969+ self -> read_from .result = _PyTuple_FromPairSteal (
970+ Py_NewRef (self -> read_from .allocated_buffer ), addr );
968971 if (self -> read_from .result == NULL ) {
969- Py_CLEAR (addr );
970972 return NULL ;
971973 }
972974
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-
979975 return Py_NewRef (self -> read_from .result );
980976 case TYPE_READ_FROM_INTO :
981977 // unparse the address
@@ -986,19 +982,19 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
986982 return NULL ;
987983 }
988984
985+ transferred_obj = PyLong_FromUnsignedLong ((unsigned long )transferred );
986+ if (transferred_obj == NULL ) {
987+ Py_DECREF (addr );
988+ return NULL ;
989+ }
990+
989991 // The result is a two item tuple: (number of bytes read, address)
990- self -> read_from_into .result = PyTuple_New (2 );
992+ self -> read_from_into .result = _PyTuple_FromPairSteal (
993+ transferred_obj , addr );
991994 if (self -> read_from_into .result == NULL ) {
992- Py_CLEAR (addr );
993995 return NULL ;
994996 }
995997
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-
1002998 return Py_NewRef (self -> read_from_into .result );
1003999 default :
10041000 return PyLong_FromUnsignedLong ((unsigned long ) transferred );
0 commit comments