From: W. Trevor King Date: Tue, 15 Mar 2011 19:52:23 +0000 (-0400) Subject: Add preliminary cdef visibility tests. They pass, but I don't think they're right... X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=febd8e9d7d49b02fde672c753c6d2ba2b7455b89;p=cython.git Add preliminary cdef visibility tests. They pass, but I don't think they're right yet. --- diff --git a/tests/errors/cdef_visibility.pyx b/tests/errors/cdef_visibility.pyx new file mode 100644 index 00000000..ff3664ae --- /dev/null +++ b/tests/errors/cdef_visibility.pyx @@ -0,0 +1,38 @@ +# TODO: a way to keep going after parsing errors? + +cdef private int priv_global_variable + +cdef private priv_global_function(x): + return x*2 + + +cdef class bare_global_class: + cdef private int priv_attribute + + def __cinit__(self): + self.priv_attribute = 1 + + +cdef class public pub_global_class: + cpdef cp_method(self, x): + return x*2 + + +cdef class private priv_global_class: + cpdef cp_method(self, x): + return x*2 + + +cdef class readonly ro_global_class: + cpdef cp_method(self, x): + return x*2 + + +cpdef class cp_global_class: + cpdef cp_method(self, x): + return x*2 + + +_ERRORS = u""" +3:17: Syntax error in C variable declaration +""" diff --git a/tests/run/cdef_visibility.pyx b/tests/run/cdef_visibility.pyx new file mode 100644 index 00000000..88b0d937 --- /dev/null +++ b/tests/run/cdef_visibility.pyx @@ -0,0 +1,191 @@ +__doc__ = u""" + +Due to module-level entries being Python objects, we cannot enforce +any visibility flags for the entries. Perhaps they should raise +errors on compilation to avoid suprises? We should still hide the +private ones, as we do with functions. + +>>> bare_global_variable = 1 +>>> print bare_global_variable +1 +>>> bare_global_variable = 'one' +>>> bare_global_variable +'one' +>>> pub_global_variable = 1 +>>> print pub_global_variable +1 +>>> pub_global_variable = 'one' +>>> pub_global_variable +'one' +>>> priv_global_variable = 1 +>>> print priv_global_variable +1 +>>> priv_global_variable = 'one' +>>> priv_global_variable +'one' +>>> ro_global_variable = 1 +>>> print ro_global_variable +1 +>>> ro_global_variable = 'one' +>>> ro_global_variable +'one' +>>> cp_global_variable = 1 +>>> print cp_global_variable +1 +>>> cp_global_variable = 'one' +>>> cp_global_variable +'one' + +>>> print bare_global_function(1) +Traceback (most recent call last): +NameError: name 'bare_global_function' is not defined +>>> bare_global_function = 'one' +>>> bare_global_function +'one' +>>> print pub_global_function(1) +Traceback (most recent call last): +NameError: name 'pub_global_function' is not defined +>>> pub_global_function = 'one' +>>> pub_global_function +'one' +>>> #print priv_global_function(1) +>>> #priv_global_function = lambda x: x*3 +>>> print ro_global_function(1) +Traceback (most recent call last): +NameError: name 'ro_global_function' is not defined +>>> ro_global_function = 'one' +>>> ro_global_function +'one' +>>> print cp_global_function(1) +2 +>>> cp_global_function = 'one' +>>> cp_global_function +'one' + +>>> x = bare_global_class() +>>> #x = pub_global_class() +>>> #x = priv_global_class() +>>> #x = ro_global_class() +>>> #x = cp_global_class() + +>>> x = bare_global_class() + +>>> print x.bare_attribute +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'bare_attribute' +>>> x.bare_attribute = 2 +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'bare_attribute' +>>> print x.bare_attribute +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'bare_attribute' + +>>> print x.pub_attribute +1 +>>> x.pub_attribute = 2 +>>> print x.pub_attribute +2 +>>> x.pub_attribute = 'one' +Traceback (most recent call last): +TypeError: an integer is required +>>> x.pub_attribute +2 + +>>> print x.priv_attribute +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'priv_attribute' +>>> x.priv_attribute = 2 +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'priv_attribute' + +>>> print x.ro_attribute +1 +>>> x.ro_attribute = 2 +Traceback (most recent call last): +AttributeError: attribute 'ro_attribute' of 'cdef_visibility.bare_global_class' objects is not writable +>>> print x.ro_attribute +1 + +>>> print x.cp_attribute +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'cp_attribute' +>>> x.cp_attribute = 2 +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'cp_attribute' + + +>>> print x.bare_method(1) +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'bare_method' +>>> x.bare_method = 'one' +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'bare_method' + +>>> print x.pub_method(1) +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'pub_method' +>>> x.pub_method = 'one' +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'pub_method' + +>>> #print x.priv_method(1) +>>> #x.priv_method = 'one' +>>> #print x.priv_method(1) + +>>> print x.ro_method(1) +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'ro_method' +>>> x.ro_method = 'one' +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object has no attribute 'ro_method' + +>>> print x.cp_method(1) +2 +>>> x.cp_method = 'one' +Traceback (most recent call last): +AttributeError: 'cdef_visibility.bare_global_class' object attribute 'cp_method' is read-only +>>> print x.cp_method(1) +2 +""" + +cdef int bare_global_variable +cdef public int pub_global_variable +cdef readonly int ro_global_variable +cpdef int cp_global_variable + +cdef bare_global_function(x): + return x*2 + +cdef public pub_global_function(x): + return x*2 + +cdef readonly ro_global_function(x): + return x*2 + +cpdef cp_global_function(x): + return x*2 + + +cdef class bare_global_class: + cdef int bare_attribute + cdef public int pub_attribute + cdef readonly int ro_attribute + cpdef int cp_attribute + + def __cinit__(self): + self.bare_attribute = 1 + self.pub_attribute = 1 + self.ro_attribute = 1 + self.cp_attribute = 1 + + cdef bare_method(self, x): + return x*2 + + cdef public pub_method(self, x): + return x*2 + + cdef readonly ro_method(self, x): + return x*2 + + cpdef cp_method(self, x): + return x*2