From 85bdf910e70ae3e62c7bc3a2b003777ee9dd10da Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Fri, 27 Nov 2009 12:08:09 -0800 Subject: [PATCH] Tests for #258. --- tests/errors/builtin_type_conflict_T170.pyx | 15 ------ tests/run/extern_builtins_T258.pyx | 52 +++++++++++++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) delete mode 100644 tests/errors/builtin_type_conflict_T170.pyx create mode 100644 tests/run/extern_builtins_T258.pyx diff --git a/tests/errors/builtin_type_conflict_T170.pyx b/tests/errors/builtin_type_conflict_T170.pyx deleted file mode 100644 index b05b6280..00000000 --- a/tests/errors/builtin_type_conflict_T170.pyx +++ /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 index 00000000..bd9c0c5d --- /dev/null +++ b/tests/run/extern_builtins_T258.pyx @@ -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) + -- 2.26.2