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
aa681c7b
Kaydet (Commit)
aa681c7b
authored
Şub 21, 2009
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fix keyword arguments for itertools.count().
Step arg without a start arg was ignored.
üst
2752e9b5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
28 deletions
+45
-28
test_itertools.py
Lib/test/test_itertools.py
+2
-0
itertoolsmodule.c
Modules/itertoolsmodule.c
+43
-28
No files found.
Lib/test/test_itertools.py
Dosyayı görüntüle @
aa681c7b
...
@@ -350,6 +350,8 @@ class TestBasicOps(unittest.TestCase):
...
@@ -350,6 +350,8 @@ class TestBasicOps(unittest.TestCase):
self
.
assertEqual
(
zip
(
'abc'
,
count
(
2
,
3
)),
[(
'a'
,
2
),
(
'b'
,
5
),
(
'c'
,
8
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
(
2
,
3
)),
[(
'a'
,
2
),
(
'b'
,
5
),
(
'c'
,
8
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
(
start
=
2
,
step
=
3
)),
self
.
assertEqual
(
zip
(
'abc'
,
count
(
start
=
2
,
step
=
3
)),
[(
'a'
,
2
),
(
'b'
,
5
),
(
'c'
,
8
)])
[(
'a'
,
2
),
(
'b'
,
5
),
(
'c'
,
8
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
(
step
=-
1
)),
[(
'a'
,
0
),
(
'b'
,
-
1
),
(
'c'
,
-
2
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
(
2
,
0
)),
[(
'a'
,
2
),
(
'b'
,
2
),
(
'c'
,
2
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
(
2
,
0
)),
[(
'a'
,
2
),
(
'b'
,
2
),
(
'c'
,
2
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
(
2
,
1
)),
[(
'a'
,
2
),
(
'b'
,
3
),
(
'c'
,
4
)])
self
.
assertEqual
(
zip
(
'abc'
,
count
(
2
,
1
)),
[(
'a'
,
2
),
(
'b'
,
3
),
(
'c'
,
4
)])
self
.
assertEqual
(
take
(
20
,
count
(
maxsize
-
15
,
3
)),
take
(
20
,
range
(
maxsize
-
15
,
maxsize
+
100
,
3
)))
self
.
assertEqual
(
take
(
20
,
count
(
maxsize
-
15
,
3
)),
take
(
20
,
range
(
maxsize
-
15
,
maxsize
+
100
,
3
)))
...
...
Modules/itertoolsmodule.c
Dosyayı görüntüle @
aa681c7b
...
@@ -3209,19 +3209,19 @@ typedef struct {
...
@@ -3209,19 +3209,19 @@ typedef struct {
/* Counting logic and invariants:
/* Counting logic and invariants:
C_add
_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified.
fast
_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified.
assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyInt(1));
assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyInt(1));
Advances with: cnt += 1
Advances with: cnt += 1
When count hits Y_SSIZE_T_MAX, switch to
Py_add
_mode.
When count hits Y_SSIZE_T_MAX, switch to
slow
_mode.
Py_add
_mode: when cnt == PY_SSIZE_T_MAX, step is not int(1), or cnt is a float.
slow
_mode: when cnt == PY_SSIZE_T_MAX, step is not int(1), or cnt is a float.
assert(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && long_step != NULL);
assert(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && long_step != NULL);
All counting is done with python objects (no overflows or underflows).
All counting is done with python objects (no overflows or underflows).
Advances with: long_cnt += long_step
Advances with: long_cnt += long_step
Step may be zero -- effectively a slow version of repeat(cnt).
Step may be zero -- effectively a slow version of repeat(cnt).
Either long_cnt or long_step may be a float.
Either long_cnt or long_step may be a float
, Fraction, or Decimal
.
*/
*/
static
PyTypeObject
count_type
;
static
PyTypeObject
count_type
;
...
@@ -3230,6 +3230,7 @@ static PyObject *
...
@@ -3230,6 +3230,7 @@ static PyObject *
count_new
(
PyTypeObject
*
type
,
PyObject
*
args
,
PyObject
*
kwds
)
count_new
(
PyTypeObject
*
type
,
PyObject
*
args
,
PyObject
*
kwds
)
{
{
countobject
*
lz
;
countobject
*
lz
;
int
slow_mode
=
0
;
Py_ssize_t
cnt
=
0
;
Py_ssize_t
cnt
=
0
;
PyObject
*
long_cnt
=
NULL
;
PyObject
*
long_cnt
=
NULL
;
PyObject
*
long_step
=
NULL
;
PyObject
*
long_step
=
NULL
;
...
@@ -3239,36 +3240,51 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
...
@@ -3239,36 +3240,51 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
kwlist
,
&
long_cnt
,
&
long_step
))
kwlist
,
&
long_cnt
,
&
long_step
))
return
NULL
;
return
NULL
;
if
(
(
long_cnt
!=
NULL
&&
!
PyNumber_Check
(
long_cnt
)
)
||
if
(
long_cnt
!=
NULL
&&
!
PyNumber_Check
(
long_cnt
)
||
(
long_step
!=
NULL
&&
!
PyNumber_Check
(
long_step
)
))
{
long_step
!=
NULL
&&
!
PyNumber_Check
(
long_step
))
{
PyErr_SetString
(
PyExc_TypeError
,
"a number is required"
);
PyErr_SetString
(
PyExc_TypeError
,
"a number is required"
);
return
NULL
;
return
NULL
;
}
}
if
(
long_cnt
!=
NULL
)
{
cnt
=
PyInt_AsSsize_t
(
long_cnt
);
if
(
cnt
==
-
1
&&
PyErr_Occurred
()
||
!
PyInt_Check
(
long_cnt
))
{
PyErr_Clear
();
slow_mode
=
1
;
}
Py_INCREF
(
long_cnt
);
}
else
{
cnt
=
0
;
long_cnt
=
PyInt_FromLong
(
0
);
}
/* If not specified, step defaults to 1 */
if
(
long_step
==
NULL
)
{
if
(
long_step
==
NULL
)
{
/* If not specified, step defaults to 1 */
long_step
=
PyInt_FromLong
(
1
);
long_step
=
PyInt_FromLong
(
1
);
if
(
long_step
==
NULL
)
if
(
long_step
==
NULL
)
{
Py_DECREF
(
long_cnt
);
return
NULL
;
return
NULL
;
}
}
else
}
else
Py_INCREF
(
long_step
);
Py_INCREF
(
long_step
);
assert
(
long_step
!=
NULL
);
if
(
long_cnt
!=
NULL
)
{
assert
(
long_cnt
!=
NULL
&&
long_step
!=
NULL
);
cnt
=
PyInt_AsSsize_t
(
long_cnt
);
if
((
cnt
==
-
1
&&
PyErr_Occurred
())
||
/* Fast mode only works when the step is 1 */
!
PyIndex_Check
(
long_cnt
)
||
if
(
!
PyInt_Check
(
long_step
)
||
!
PyInt_Check
(
long_step
)
||
PyInt_AS_LONG
(
long_step
)
!=
1
)
{
PyInt_AS_LONG
(
long_step
)
!=
1
)
{
slow_mode
=
1
;
/* Switch to Py_add_mode */
PyErr_Clear
();
Py_INCREF
(
long_cnt
);
cnt
=
PY_SSIZE_T_MAX
;
}
else
long_cnt
=
NULL
;
}
}
assert
((
cnt
!=
PY_SSIZE_T_MAX
&&
long_cnt
==
NULL
)
||
(
cnt
==
PY_SSIZE_T_MAX
&&
long_cnt
!=
NULL
));
if
(
slow_mode
)
cnt
=
PY_SSIZE_T_MAX
;
else
Py_CLEAR
(
long_cnt
);
assert
(
cnt
!=
PY_SSIZE_T_MAX
&&
long_cnt
==
NULL
&&
!
slow_mode
||
cnt
==
PY_SSIZE_T_MAX
&&
long_cnt
!=
NULL
&&
slow_mode
);
assert
(
slow_mode
||
PyInt_Check
(
long_step
)
&&
PyInt_AS_LONG
(
long_step
)
==
1
);
/* create countobject structure */
/* create countobject structure */
lz
=
(
countobject
*
)
type
->
tp_alloc
(
type
,
0
);
lz
=
(
countobject
*
)
type
->
tp_alloc
(
type
,
0
);
...
@@ -3308,7 +3324,7 @@ count_nextlong(countobject *lz)
...
@@ -3308,7 +3324,7 @@ count_nextlong(countobject *lz)
long_cnt
=
lz
->
long_cnt
;
long_cnt
=
lz
->
long_cnt
;
if
(
long_cnt
==
NULL
)
{
if
(
long_cnt
==
NULL
)
{
/* Switch to
Py_add
_mode */
/* Switch to
slow
_mode */
long_cnt
=
PyInt_FromSsize_t
(
PY_SSIZE_T_MAX
);
long_cnt
=
PyInt_FromSsize_t
(
PY_SSIZE_T_MAX
);
if
(
long_cnt
==
NULL
)
if
(
long_cnt
==
NULL
)
return
NULL
;
return
NULL
;
...
@@ -3360,11 +3376,10 @@ count_repr(countobject *lz)
...
@@ -3360,11 +3376,10 @@ count_repr(countobject *lz)
}
}
PyDoc_STRVAR
(
count_doc
,
PyDoc_STRVAR
(
count_doc
,
"count(
[start[, step]
]) --> count object
\n
\
"count(
start=0, step=1
]) --> count object
\n
\
\n
\
\n
\
Return a count object whose .next() method returns consecutive
\n
\
Return a count object whose .next() method returns consecutive values.
\n
\
integers starting from zero or, if specified, from start.
\n
\
Equivalent to:
\n\n
\
If step is specified, counts by that interval. Equivalent to:
\n\n
\
def count(firstval=0, step=1):
\n
\
def count(firstval=0, step=1):
\n
\
x = firstval
\n
\
x = firstval
\n
\
while 1:
\n
\
while 1:
\n
\
...
...
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