Re: [Cython] cython doesn't compile cdef'd variables of type set?
author"didier deshommes" <dfdeshom@gmail.com>
Fri, 10 Oct 2008 08:39:16 +0000 (01:39 -0700)
committer"didier deshommes" <dfdeshom@gmail.com>
Fri, 10 Oct 2008 08:39:16 +0000 (01:39 -0700)
Cython/Compiler/Builtin.py
Cython/Compiler/PyrexTypes.py
tests/run/set.pyx [new file with mode: 0644]

index 89ca0f97ba0c3e969f6f9da42d30534434f7dbe7..8b9f16f77102cc82cda01330c04ce6a80404cbdc 100644 (file)
@@ -105,14 +105,18 @@ builtin_types_table = [
                                     ("keys",  "O",   "O", "PyDict_Keys"),
                                     ("values","O",   "O", "PyDict_Values")]),
 
-    ("set",       "PySet_Type",       []),
+    ("set",       "PySet_Type",    [("clear",   "O",  "i", "PySet_Clear"), 
+                                    ("discard", "OO", "i", "PySet_Discard"),
+                                    ("add",     "OO", "i", "PySet_Add"),
+                                    ("pop",     "O",  "O", "PySet_Pop")]),
+    
     ("frozenset", "PyFrozenSet_Type", []),
 
     ("slice",   "PySlice_Type",    []),
     ("file",    "PyFile_Type",     []),
 
 ]
-
+        
 builtin_structs_table = [
     ('Py_buffer', 'Py_buffer',
      [("buf",        PyrexTypes.c_void_ptr_type),
index 55ccc80df67d04657064f293228c6ca551f21273..da18bcfc93964385ac9834b0fd70dc02027de13a 100644 (file)
@@ -6,7 +6,6 @@ import StringEncoding
 import Naming
 import copy
 
-
 class BaseType:
     #
     #  Base class for all Pyrex types including pseudo-types.
@@ -297,7 +296,12 @@ class BuiltinObjectType(PyObjectType):
         return type.is_pyobject and self.assignable_from(type)
         
     def type_test_code(self, arg):
-        return 'likely(Py%s_CheckExact(%s)) || (%s) == Py_None || (PyErr_Format(PyExc_TypeError, "Expected %s, got %%s", Py_TYPE(%s)->tp_name), 0)' % (self.name[0].upper() + self.name[1:], arg, arg, self.name, arg)
+        type = self.name.capitalize()
+        if type == 'Set': 
+            type = 'AnySet'
+        elif type == 'Frozenset':
+            type = 'FrozenSet'
+        return 'likely(Py%s_CheckExact(%s)) || (%s) == Py_None || (PyErr_Format(PyExc_TypeError, "Expected %s, got %%s", Py_TYPE(%s)->tp_name), 0)' % (type, arg, arg, self.name, arg)
 
     def declaration_code(self, entity_code, 
             for_display = 0, dll_linkage = None, pyrex = 0):
diff --git a/tests/run/set.pyx b/tests/run/set.pyx
new file mode 100644 (file)
index 0000000..b5a2141
--- /dev/null
@@ -0,0 +1,42 @@
+__doc__ = u"""
+>>> test_set_add()
+set(['a', 1])
+>>> test_set_clear()
+set([])
+>>> test_set_pop()
+set([])
+>>> test_set_discard()
+set([233, '12'])
+"""
+
+def test_set_add():
+    cdef set s1
+    s1 = set([1])
+    s1.add(1)
+    s1.add('a')
+    s1.add(1)
+    return s1
+
+def test_set_clear():
+    cdef set s1
+    s1 = set([1])
+    s1.clear()
+    return s1
+
+def test_set_pop():
+    cdef set s1
+    s1 = set()
+    s1.add('2')
+    two = s1.pop()
+    return s1
+
+def test_set_discard():
+    cdef set s1
+    s1 = set()
+    s1.add('12')
+    s1.add(3)
+    s1.add(233)
+    s1.discard('3')
+    s1.discard(3)
+    return s1
+