Tests for #258.
authorRobert Bradshaw <robertwb@math.washington.edu>
Fri, 27 Nov 2009 20:08:09 +0000 (12:08 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Fri, 27 Nov 2009 20:08:09 +0000 (12:08 -0800)
tests/errors/builtin_type_conflict_T170.pyx [deleted file]
tests/run/extern_builtins_T258.pyx [new file with mode: 0644]

diff --git a/tests/errors/builtin_type_conflict_T170.pyx b/tests/errors/builtin_type_conflict_T170.pyx
deleted file mode 100644 (file)
index b05b628..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-cdef extern from *:
-    ctypedef class __builtin__.list [object PyListObject]:
-        pass
-
-cdef list foo = []
-
-# This is too invasive for Python 0.11.x, re-enable in 0.12
-NEW_ERRORS = u"""
-:2:4: list already a builtin Cython type
-"""
-
-_ERRORS = u"""
-5:16: Cannot coerce list to type 'list'
-"""
-
diff --git a/tests/run/extern_builtins_T258.pyx b/tests/run/extern_builtins_T258.pyx
new file mode 100644 (file)
index 0000000..bd9c0c5
--- /dev/null
@@ -0,0 +1,52 @@
+cdef extern from "Python.h":
+    
+    ctypedef class __builtin__.str  [object PyStringObject]:
+        cdef long ob_shash
+
+    ctypedef class __builtin__.list  [object PyListObject]:
+        cdef Py_ssize_t ob_size
+        cdef Py_ssize_t allocated
+
+    ctypedef class __builtin__.dict  [object PyDictObject]:
+        pass
+
+cdef str s = "abc"
+cdef list L = [1,2,4]
+cdef dict d = {'A': 'a'}
+
+    
+def test_list(list L):
+    """
+    >>> test_list(range(10))
+    True
+    >>> class list_subclass(list): pass
+    >>> test_list(list_subclass([1,2,3]))
+    True
+    """
+    return L.ob_size <= L.allocated
+
+def test_str(str s):
+    """
+    >>> test_str("abc")
+    True
+    >>> class str_subclass(str): pass
+    >>> test_str(str_subclass("xyz"))
+    True
+    """
+    cdef char* ss = s
+    return hash(s) == s.ob_shash
+
+def test_tuple(tuple t):
+    """
+    Actual builtin types are restrictive wrt subclassing so optimizations can be safely performed.
+    
+    >>> test_tuple((1,2))
+    2
+    >>> class tuple_subclass(tuple): pass
+    >>> test_tuple(tuple_subclass((1,2)))
+    Traceback (most recent call last):
+    ...
+    TypeError: Argument 't' has incorrect type (expected tuple, got tuple_subclass)
+    """
+    return len(t)
+