Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
91383576
Commit
91383576
authored
May 13, 2008
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
parent
ef9764f1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
11 deletions
+45
-11
Doc/library/functions.rst
Doc/library/functions.rst
+7
-5
Lib/test/test_enumerate.py
Lib/test/test_enumerate.py
+15
-1
Objects/enumobject.c
Objects/enumobject.c
+23
-5
No files found.
Doc/library/functions.rst
View file @
91383576
...
@@ -335,15 +335,15 @@ available. They are listed here in alphabetical order.
...
@@ -335,15 +335,15 @@ available. They are listed here in alphabetical order.
Using
:
func
:`
divmod
`
with
complex
numbers
is
deprecated
.
Using
:
func
:`
divmod
`
with
complex
numbers
is
deprecated
.
..
function
::
enumerate
(
sequence
)
..
function
::
enumerate
(
sequence
[,
start
=
0
]
)
Return
an
enumerate
object
.
*
sequence
*
must
be
a
sequence
,
an
Return
an
enumerate
object
.
*
sequence
*
must
be
a
sequence
,
an
:
term
:`
iterator
`,
or
some
other
object
which
supports
iteration
.
The
:
term
:`
iterator
`,
or
some
other
object
which
supports
iteration
.
The
:
meth
:`
next
`
method
of
the
iterator
returned
by
:
func
:`
enumerate
`
returns
a
:
meth
:`
next
`
method
of
the
iterator
returned
by
:
func
:`
enumerate
`
returns
a
tuple
containing
a
count
(
from
zero
)
and
the
corresponding
value
obtained
tuple
containing
a
count
(
from
*
start
*
which
defaults
to
0
)
and
the
from
iterating
over
*
iterable
*.
:
func
:`
enumerate
`
is
useful
for
obtaining
an
corresponding
value
obtained
from
iterating
over
*
iterable
*.
indexed
series
:
``(
0
,
seq
[
0
])``,
``(
1
,
seq
[
1
])``,
``(
2
,
seq
[
2
])``,
....
For
:
func
:`
enumerate
`
is
useful
for
obtaining
an
indexed
series
:
``(
0
,
seq
[
0
])``,
example
:
``(
1
,
seq
[
1
])``,
``(
2
,
seq
[
2
])``,
....
For
example
:
>>>
for
i
,
season
in
enumerate
([
'Spring'
,
'Summer'
,
'Fall'
,
'Winter'
]):
>>>
for
i
,
season
in
enumerate
([
'Spring'
,
'Summer'
,
'Fall'
,
'Winter'
]):
...
print
i
,
season
...
print
i
,
season
...
@@ -353,6 +353,8 @@ available. They are listed here in alphabetical order.
...
@@ -353,6 +353,8 @@ available. They are listed here in alphabetical order.
3
Winter
3
Winter
..
versionadded
::
2.3
..
versionadded
::
2.3
..
versionadded
::
2.6
The
*
start
*
parameter
.
..
function
::
eval
(
expression
[,
globals
[,
locals
]])
..
function
::
eval
(
expression
[,
globals
[,
locals
]])
...
...
Lib/test/test_enumerate.py
View file @
91383576
...
@@ -100,7 +100,8 @@ class EnumerateTestCase(unittest.TestCase):
...
@@ -100,7 +100,8 @@ class EnumerateTestCase(unittest.TestCase):
def
test_argumentcheck
(
self
):
def
test_argumentcheck
(
self
):
self
.
assertRaises
(
TypeError
,
self
.
enum
)
# no arguments
self
.
assertRaises
(
TypeError
,
self
.
enum
)
# no arguments
self
.
assertRaises
(
TypeError
,
self
.
enum
,
1
)
# wrong type (not iterable)
self
.
assertRaises
(
TypeError
,
self
.
enum
,
1
)
# wrong type (not iterable)
self
.
assertRaises
(
TypeError
,
self
.
enum
,
'abc'
,
2
)
# too many arguments
self
.
assertRaises
(
TypeError
,
self
.
enum
,
'abc'
,
'a'
)
# wrong type
self
.
assertRaises
(
TypeError
,
self
.
enum
,
'abc'
,
2
,
3
)
# too many arguments
def
test_tuple_reuse
(
self
):
def
test_tuple_reuse
(
self
):
# Tests an implementation detail where tuple is reused
# Tests an implementation detail where tuple is reused
...
@@ -196,6 +197,19 @@ class TestReversed(unittest.TestCase):
...
@@ -196,6 +197,19 @@ class TestReversed(unittest.TestCase):
self
.
assertEqual
(
rc
,
sys
.
getrefcount
(
r
))
self
.
assertEqual
(
rc
,
sys
.
getrefcount
(
r
))
class
TestStart
(
EnumerateTestCase
):
enum
=
lambda
i
:
enumerate
(
i
,
start
=
11
)
seq
,
res
=
'abc'
,
[(
1
,
'a'
),
(
2
,
'b'
),
(
3
,
'c'
)]
class
TestLongStart
(
EnumerateTestCase
):
enum
=
lambda
i
:
enumerate
(
i
,
start
=
sys
.
maxint
+
1
)
seq
,
res
=
'abc'
,
[(
sys
.
maxint
+
1
,
'a'
),
(
sys
.
maxint
+
2
,
'b'
),
(
sys
.
maxint
+
3
,
'c'
)]
def
test_main
(
verbose
=
None
):
def
test_main
(
verbose
=
None
):
testclasses
=
(
EnumerateTestCase
,
SubclassTestCase
,
TestEmpty
,
TestBig
,
testclasses
=
(
EnumerateTestCase
,
SubclassTestCase
,
TestEmpty
,
TestBig
,
TestReversed
)
TestReversed
)
...
...
Objects/enumobject.c
View file @
91383576
...
@@ -15,18 +15,36 @@ enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
...
@@ -15,18 +15,36 @@ enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
{
enumobject
*
en
;
enumobject
*
en
;
PyObject
*
seq
=
NULL
;
PyObject
*
seq
=
NULL
;
static
char
*
kwlist
[]
=
{
"sequence"
,
0
};
PyObject
*
start
=
NULL
;
static
char
*
kwlist
[]
=
{
"sequence"
,
"start"
,
0
};
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"O:enumerate"
,
kwlist
,
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"O
|O
:enumerate"
,
kwlist
,
&
seq
))
&
seq
,
&
start
))
return
NULL
;
return
NULL
;
en
=
(
enumobject
*
)
type
->
tp_alloc
(
type
,
0
);
en
=
(
enumobject
*
)
type
->
tp_alloc
(
type
,
0
);
if
(
en
==
NULL
)
if
(
en
==
NULL
)
return
NULL
;
return
NULL
;
en
->
en_index
=
0
;
if
(
start
)
{
start
=
PyNumber_Index
(
start
);
if
(
start
==
NULL
)
{
Py_DECREF
(
en
);
return
NULL
;
}
if
(
PyLong_Check
(
start
))
{
en
->
en_index
=
LONG_MAX
;
en
->
en_longindex
=
start
;
}
else
{
assert
(
PyInt_Check
(
start
));
en
->
en_index
=
PyInt_AsLong
(
start
);
en
->
en_longindex
=
NULL
;
Py_DECREF
(
start
);
}
}
else
{
en
->
en_index
=
0
;
en
->
en_longindex
=
NULL
;
}
en
->
en_sit
=
PyObject_GetIter
(
seq
);
en
->
en_sit
=
PyObject_GetIter
(
seq
);
en
->
en_longindex
=
NULL
;
if
(
en
->
en_sit
==
NULL
)
{
if
(
en
->
en_sit
==
NULL
)
{
Py_DECREF
(
en
);
Py_DECREF
(
en
);
return
NULL
;
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