Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
3cc3601c
Commit
3cc3601c
authored
Mar 16, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clean up GetItemInt code and fix negative indexing of objects with length > max(Py_ssize_t)
parent
ac90a80a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
13 deletions
+70
-13
Cython/Utility/ObjectHandling.c
Cython/Utility/ObjectHandling.c
+31
-13
tests/run/index.pyx
tests/run/index.pyx
+39
-0
No files found.
Cython/Utility/ObjectHandling.c
View file @
3cc3601c
...
...
@@ -238,6 +238,27 @@ static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
/////////////// GetItemInt.proto ///////////////
#define __Pyx_GetItemInt(o, i, size, to_py_func, is_list, wraparound, boundscheck) \
(((size) <= sizeof(Py_ssize_t)) ? \
__Pyx_GetItemInt_Fast(o, i, is_list, wraparound, boundscheck) : \
__Pyx_GetItemInt_Generic(o, to_py_func(i)))
{{
for
type
in
[
'
List
'
,
'
Tuple
'
]}}
#define __Pyx_GetItemInt_{{type}}(o, i, size, to_py_func, is_list, wraparound, boundscheck) \
(((size) <= sizeof(Py_ssize_t)) ? \
__Pyx_GetItemInt_{{type}}_Fast(o, i, wraparound, boundscheck) : \
__Pyx_GetItemInt_Generic(o, to_py_func(i)))
static
CYTHON_INLINE
PyObject
*
__Pyx_GetItemInt_
{{
type
}}
_Fast
(
PyObject
*
o
,
Py_ssize_t
i
,
int
wraparound
,
int
boundscheck
);
{{
endfor
}}
static
CYTHON_INLINE
PyObject
*
__Pyx_GetItemInt_Generic
(
PyObject
*
o
,
PyObject
*
j
);
static
CYTHON_INLINE
PyObject
*
__Pyx_GetItemInt_Fast
(
PyObject
*
o
,
Py_ssize_t
i
,
int
is_list
,
int
wraparound
,
int
boundscheck
);
/////////////// GetItemInt ///////////////
static
CYTHON_INLINE
PyObject
*
__Pyx_GetItemInt_Generic
(
PyObject
*
o
,
PyObject
*
j
)
{
PyObject
*
r
;
if
(
!
j
)
return
NULL
;
...
...
@@ -247,11 +268,6 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j
}
{{
for
type
in
[
'
List
'
,
'
Tuple
'
]}}
#define __Pyx_GetItemInt_{{type}}(o, i, size, to_py_func, is_list, wraparound, boundscheck) \
(((size) <= sizeof(Py_ssize_t)) ? \
__Pyx_GetItemInt_{{type}}_Fast(o, i, wraparound, boundscheck) : \
__Pyx_GetItemInt_Generic(o, to_py_func(i)))
static
CYTHON_INLINE
PyObject
*
__Pyx_GetItemInt_
{{
type
}}
_Fast
(
PyObject
*
o
,
Py_ssize_t
i
,
int
wraparound
,
int
boundscheck
)
{
#if CYTHON_COMPILING_IN_CPYTHON
...
...
@@ -268,11 +284,6 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_{{type}}_Fast(PyObject *o, Py_ss
}
{{
endfor
}}
#define __Pyx_GetItemInt(o, i, size, to_py_func, is_list, wraparound, boundscheck) \
(((size) <= sizeof(Py_ssize_t)) ? \
__Pyx_GetItemInt_Fast(o, i, is_list, wraparound, boundscheck) : \
__Pyx_GetItemInt_Generic(o, to_py_func(i)))
static
CYTHON_INLINE
PyObject
*
__Pyx_GetItemInt_Fast
(
PyObject
*
o
,
Py_ssize_t
i
,
int
is_list
,
int
wraparound
,
int
boundscheck
)
{
#if CYTHON_COMPILING_IN_CPYTHON
...
...
@@ -291,13 +302,20 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
Py_INCREF
(
r
);
return
r
;
}
}
else
{
/* inlined PySequence_GetItem() */
}
else
{
/* inlined PySequence_GetItem()
+ special cased length overflow
*/
PySequenceMethods
*
m
=
Py_TYPE
(
o
)
->
tp_as_sequence
;
if
(
likely
(
m
&&
m
->
sq_item
))
{
if
(
wraparound
&&
unlikely
(
i
<
0
)
&&
likely
(
m
->
sq_length
))
{
Py_ssize_t
l
=
m
->
sq_length
(
o
);
if
(
unlikely
(
l
<
0
))
return
NULL
;
i
+=
l
;
if
(
likely
(
l
>=
0
))
{
i
+=
l
;
}
else
{
// if length > max(Py_ssize_t), maybe the object can wrap around itself?
if
(
PyErr_ExceptionMatches
(
PyExc_OverflowError
))
PyErr_Clear
();
else
return
NULL
;
}
}
return
m
->
sq_item
(
o
,
i
);
}
...
...
tests/run/index.pyx
View file @
3cc3601c
...
...
@@ -9,6 +9,9 @@ import sys
if
sys
.
version_info
<
(
2
,
5
):
__doc__
=
__doc__
.
replace
(
u"'int' object ..."
,
u'unsubscriptable object'
)
cdef
Py_ssize_t
maxsize
=
getattr
(
sys
,
'maxsize'
,
getattr
(
sys
,
'maxint'
,
None
))
py_maxsize
=
maxsize
import
cython
def
index_tuple
(
tuple
t
,
int
i
):
...
...
@@ -160,3 +163,39 @@ def large_literal_index(object o):
True
"""
return
o
[
1000000000000000000000000000000
]
class
LargeIndexable
(
object
):
def
__len__
(
self
):
raise
OverflowError
def
__getitem__
(
self
,
index
):
return
index
def
test_large_indexing
(
obj
):
"""
>>> obj = LargeIndexable()
>>> zero, pone, none, pmaxsize, nmaxsize = test_large_indexing(obj)
>>> # , p2maxsize, n2maxsize
>>> zero
0
>>> pone
1
>>> none
-1
>>> pmaxsize == py_maxsize
True
>>> nmaxsize == -py_maxsize
True
#>>> p2maxsize == py_maxsize*2
#True
#>>> n2maxsize == -py_maxsize*2
#True
"""
return
(
obj
[
0
],
obj
[
1
],
obj
[
-
1
],
obj
[
maxsize
],
obj
[
-
maxsize
],
#obj[maxsize*2], obj[-maxsize*2] # FIXME!
)
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