Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"cdef list" variable generates incorrect PyList_SetItem code #844

Closed
robertwb opened this issue Apr 17, 2009 · 4 comments
Closed

"cdef list" variable generates incorrect PyList_SetItem code #844

robertwb opened this issue Apr 17, 2009 · 4 comments

Comments

@robertwb
Copy link
Contributor

The first function below "in_range" works fine (the last line
generates a call to PyObject_SetItem) but the "out_of_range" function does not. The last line in it generates a call to PyList_SetItem as it should, but the 'ob' PyObject is not converted to a Py_ssize_t.

[cat _out_of_range.pyx
def in_range():
   lst = range(11)
   ob = 10
   lst[ob](590]$) = -10

def out_of_range():
   cdef list lst = range(11)
   ob = 10
   lst[= -10

Compiling raises a warning, as it should:

[591](ob])$ python setup.py build_ext --inplace
running build_ext
cythoning _out_of_range.pyx to _out_of_range.c
building '_out_of_range' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall
-Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c _out_of_range.c
-o build/temp.linux-x86_64-2.5/_out_of_range.o
_out_of_range.c: In function ‘__pyx_pf_13_out_of_range_out_of_range’:
_out_of_range.c:429: warning: passing argument 2 of ‘PyList_SetItem’
makes integer from pointer without a cast
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions
build/temp.linux-x86_64-2.5/_out_of_range.o -o _out_of_range.so

And calling it raises the exception:

[python out_of_range.py
after in_range
Traceback (most recent call last):
 File "out_of_range.py", line 5, in <module>
   _out_of_range.out_of_range()
 File "_out_of_range.pyx", line 9, in _out_of_range.out_of_range
(_out_of_range.c:429)
   lst[ob](592]$) = -10
IndexError: list assignment index out of range

This can be fixed easily with a wrapper around the index variable in the PyList_SetItem generated code in ExprNodes.py:generate_setitem_code

Migrated from http://trac.cython.org/ticket/284

@robertwb
Copy link
Contributor Author

scoder changed description from

The first function below "in_range" works fine (the last line
generates a call to PyObject_SetItem) but the "out_of_range" function
does not. The last line in it generates a call to PyList_SetItem as
it should, but the 'ob' PyObject is not converted to a Py_ssize_t.

[cat _out_of_range.pyx
def in_range():
   lst = range(11)
   ob = 10
   lst[ob](590]$) = -10

def out_of_range():
   cdef list lst = range(11)
   ob = 10
   lst[= -10

Compiling raises a warning, as it should:

[591](ob])$ python setup.py build_ext --inplace
running build_ext
cythoning _out_of_range.pyx to _out_of_range.c
building '_out_of_range' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall
-Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c _out_of_range.c
-o build/temp.linux-x86_64-2.5/_out_of_range.o
_out_of_range.c: In function ‘__pyx_pf_13_out_of_range_out_of_range’:
_out_of_range.c:429: warning: passing argument 2 of ‘PyList_SetItem’
makes integer from pointer without a cast
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions
build/temp.linux-x86_64-2.5/_out_of_range.o -o _out_of_range.so

And calling it raises the exception:

[python out_of_range.py
after in_range
Traceback (most recent call last):
 File "out_of_range.py", line 5, in <module>
   _out_of_range.out_of_range()
 File "_out_of_range.pyx", line 9, in _out_of_range.out_of_range
(_out_of_range.c:429)
   lst[ob](592]$) = -10
IndexError: list assignment index out of range

This can be fixed easily with a wrapper around the index variable in the PyList_SetItem generated code in ExprNodes.py:generate_setitem_code

to

The first function below "in_range" works fine (the last line
generates a call to PyObject_SetItem) but the "out_of_range" function does not. The last line in it generates a call to PyList_SetItem as it should, but the 'ob' PyObject is not converted to a Py_ssize_t.

[cat _out_of_range.pyx
def in_range():
   lst = range(11)
   ob = 10
   lst[ob](590]$) = -10

def out_of_range():
   cdef list lst = range(11)
   ob = 10
   lst[= -10

Compiling raises a warning, as it should:

[591](ob])$ python setup.py build_ext --inplace
running build_ext
cythoning _out_of_range.pyx to _out_of_range.c
building '_out_of_range' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall
-Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c _out_of_range.c
-o build/temp.linux-x86_64-2.5/_out_of_range.o
_out_of_range.c: In function ‘__pyx_pf_13_out_of_range_out_of_range’:
_out_of_range.c:429: warning: passing argument 2 of ‘PyList_SetItem’
makes integer from pointer without a cast
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions
build/temp.linux-x86_64-2.5/_out_of_range.o -o _out_of_range.so

And calling it raises the exception:

[python out_of_range.py
after in_range
Traceback (most recent call last):
 File "out_of_range.py", line 5, in <module>
   _out_of_range.out_of_range()
 File "_out_of_range.pyx", line 9, in _out_of_range.out_of_range
(_out_of_range.c:429)
   lst[ob](592]$) = -10
IndexError: list assignment index out of range

This can be fixed easily with a wrapper around the index variable in the PyList_SetItem generated code in ExprNodes.py:generate_setitem_code
milestone from wishlist to 0.11.2
priority from trivial to major
commented

Clearly a bug, looks like we need to coerce the index to Py_ssize_t first.

@robertwb
Copy link
Contributor Author

@robertwb commented

Coercing to a Py_ssize_t will raise the wrong kind of exception.

@robertwb
Copy link
Contributor Author

@robertwb
Copy link
Contributor Author

@robertwb changed resolution to fixed
status from new to closed
commented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant