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
65baa341
Kaydet (Commit)
65baa341
authored
Şub 07, 2008
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 2025: Add index() and count() methods to tuple so that it will follow
the ABC for collections.Sequence.
üst
2f653c19
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
119 additions
and
0 deletions
+119
-0
seq_tests.py
Lib/test/seq_tests.py
+62
-0
NEWS
Misc/NEWS
+3
-0
tupleobject.c
Objects/tupleobject.c
+54
-0
No files found.
Lib/test/seq_tests.py
Dosyayı görüntüle @
65baa341
...
...
@@ -4,6 +4,7 @@ Tests common to tuple, list and UserList.UserList
import
unittest
from
test
import
test_support
import
sys
# Various iterables
# This is used for checking the constructor (here and in test_deque.py)
...
...
@@ -326,3 +327,64 @@ class CommonTest(unittest.TestCase):
self
.
assertEqual
(
a
.
__getitem__
(
slice
(
3
,
5
)),
self
.
type2test
([]))
self
.
assertRaises
(
ValueError
,
a
.
__getitem__
,
slice
(
0
,
10
,
0
))
self
.
assertRaises
(
TypeError
,
a
.
__getitem__
,
'x'
)
def
test_count
(
self
):
a
=
self
.
type2test
([
0
,
1
,
2
])
*
3
self
.
assertEqual
(
a
.
count
(
0
),
3
)
self
.
assertEqual
(
a
.
count
(
1
),
3
)
self
.
assertEqual
(
a
.
count
(
3
),
0
)
self
.
assertRaises
(
TypeError
,
a
.
count
)
class
BadExc
(
Exception
):
pass
class
BadCmp
:
def
__eq__
(
self
,
other
):
if
other
==
2
:
raise
BadExc
()
return
False
self
.
assertRaises
(
BadExc
,
a
.
count
,
BadCmp
())
def
test_index
(
self
):
u
=
self
.
type2test
([
0
,
1
])
self
.
assertEqual
(
u
.
index
(
0
),
0
)
self
.
assertEqual
(
u
.
index
(
1
),
1
)
self
.
assertRaises
(
ValueError
,
u
.
index
,
2
)
u
=
self
.
type2test
([
-
2
,
-
1
,
0
,
0
,
1
,
2
])
self
.
assertEqual
(
u
.
count
(
0
),
2
)
self
.
assertEqual
(
u
.
index
(
0
),
2
)
self
.
assertEqual
(
u
.
index
(
0
,
2
),
2
)
self
.
assertEqual
(
u
.
index
(
-
2
,
-
10
),
0
)
self
.
assertEqual
(
u
.
index
(
0
,
3
),
3
)
self
.
assertEqual
(
u
.
index
(
0
,
3
,
4
),
3
)
self
.
assertRaises
(
ValueError
,
u
.
index
,
2
,
0
,
-
10
)
self
.
assertRaises
(
TypeError
,
u
.
index
)
class
BadExc
(
Exception
):
pass
class
BadCmp
:
def
__eq__
(
self
,
other
):
if
other
==
2
:
raise
BadExc
()
return
False
a
=
self
.
type2test
([
0
,
1
,
2
,
3
])
self
.
assertRaises
(
BadExc
,
a
.
index
,
BadCmp
())
a
=
self
.
type2test
([
-
2
,
-
1
,
0
,
0
,
1
,
2
])
self
.
assertEqual
(
a
.
index
(
0
),
2
)
self
.
assertEqual
(
a
.
index
(
0
,
2
),
2
)
self
.
assertEqual
(
a
.
index
(
0
,
-
4
),
2
)
self
.
assertEqual
(
a
.
index
(
-
2
,
-
10
),
0
)
self
.
assertEqual
(
a
.
index
(
0
,
3
),
3
)
self
.
assertEqual
(
a
.
index
(
0
,
-
3
),
3
)
self
.
assertEqual
(
a
.
index
(
0
,
3
,
4
),
3
)
self
.
assertEqual
(
a
.
index
(
0
,
-
3
,
-
2
),
3
)
self
.
assertEqual
(
a
.
index
(
0
,
-
4
*
sys
.
maxsize
,
4
*
sys
.
maxsize
),
2
)
self
.
assertRaises
(
ValueError
,
a
.
index
,
0
,
4
*
sys
.
maxsize
,
-
4
*
sys
.
maxsize
)
self
.
assertRaises
(
ValueError
,
a
.
index
,
2
,
0
,
-
10
)
Misc/NEWS
Dosyayı görüntüle @
65baa341
...
...
@@ -12,6 +12,9 @@ What's New in Python 3.0a3?
Core and Builtins
-----------------
- Issue #2025 : Add tuple.count() and tuple.index() methods to comply with
the collections.Sequence API.
- Fixed multiple reinitialization of the Python interpreter. The small int
list in longobject.c has caused a seg fault during the third finalization.
...
...
Objects/tupleobject.c
Dosyayı görüntüle @
65baa341
...
...
@@ -430,6 +430,53 @@ tuplerepeat(PyTupleObject *a, Py_ssize_t n)
return
(
PyObject
*
)
np
;
}
static
PyObject
*
tupleindex
(
PyTupleObject
*
self
,
PyObject
*
args
)
{
Py_ssize_t
i
,
start
=
0
,
stop
=
Py_SIZE
(
self
);
PyObject
*
v
;
if
(
!
PyArg_ParseTuple
(
args
,
"O|O&O&:index"
,
&
v
,
_PyEval_SliceIndex
,
&
start
,
_PyEval_SliceIndex
,
&
stop
))
return
NULL
;
if
(
start
<
0
)
{
start
+=
Py_SIZE
(
self
);
if
(
start
<
0
)
start
=
0
;
}
if
(
stop
<
0
)
{
stop
+=
Py_SIZE
(
self
);
if
(
stop
<
0
)
stop
=
0
;
}
for
(
i
=
start
;
i
<
stop
&&
i
<
Py_SIZE
(
self
);
i
++
)
{
int
cmp
=
PyObject_RichCompareBool
(
self
->
ob_item
[
i
],
v
,
Py_EQ
);
if
(
cmp
>
0
)
return
PyLong_FromSsize_t
(
i
);
else
if
(
cmp
<
0
)
return
NULL
;
}
PyErr_SetString
(
PyExc_ValueError
,
"tuple.index(x): x not in list"
);
return
NULL
;
}
static
PyObject
*
tuplecount
(
PyTupleObject
*
self
,
PyObject
*
v
)
{
Py_ssize_t
count
=
0
;
Py_ssize_t
i
;
for
(
i
=
0
;
i
<
Py_SIZE
(
self
);
i
++
)
{
int
cmp
=
PyObject_RichCompareBool
(
self
->
ob_item
[
i
],
v
,
Py_EQ
);
if
(
cmp
>
0
)
count
++
;
else
if
(
cmp
<
0
)
return
NULL
;
}
return
PyLong_FromSsize_t
(
count
);
}
static
int
tupletraverse
(
PyTupleObject
*
o
,
visitproc
visit
,
void
*
arg
)
{
...
...
@@ -636,8 +683,15 @@ tuple_getnewargs(PyTupleObject *v)
}
PyDoc_STRVAR
(
index_doc
,
"T.index(value, [start, [stop]]) -> integer -- return first index of value"
);
PyDoc_STRVAR
(
count_doc
,
"T.count(value) -> integer -- return number of occurrences of value"
);
static
PyMethodDef
tuple_methods
[]
=
{
{
"__getnewargs__"
,
(
PyCFunction
)
tuple_getnewargs
,
METH_NOARGS
},
{
"index"
,
(
PyCFunction
)
tupleindex
,
METH_VARARGS
,
index_doc
},
{
"count"
,
(
PyCFunction
)
tuplecount
,
METH_O
,
count_doc
},
{
NULL
,
NULL
}
/* sentinel */
};
...
...
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