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
314861c5
Commit
314861c5
authored
Mar 30, 2006
by
Armin Rigo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor bugs in the __index__ code (PEP 357), with tests.
parent
4ef3a23a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
97 additions
and
129 deletions
+97
-129
Lib/test/test_index.py
Lib/test/test_index.py
+73
-95
Objects/abstract.c
Objects/abstract.c
+3
-2
Objects/typeobject.c
Objects/typeobject.c
+21
-32
No files found.
Lib/test/test_index.py
View file @
314861c5
...
@@ -10,9 +10,8 @@ class newstyle(object):
...
@@ -10,9 +10,8 @@ class newstyle(object):
def
__index__
(
self
):
def
__index__
(
self
):
return
self
.
ind
return
self
.
ind
class
List
TestCase
(
unittest
.
TestCase
):
class
Base
TestCase
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
seq
=
[
0
,
10
,
20
,
30
,
40
,
50
]
self
.
o
=
oldstyle
()
self
.
o
=
oldstyle
()
self
.
n
=
newstyle
()
self
.
n
=
newstyle
()
self
.
o2
=
oldstyle
()
self
.
o2
=
oldstyle
()
...
@@ -21,8 +20,8 @@ class ListTestCase(unittest.TestCase):
...
@@ -21,8 +20,8 @@ class ListTestCase(unittest.TestCase):
def
test_basic
(
self
):
def
test_basic
(
self
):
self
.
o
.
ind
=
-
2
self
.
o
.
ind
=
-
2
self
.
n
.
ind
=
2
self
.
n
.
ind
=
2
assert
(
self
.
seq
[
self
.
n
]
==
20
)
assert
(
self
.
seq
[
self
.
n
]
==
self
.
seq
[
2
]
)
assert
(
self
.
seq
[
self
.
o
]
==
40
)
assert
(
self
.
seq
[
self
.
o
]
==
self
.
seq
[
-
2
]
)
assert
(
operator
.
index
(
self
.
o
)
==
-
2
)
assert
(
operator
.
index
(
self
.
o
)
==
-
2
)
assert
(
operator
.
index
(
self
.
n
)
==
2
)
assert
(
operator
.
index
(
self
.
n
)
==
2
)
...
@@ -43,108 +42,86 @@ class ListTestCase(unittest.TestCase):
...
@@ -43,108 +42,86 @@ class ListTestCase(unittest.TestCase):
assert
(
self
.
seq
[
self
.
o
:
self
.
o2
]
==
self
.
seq
[
1
:
3
])
assert
(
self
.
seq
[
self
.
o
:
self
.
o2
]
==
self
.
seq
[
1
:
3
])
assert
(
self
.
seq
[
self
.
n
:
self
.
n2
]
==
self
.
seq
[
2
:
4
])
assert
(
self
.
seq
[
self
.
n
:
self
.
n2
]
==
self
.
seq
[
2
:
4
])
class
TupleTestCase
(
unittest
.
TestCase
):
def
test_repeat
(
self
):
def
setUp
(
self
):
self
.
o
.
ind
=
3
self
.
seq
=
(
0
,
10
,
20
,
30
,
40
,
50
)
self
.
n
.
ind
=
2
self
.
o
=
oldstyle
()
assert
(
self
.
seq
*
self
.
o
==
self
.
seq
*
3
)
self
.
n
=
newstyle
()
assert
(
self
.
seq
*
self
.
n
==
self
.
seq
*
2
)
self
.
o2
=
oldstyle
()
assert
(
self
.
o
*
self
.
seq
==
self
.
seq
*
3
)
self
.
n2
=
newstyle
()
assert
(
self
.
n
*
self
.
seq
==
self
.
seq
*
2
)
def
test_wrappers
(
self
):
def
test_basic
(
self
):
n
=
self
.
n
n
.
ind
=
5
assert
n
.
__index__
()
==
5
assert
6
.
__index__
()
==
6
assert
-
7L
.
__index__
()
==
-
7
assert
self
.
seq
.
__getitem__
(
n
)
==
self
.
seq
[
5
]
assert
self
.
seq
.
__mul__
(
n
)
==
self
.
seq
*
5
assert
self
.
seq
.
__rmul__
(
n
)
==
self
.
seq
*
5
def
test_infinite_recusion
(
self
):
class
Trap1
(
int
):
def
__index__
(
self
):
return
self
class
Trap2
(
long
):
def
__index__
(
self
):
return
self
self
.
failUnlessRaises
(
TypeError
,
operator
.
getitem
,
self
.
seq
,
Trap1
())
self
.
failUnlessRaises
(
TypeError
,
operator
.
getitem
,
self
.
seq
,
Trap2
())
class
ListTestCase
(
BaseTestCase
):
seq
=
[
0
,
10
,
20
,
30
,
40
,
50
]
def
test_setdelitem
(
self
):
self
.
o
.
ind
=
-
2
self
.
o
.
ind
=
-
2
self
.
n
.
ind
=
2
self
.
n
.
ind
=
2
assert
(
self
.
seq
[
self
.
n
]
==
20
)
lst
=
list
(
'ab!cdefghi!j'
)
assert
(
self
.
seq
[
self
.
o
]
==
40
)
del
lst
[
self
.
o
]
assert
(
operator
.
index
(
self
.
o
)
==
-
2
)
del
lst
[
self
.
n
]
assert
(
operator
.
index
(
self
.
n
)
==
2
)
lst
[
self
.
o
]
=
'X'
lst
[
self
.
n
]
=
'Y'
assert
lst
==
list
(
'abYdefghXj'
)
def
test_error
(
self
):
lst
=
[
5
,
6
,
7
,
8
,
9
,
10
,
11
]
self
.
o
.
ind
=
'dumb'
lst
.
__setitem__
(
self
.
n
,
"here"
)
self
.
n
.
ind
=
'bad'
assert
lst
==
[
5
,
6
,
"here"
,
8
,
9
,
10
,
11
]
myfunc
=
lambda
x
,
obj
:
obj
.
seq
[
x
]
lst
.
__delitem__
(
self
.
n
)
self
.
failUnlessRaises
(
TypeError
,
operator
.
index
,
self
.
o
)
assert
lst
==
[
5
,
6
,
8
,
9
,
10
,
11
]
self
.
failUnlessRaises
(
TypeError
,
operator
.
index
,
self
.
n
)
self
.
failUnlessRaises
(
TypeError
,
myfunc
,
self
.
o
,
self
)
self
.
failUnlessRaises
(
TypeError
,
myfunc
,
self
.
n
,
self
)
def
test_slice
(
self
):
def
test_inplace_repeat
(
self
):
self
.
o
.
ind
=
1
self
.
o
.
ind
=
2
self
.
o2
.
ind
=
3
self
.
n
.
ind
=
3
self
.
n
.
ind
=
2
lst
=
[
6
,
4
]
self
.
n2
.
ind
=
4
lst
*=
self
.
o
assert
(
self
.
seq
[
self
.
o
:
self
.
o2
]
==
self
.
seq
[
1
:
3
])
assert
lst
==
[
6
,
4
,
6
,
4
]
assert
(
self
.
seq
[
self
.
n
:
self
.
n2
]
==
self
.
seq
[
2
:
4
])
lst
*=
self
.
n
assert
lst
==
[
6
,
4
,
6
,
4
]
*
3
class
StringTestCase
(
unittest
.
TestCase
):
lst
=
[
5
,
6
,
7
,
8
,
9
,
11
]
def
setUp
(
self
):
l2
=
lst
.
__imul__
(
self
.
n
)
self
.
seq
=
"this is a test"
assert
l2
is
lst
self
.
o
=
oldstyle
()
assert
lst
==
[
5
,
6
,
7
,
8
,
9
,
11
]
*
3
self
.
n
=
newstyle
()
self
.
o2
=
oldstyle
()
self
.
n2
=
newstyle
()
def
test_basic
(
self
):
class
TupleTestCase
(
BaseTestCase
):
self
.
o
.
ind
=
-
2
seq
=
(
0
,
10
,
20
,
30
,
40
,
50
)
self
.
n
.
ind
=
2
assert
(
self
.
seq
[
self
.
n
]
==
self
.
seq
[
2
])
assert
(
self
.
seq
[
self
.
o
]
==
self
.
seq
[
-
2
])
assert
(
operator
.
index
(
self
.
o
)
==
-
2
)
assert
(
operator
.
index
(
self
.
n
)
==
2
)
def
test_error
(
self
):
class
StringTestCase
(
BaseTestCase
):
self
.
o
.
ind
=
'dumb'
seq
=
"this is a test"
self
.
n
.
ind
=
'bad'
myfunc
=
lambda
x
,
obj
:
obj
.
seq
[
x
]
self
.
failUnlessRaises
(
TypeError
,
operator
.
index
,
self
.
o
)
self
.
failUnlessRaises
(
TypeError
,
operator
.
index
,
self
.
n
)
self
.
failUnlessRaises
(
TypeError
,
myfunc
,
self
.
o
,
self
)
self
.
failUnlessRaises
(
TypeError
,
myfunc
,
self
.
n
,
self
)
def
test_slice
(
self
):
class
UnicodeTestCase
(
BaseTestCase
):
self
.
o
.
ind
=
1
seq
=
u"this is a test"
self
.
o2
.
ind
=
3
self
.
n
.
ind
=
2
self
.
n2
.
ind
=
4
assert
(
self
.
seq
[
self
.
o
:
self
.
o2
]
==
self
.
seq
[
1
:
3
])
assert
(
self
.
seq
[
self
.
n
:
self
.
n2
]
==
self
.
seq
[
2
:
4
])
class
UnicodeTestCase
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
seq
=
u"this is a test"
self
.
o
=
oldstyle
()
self
.
n
=
newstyle
()
self
.
o2
=
oldstyle
()
self
.
n2
=
newstyle
()
def
test_basic
(
self
):
class
XRangeTestCase
(
unittest
.
TestCase
):
self
.
o
.
ind
=
-
2
self
.
n
.
ind
=
2
assert
(
self
.
seq
[
self
.
n
]
==
self
.
seq
[
2
])
assert
(
self
.
seq
[
self
.
o
]
==
self
.
seq
[
-
2
])
assert
(
operator
.
index
(
self
.
o
)
==
-
2
)
assert
(
operator
.
index
(
self
.
n
)
==
2
)
def
test_error
(
self
):
self
.
o
.
ind
=
'dumb'
self
.
n
.
ind
=
'bad'
myfunc
=
lambda
x
,
obj
:
obj
.
seq
[
x
]
self
.
failUnlessRaises
(
TypeError
,
operator
.
index
,
self
.
o
)
self
.
failUnlessRaises
(
TypeError
,
operator
.
index
,
self
.
n
)
self
.
failUnlessRaises
(
TypeError
,
myfunc
,
self
.
o
,
self
)
self
.
failUnlessRaises
(
TypeError
,
myfunc
,
self
.
n
,
self
)
def
test_slice
(
self
):
def
test_xrange
(
self
):
self
.
o
.
ind
=
1
n
=
newstyle
()
self
.
o2
.
ind
=
3
n
.
ind
=
5
self
.
n
.
ind
=
2
assert
xrange
(
1
,
20
)[
n
]
==
6
self
.
n2
.
ind
=
4
assert
xrange
(
1
,
20
).
__getitem__
(
n
)
==
6
assert
(
self
.
seq
[
self
.
o
:
self
.
o2
]
==
self
.
seq
[
1
:
3
])
assert
(
self
.
seq
[
self
.
n
:
self
.
n2
]
==
self
.
seq
[
2
:
4
])
def
test_main
():
def
test_main
():
...
@@ -152,7 +129,8 @@ def test_main():
...
@@ -152,7 +129,8 @@ def test_main():
ListTestCase
,
ListTestCase
,
TupleTestCase
,
TupleTestCase
,
StringTestCase
,
StringTestCase
,
UnicodeTestCase
UnicodeTestCase
,
XRangeTestCase
,
)
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
Objects/abstract.c
View file @
314861c5
...
@@ -942,8 +942,9 @@ PyNumber_Index(PyObject *item)
...
@@ -942,8 +942,9 @@ PyNumber_Index(PyObject *item)
value
=
nb
->
nb_index
(
item
);
value
=
nb
->
nb_index
(
item
);
}
}
else
{
else
{
PyErr_SetString
(
PyExc_IndexError
,
PyErr_Format
(
PyExc_TypeError
,
"object cannot be interpreted as an index"
);
"'%.200s' object cannot be interpreted "
"as an index"
,
item
->
ob_type
->
tp_name
);
}
}
return
value
;
return
value
;
}
}
...
...
Objects/typeobject.c
View file @
314861c5
...
@@ -3542,12 +3542,16 @@ wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
...
@@ -3542,12 +3542,16 @@ wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
}
}
static
PyObject
*
static
PyObject
*
wrap_
ssize
argfunc
(
PyObject
*
self
,
PyObject
*
args
,
void
*
wrapped
)
wrap_
index
argfunc
(
PyObject
*
self
,
PyObject
*
args
,
void
*
wrapped
)
{
{
ssizeargfunc
func
=
(
ssizeargfunc
)
wrapped
;
ssizeargfunc
func
=
(
ssizeargfunc
)
wrapped
;
PyObject
*
o
;
Py_ssize_t
i
;
Py_ssize_t
i
;
if
(
!
PyArg_ParseTuple
(
args
,
"n"
,
&
i
))
if
(
!
PyArg_UnpackTuple
(
args
,
""
,
1
,
1
,
&
o
))
return
NULL
;
i
=
PyNumber_Index
(
o
);
if
(
i
==
-
1
&&
PyErr_Occurred
())
return
NULL
;
return
NULL
;
return
(
*
func
)(
self
,
i
);
return
(
*
func
)(
self
,
i
);
}
}
...
@@ -3557,7 +3561,7 @@ getindex(PyObject *self, PyObject *arg)
...
@@ -3557,7 +3561,7 @@ getindex(PyObject *self, PyObject *arg)
{
{
Py_ssize_t
i
;
Py_ssize_t
i
;
i
=
Py
Int_AsSsize_t
(
arg
);
i
=
Py
Number_Index
(
arg
);
if
(
i
==
-
1
&&
PyErr_Occurred
())
if
(
i
==
-
1
&&
PyErr_Occurred
())
return
-
1
;
return
-
1
;
if
(
i
<
0
)
{
if
(
i
<
0
)
{
...
@@ -4366,36 +4370,21 @@ slot_nb_nonzero(PyObject *self)
...
@@ -4366,36 +4370,21 @@ slot_nb_nonzero(PyObject *self)
static
Py_ssize_t
static
Py_ssize_t
slot_nb_index
(
PyObject
*
self
)
slot_nb_index
(
PyObject
*
self
)
{
{
PyObject
*
func
,
*
args
;
static
PyObject
*
index_str
;
static
PyObject
*
index_str
;
Py_ssize_t
result
=
-
1
;
PyObject
*
temp
=
call_method
(
self
,
"__index__"
,
&
index_str
,
"()"
);
Py_ssize_t
result
;
func
=
lookup_maybe
(
self
,
"__index__"
,
&
index_str
);
if
(
temp
==
NULL
)
if
(
func
==
NULL
)
{
if
(
!
PyErr_Occurred
())
{
PyErr_SetString
(
PyExc_TypeError
,
"object cannot be interpreted as an index"
);
}
return
-
1
;
return
-
1
;
}
if
(
PyInt_CheckExact
(
temp
)
||
PyLong_CheckExact
(
temp
))
{
args
=
PyTuple_New
(
0
);
result
=
temp
->
ob_type
->
tp_as_number
->
nb_index
(
temp
);
if
(
args
!=
NULL
)
{
PyObject
*
temp
=
PyObject_Call
(
func
,
args
,
NULL
);
Py_DECREF
(
args
);
if
(
temp
!=
NULL
)
{
if
(
PyInt_Check
(
temp
)
||
PyLong_Check
(
temp
))
{
result
=
temp
->
ob_type
->
tp_as_number
->
nb_index
(
temp
);
}
else
{
PyErr_SetString
(
PyExc_TypeError
,
"__index__ must return an int or a long"
);
result
=
-
1
;
}
Py_DECREF
(
temp
);
}
}
}
Py_DECREF
(
func
);
else
{
PyErr_SetString
(
PyExc_TypeError
,
"__index__ must return an int or a long"
);
result
=
-
1
;
}
Py_DECREF
(
temp
);
return
result
;
return
result
;
}
}
...
@@ -5026,9 +5015,9 @@ static slotdef slotdefs[] = {
...
@@ -5026,9 +5015,9 @@ static slotdef slotdefs[] = {
test_descr.notimplemented() */
test_descr.notimplemented() */
SQSLOT
(
"__add__"
,
sq_concat
,
NULL
,
wrap_binaryfunc
,
SQSLOT
(
"__add__"
,
sq_concat
,
NULL
,
wrap_binaryfunc
,
"x.__add__(y) <==> x+y"
),
"x.__add__(y) <==> x+y"
),
SQSLOT
(
"__mul__"
,
sq_repeat
,
NULL
,
wrap_
ssize
argfunc
,
SQSLOT
(
"__mul__"
,
sq_repeat
,
NULL
,
wrap_
index
argfunc
,
"x.__mul__(n) <==> x*n"
),
"x.__mul__(n) <==> x*n"
),
SQSLOT
(
"__rmul__"
,
sq_repeat
,
NULL
,
wrap_
ssize
argfunc
,
SQSLOT
(
"__rmul__"
,
sq_repeat
,
NULL
,
wrap_
index
argfunc
,
"x.__rmul__(n) <==> n*x"
),
"x.__rmul__(n) <==> n*x"
),
SQSLOT
(
"__getitem__"
,
sq_item
,
slot_sq_item
,
wrap_sq_item
,
SQSLOT
(
"__getitem__"
,
sq_item
,
slot_sq_item
,
wrap_sq_item
,
"x.__getitem__(y) <==> x[y]"
),
"x.__getitem__(y) <==> x[y]"
),
...
@@ -5054,7 +5043,7 @@ static slotdef slotdefs[] = {
...
@@ -5054,7 +5043,7 @@ static slotdef slotdefs[] = {
SQSLOT
(
"__iadd__"
,
sq_inplace_concat
,
NULL
,
SQSLOT
(
"__iadd__"
,
sq_inplace_concat
,
NULL
,
wrap_binaryfunc
,
"x.__iadd__(y) <==> x+=y"
),
wrap_binaryfunc
,
"x.__iadd__(y) <==> x+=y"
),
SQSLOT
(
"__imul__"
,
sq_inplace_repeat
,
NULL
,
SQSLOT
(
"__imul__"
,
sq_inplace_repeat
,
NULL
,
wrap_
ssize
argfunc
,
"x.__imul__(y) <==> x*=y"
),
wrap_
index
argfunc
,
"x.__imul__(y) <==> x*=y"
),
MPSLOT
(
"__len__"
,
mp_length
,
slot_mp_length
,
wrap_lenfunc
,
MPSLOT
(
"__len__"
,
mp_length
,
slot_mp_length
,
wrap_lenfunc
,
"x.__len__() <==> len(x)"
),
"x.__len__() <==> len(x)"
),
...
...
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