@@ -1768,7 +1768,7 @@ builtin_locals_impl(PyObject *module)
1768
1768
static PyObject *
1769
1769
min_max (PyObject * const * args , Py_ssize_t nargs , PyObject * kwnames , int op )
1770
1770
{
1771
- PyObject * it , * item , * val , * maxitem , * maxval , * keyfunc = NULL ;
1771
+ PyObject * it = NULL , * item , * val , * maxitem , * maxval , * keyfunc = NULL ;
1772
1772
PyObject * defaultval = NULL ;
1773
1773
static const char * const keywords [] = {"key" , "default" , NULL };
1774
1774
static _PyArg_Parser _parser_min = {"|$OO:min" , keywords , 0 };
@@ -1807,13 +1807,23 @@ min_max(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, int op)
1807
1807
1808
1808
maxitem = NULL ; /* the result */
1809
1809
maxval = NULL ; /* the value associated with the result */
1810
- int i = 0 ;
1811
- while (positional ?
1812
- ((i < nargs ) && (item = args [i ++ ]))
1813
- : !!(item = PyIter_Next (it ))) {
1814
- if (positional ) {
1810
+ while (1 ) {
1811
+ if (it == NULL ) {
1812
+ if (nargs -- <= 0 ) {
1813
+ break ;
1814
+ }
1815
+ item = * args ++ ;
1815
1816
Py_INCREF (item );
1816
1817
}
1818
+ else {
1819
+ item = PyIter_Next (it );
1820
+ if (item == NULL ) {
1821
+ if (PyErr_Occurred ()) {
1822
+ goto Fail_it ;
1823
+ }
1824
+ break ;
1825
+ }
1826
+ }
1817
1827
1818
1828
/* get the value from the key function */
1819
1829
if (keyfunc != NULL ) {
@@ -1848,8 +1858,6 @@ min_max(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, int op)
1848
1858
}
1849
1859
}
1850
1860
}
1851
- if (PyErr_Occurred ())
1852
- goto Fail_it ;
1853
1861
if (maxval == NULL ) {
1854
1862
assert (maxitem == NULL );
1855
1863
if (defaultval != NULL ) {
@@ -1861,9 +1869,7 @@ min_max(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, int op)
1861
1869
}
1862
1870
else
1863
1871
Py_DECREF (maxval );
1864
- if (!positional ) {
1865
- Py_DECREF (it );
1866
- }
1872
+ Py_XDECREF (it );
1867
1873
return maxitem ;
1868
1874
1869
1875
Fail_it_item_and_val :
@@ -1873,9 +1879,7 @@ min_max(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, int op)
1873
1879
Fail_it :
1874
1880
Py_XDECREF (maxval );
1875
1881
Py_XDECREF (maxitem );
1876
- if (!positional ) {
1877
- Py_DECREF (it );
1878
- }
1882
+ Py_XDECREF (it );
1879
1883
return NULL ;
1880
1884
}
1881
1885
0 commit comments