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
50e90e26
Kaydet (Commit)
50e90e26
authored
Eki 04, 2007
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
itertools.count() no longer limited to sys.maxint.
üst
8f669370
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
17 deletions
+82
-17
itertools.rst
Doc/library/itertools.rst
+3
-7
test_itertools.py
Lib/test/test_itertools.py
+6
-1
NEWS
Misc/NEWS
+3
-0
itertoolsmodule.c
Modules/itertoolsmodule.c
+70
-9
No files found.
Doc/library/itertools.rst
Dosyayı görüntüle @
50e90e26
...
@@ -79,19 +79,15 @@ loops that truncate the stream.
...
@@ -79,19 +79,15 @@ loops that truncate the stream.
.. function:: count([n])
.. function:: count([n])
Make an iterator that returns consecutive integers starting with *n*. If not
Make an iterator that returns consecutive integers starting with *n*. If not
specified *n* defaults to zero. Does not currently support python long
specified *n* defaults to zero. Often used as an argument to :func:`imap` to
integers. Often used as an argument to :func:`imap` to generate consecutive
generate consecutive data points. Also, used with :func:`izip` to add sequence
data points. Also, used with :func:`izip` to add sequence numbers. Equivalent
numbers. Equivalent to::
to::
def count(n=0):
def count(n=0):
while True:
while True:
yield n
yield n
n += 1
n += 1
Note, :func:`count` does not check for overflow and will return negative numbers
after exceeding ``sys.maxint``. This behavior may change in the future.
.. function:: cycle(iterable)
.. function:: cycle(iterable)
...
...
Lib/test/test_itertools.py
Dosyayı görüntüle @
50e90e26
...
@@ -52,9 +52,12 @@ class TestBasicOps(unittest.TestCase):
...
@@ -52,9 +52,12 @@ class TestBasicOps(unittest.TestCase):
self
.
assertEqual
(
zip
(
'abc'
,
count
()),
[(
'a'
,
0
),
(
'b'
,
1
),
(
'c'
,
2
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
()),
[(
'a'
,
0
),
(
'b'
,
1
),
(
'c'
,
2
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
(
3
)),
[(
'a'
,
3
),
(
'b'
,
4
),
(
'c'
,
5
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
(
3
)),
[(
'a'
,
3
),
(
'b'
,
4
),
(
'c'
,
5
)])
self
.
assertEqual
(
take
(
2
,
zip
(
'abc'
,
count
(
3
))),
[(
'a'
,
3
),
(
'b'
,
4
)])
self
.
assertEqual
(
take
(
2
,
zip
(
'abc'
,
count
(
3
))),
[(
'a'
,
3
),
(
'b'
,
4
)])
self
.
assertEqual
(
take
(
2
,
zip
(
'abc'
,
count
(
-
1
))),
[(
'a'
,
-
1
),
(
'b'
,
0
)])
self
.
assertEqual
(
take
(
2
,
zip
(
'abc'
,
count
(
-
3
))),
[(
'a'
,
-
3
),
(
'b'
,
-
2
)])
self
.
assertRaises
(
TypeError
,
count
,
2
,
3
)
self
.
assertRaises
(
TypeError
,
count
,
2
,
3
)
self
.
assertRaises
(
TypeError
,
count
,
'a'
)
self
.
assertRaises
(
TypeError
,
count
,
'a'
)
self
.
assertRaises
(
OverflowError
,
list
,
islice
(
count
(
maxsize
-
5
),
10
))
self
.
assertEqual
(
list
(
islice
(
count
(
maxsize
-
5
),
10
)),
range
(
maxsize
-
5
,
maxsize
+
5
))
self
.
assertEqual
(
list
(
islice
(
count
(
-
maxsize
-
5
),
10
)),
range
(
-
maxsize
-
5
,
-
maxsize
+
5
))
c
=
count
(
3
)
c
=
count
(
3
)
self
.
assertEqual
(
repr
(
c
),
'count(3)'
)
self
.
assertEqual
(
repr
(
c
),
'count(3)'
)
c
.
next
()
c
.
next
()
...
@@ -63,6 +66,8 @@ class TestBasicOps(unittest.TestCase):
...
@@ -63,6 +66,8 @@ class TestBasicOps(unittest.TestCase):
self
.
assertEqual
(
repr
(
c
),
'count(-9)'
)
self
.
assertEqual
(
repr
(
c
),
'count(-9)'
)
c
.
next
()
c
.
next
()
self
.
assertEqual
(
c
.
next
(),
-
8
)
self
.
assertEqual
(
c
.
next
(),
-
8
)
for
i
in
(
-
sys
.
maxint
-
5
,
-
sys
.
maxint
+
5
,
-
10
,
-
1
,
0
,
10
,
sys
.
maxint
-
5
,
sys
.
maxint
+
5
):
self
.
assertEqual
(
repr
(
count
(
i
)),
'count(
%
r)'
%
i
)
def
test_cycle
(
self
):
def
test_cycle
(
self
):
self
.
assertEqual
(
take
(
10
,
cycle
(
'abc'
)),
list
(
'abcabcabca'
))
self
.
assertEqual
(
take
(
10
,
cycle
(
'abc'
)),
list
(
'abcabcabca'
))
...
...
Misc/NEWS
Dosyayı görüntüle @
50e90e26
...
@@ -270,6 +270,9 @@ Core and builtins
...
@@ -270,6 +270,9 @@ Core and builtins
Library
Library
-------
-------
-
itertools
.
count
()
is
no
longer
bounded
to
LONG_MAX
.
Formerly
,
it
raised
an
OverflowError
.
Now
,
automatically
shifts
from
ints
to
longs
.
-
Patch
#
1541463
:
optimize
performance
of
cgi
.
FieldStorage
operations
.
-
Patch
#
1541463
:
optimize
performance
of
cgi
.
FieldStorage
operations
.
-
Decimal
is
fully
updated
to
the
latest
Decimal
Specification
(
v1
.66
).
-
Decimal
is
fully
updated
to
the
latest
Decimal
Specification
(
v1
.66
).
...
...
Modules/itertoolsmodule.c
Dosyayı görüntüle @
50e90e26
...
@@ -2032,6 +2032,7 @@ static PyTypeObject ifilterfalse_type = {
...
@@ -2032,6 +2032,7 @@ static PyTypeObject ifilterfalse_type = {
typedef
struct
{
typedef
struct
{
PyObject_HEAD
PyObject_HEAD
Py_ssize_t
cnt
;
Py_ssize_t
cnt
;
PyObject
*
long_cnt
;
/* Arbitrarily large count when cnt >= PY_SSIZE_T_MAX */
}
countobject
;
}
countobject
;
static
PyTypeObject
count_type
;
static
PyTypeObject
count_type
;
...
@@ -2041,37 +2042,97 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
...
@@ -2041,37 +2042,97 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
{
countobject
*
lz
;
countobject
*
lz
;
Py_ssize_t
cnt
=
0
;
Py_ssize_t
cnt
=
0
;
PyObject
*
cnt_arg
=
NULL
;
PyObject
*
long_cnt
=
NULL
;
if
(
type
==
&
count_type
&&
!
_PyArg_NoKeywords
(
"count()"
,
kwds
))
if
(
type
==
&
count_type
&&
!
_PyArg_NoKeywords
(
"count()"
,
kwds
))
return
NULL
;
return
NULL
;
if
(
!
PyArg_
ParseTuple
(
args
,
"|n:count"
,
&
cnt
))
if
(
!
PyArg_
UnpackTuple
(
args
,
"count"
,
0
,
1
,
&
cnt_arg
))
return
NULL
;
return
NULL
;
if
(
cnt_arg
!=
NULL
)
{
cnt
=
PyInt_AsSsize_t
(
cnt_arg
);
if
(
cnt
==
-
1
&&
PyErr_Occurred
())
{
PyErr_Clear
();
if
(
!
PyLong_Check
(
cnt_arg
))
{
PyErr_SetString
(
PyExc_TypeError
,
"an integer is required"
);
return
NULL
;
}
long_cnt
=
cnt_arg
;
Py_INCREF
(
long_cnt
);
cnt
=
PY_SSIZE_T_MAX
;
}
}
/* create countobject structure */
/* create countobject structure */
lz
=
(
countobject
*
)
PyObject_New
(
countobject
,
&
count_type
);
lz
=
(
countobject
*
)
PyObject_New
(
countobject
,
&
count_type
);
if
(
lz
==
NULL
)
if
(
lz
==
NULL
)
{
Py_XDECREF
(
long_cnt
);
return
NULL
;
return
NULL
;
}
lz
->
cnt
=
cnt
;
lz
->
cnt
=
cnt
;
lz
->
long_cnt
=
long_cnt
;
return
(
PyObject
*
)
lz
;
return
(
PyObject
*
)
lz
;
}
}
static
void
count_dealloc
(
countobject
*
lz
)
{
Py_XDECREF
(
lz
->
long_cnt
);
PyObject_Del
(
lz
);
}
static
PyObject
*
count_nextlong
(
countobject
*
lz
)
{
static
PyObject
*
one
=
NULL
;
PyObject
*
cnt
;
PyObject
*
stepped_up
;
if
(
lz
->
long_cnt
==
NULL
)
{
lz
->
long_cnt
=
PyInt_FromSsize_t
(
PY_SSIZE_T_MAX
);
if
(
lz
->
long_cnt
==
NULL
)
return
NULL
;
}
if
(
one
==
NULL
)
{
one
=
PyInt_FromLong
(
1
);
if
(
one
==
NULL
)
return
NULL
;
}
cnt
=
lz
->
long_cnt
;
assert
(
cnt
!=
NULL
);
stepped_up
=
PyNumber_Add
(
cnt
,
one
);
if
(
stepped_up
==
NULL
)
return
NULL
;
lz
->
long_cnt
=
stepped_up
;
return
cnt
;
}
static
PyObject
*
static
PyObject
*
count_next
(
countobject
*
lz
)
count_next
(
countobject
*
lz
)
{
{
if
(
lz
->
cnt
==
PY_SSIZE_T_MAX
)
{
if
(
lz
->
cnt
==
PY_SSIZE_T_MAX
)
PyErr_SetString
(
PyExc_OverflowError
,
return
count_nextlong
(
lz
);
"cannot count beyond PY_SSIZE_T_MAX"
);
return
NULL
;
}
return
PyInt_FromSsize_t
(
lz
->
cnt
++
);
return
PyInt_FromSsize_t
(
lz
->
cnt
++
);
}
}
static
PyObject
*
static
PyObject
*
count_repr
(
countobject
*
lz
)
count_repr
(
countobject
*
lz
)
{
{
return
PyString_FromFormat
(
"count(%zd)"
,
lz
->
cnt
);
PyObject
*
cnt_repr
;
PyObject
*
result
;
if
(
lz
->
cnt
!=
PY_SSIZE_T_MAX
)
return
PyString_FromFormat
(
"count(%zd)"
,
lz
->
cnt
);
cnt_repr
=
PyObject_Repr
(
lz
->
long_cnt
);
if
(
cnt_repr
==
NULL
)
return
NULL
;
result
=
PyString_FromFormat
(
"count(%s)"
,
PyString_AS_STRING
(
cnt_repr
));
Py_DECREF
(
cnt_repr
);
return
result
;
}
}
PyDoc_STRVAR
(
count_doc
,
PyDoc_STRVAR
(
count_doc
,
...
@@ -2086,7 +2147,7 @@ static PyTypeObject count_type = {
...
@@ -2086,7 +2147,7 @@ static PyTypeObject count_type = {
sizeof
(
countobject
),
/* tp_basicsize */
sizeof
(
countobject
),
/* tp_basicsize */
0
,
/* tp_itemsize */
0
,
/* tp_itemsize */
/* methods */
/* methods */
(
destructor
)
PyObject_Del
,
/* tp_dealloc */
(
destructor
)
count_dealloc
,
/* tp_dealloc */
0
,
/* tp_print */
0
,
/* tp_print */
0
,
/* tp_getattr */
0
,
/* tp_getattr */
0
,
/* tp_setattr */
0
,
/* tp_setattr */
...
...
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