Fix T284, cdef list setitem
authorRobert Bradshaw <robertwb@math.washington.edu>
Sat, 18 Apr 2009 08:02:12 +0000 (01:02 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sat, 18 Apr 2009 08:02:12 +0000 (01:02 -0700)
Cython/Compiler/ExprNodes.py
tests/bugs.txt
tests/run/cdef_setitem_T284.pyx

index 3bca440a21114defff541343942b3264ee059cc5..2abce3f392e26681d237e8084208a5867d840f8d 100644 (file)
@@ -1856,14 +1856,13 @@ class IndexNode(ExprNode):
             index_code = self.index.py_result()
             if self.base.type is dict_type:
                 function = "PyDict_SetItem"
-            elif self.base.type is list_type:
-                function = "PyList_SetItem"
-            # don't use PyTuple_SetItem(), as we'd normally get a
-            # TypeError when changing a tuple, while PyTuple_SetItem()
-            # would allow updates
-            #
-            #elif self.base.type is tuple_type:
-            #    function = "PyTuple_SetItem"
+            # It would seem that we could specalized lists/tuples, but that
+            # shouldn't happen here. 
+            # Both PyList_SetItem PyTuple_SetItem and a Py_ssize_t as input, 
+            # not a PyObject*, and bad conversion here would give the wrong 
+            # exception. Also, tuples are supposed to be immutable, and raise 
+            # TypeErrors when trying to set their entries (PyTuple_SetItem 
+            # is for creating new tuples from). 
             else:
                 function = "PyObject_SetItem"
         code.putln(
index 1d8ba7e1c361b774a2f62cb5fe0bcff6d60a12a3..ce41eb17f4b15d8b255bdbe0f03b17091747cda2 100644 (file)
@@ -14,4 +14,3 @@ large_consts_T237
 bad_c_struct_T252
 missing_baseclass_in_predecl_T262
 ifelseexpr_T267
-cdef_setitem_T284
index 3fd4c6d9817cf1851b44b870650565a3558c7549..81edbba46dde1e3f6503a2a9fc0d276d7cc54c46 100644 (file)
@@ -1,6 +1,12 @@
 __doc__ = u'''
 >>> no_cdef()
 >>> with_cdef()
+>>> test_list(range(11), -2, None)
+[0, 1, 2, 3, 4, 5, 6, 7, 8, None, 10]
+>>> test_list(range(11), "invalid index", None)
+Traceback (most recent call last):
+...
+TypeError: list indices must be integers
 '''
 def no_cdef():
     lst = range(11)
@@ -15,3 +21,7 @@ def with_cdef():
     lst[ob] = -10
     cdef dict dd = {}
     dd[ob] = -10
+
+def test_list(list L, object i, object a):
+    L[i] = a
+    return L