Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
15d81efb
Kaydet (Commit)
15d81efb
authored
May 04, 2001
tarafından
Tim Peters
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Generalize reduce() to work with iterators.
NEEDS DOC CHANGES.
üst
8bc10b0c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
12 deletions
+33
-12
test_iter.py
Lib/test/test_iter.py
+13
-0
NEWS
Misc/NEWS
+1
-0
bltinmodule.c
Python/bltinmodule.c
+19
-12
No files found.
Lib/test/test_iter.py
Dosyayı görüntüle @
15d81efb
...
...
@@ -385,4 +385,17 @@ class TestCase(unittest.TestCase):
except
OSError
:
pass
# Test reduces()'s use of iterators.
def
test_builtin_reduce
(
self
):
from
operator
import
add
self
.
assertEqual
(
reduce
(
add
,
SequenceClass
(
5
)),
10
)
self
.
assertEqual
(
reduce
(
add
,
SequenceClass
(
5
),
42
),
52
)
self
.
assertRaises
(
TypeError
,
reduce
,
add
,
SequenceClass
(
0
))
self
.
assertEqual
(
reduce
(
add
,
SequenceClass
(
0
),
42
),
42
)
self
.
assertEqual
(
reduce
(
add
,
SequenceClass
(
1
)),
0
)
self
.
assertEqual
(
reduce
(
add
,
SequenceClass
(
1
),
42
),
42
)
d
=
{
"one"
:
1
,
"two"
:
2
,
"three"
:
3
}
self
.
assertEqual
(
reduce
(
add
,
d
),
""
.
join
(
d
.
keys
()))
run_unittest
(
TestCase
)
Misc/NEWS
Dosyayı görüntüle @
15d81efb
...
...
@@ -22,6 +22,7 @@ Core
map()
max()
min()
reduce()
What's New in Python 2.1 (final)?
...
...
Python/bltinmodule.c
Dosyayı görüntüle @
15d81efb
...
...
@@ -1851,26 +1851,25 @@ is printed without a trailing newline before reading.";
static
PyObject
*
builtin_reduce
(
PyObject
*
self
,
PyObject
*
args
)
{
PyObject
*
seq
,
*
func
,
*
result
=
NULL
;
PySequenceMethods
*
sqf
;
register
int
i
;
PyObject
*
seq
,
*
func
,
*
result
=
NULL
,
*
it
;
if
(
!
PyArg_ParseTuple
(
args
,
"OO|O:reduce"
,
&
func
,
&
seq
,
&
result
))
return
NULL
;
if
(
result
!=
NULL
)
Py_INCREF
(
result
);
sqf
=
seq
->
ob_type
->
tp_as_sequence
;
if
(
sqf
==
NULL
||
sqf
->
sq_item
==
NULL
)
{
it
=
PyObject_GetIter
(
seq
)
;
if
(
it
==
NULL
)
{
PyErr_SetString
(
PyExc_TypeError
,
"reduce() arg 2 must be a sequence"
);
"reduce() arg 2 must support iteration"
);
Py_XDECREF
(
result
);
return
NULL
;
}
if
((
args
=
PyTuple_New
(
2
))
==
NULL
)
goto
Fail
;
for
(
i
=
0
;
;
++
i
)
{
for
(
;;
)
{
PyObject
*
op2
;
if
(
args
->
ob_refcnt
>
1
)
{
...
...
@@ -1879,12 +1878,18 @@ builtin_reduce(PyObject *self, PyObject *args)
goto
Fail
;
}
if
((
op2
=
(
*
sqf
->
sq_item
)(
seq
,
i
))
==
NULL
)
{
if
(
PyErr_ExceptionMatches
(
PyExc_IndexError
))
{
PyErr_Clear
();
break
;
op2
=
PyIter_Next
(
it
);
if
(
op2
==
NULL
)
{
/* StopIteration is *implied* by a NULL return from
* PyIter_Next() if PyErr_Occurred() is false.
*/
if
(
PyErr_Occurred
())
{
if
(
PyErr_ExceptionMatches
(
PyExc_StopIteration
))
PyErr_Clear
();
else
goto
Fail
;
}
goto
Fail
;
break
;
}
if
(
result
==
NULL
)
...
...
@@ -1903,11 +1908,13 @@ builtin_reduce(PyObject *self, PyObject *args)
PyErr_SetString
(
PyExc_TypeError
,
"reduce() of empty sequence with no initial value"
);
Py_DECREF
(
it
);
return
result
;
Fail:
Py_XDECREF
(
args
);
Py_XDECREF
(
result
);
Py_DECREF
(
it
);
return
NULL
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment