From 59840c9af1b80fca0296e50aabfbb7a2f3ee52c7 Mon Sep 17 00:00:00 2001 From: "\"didier deshommes\"" Date: Fri, 10 Oct 2008 01:39:16 -0700 Subject: [PATCH] Re: [Cython] cython doesn't compile cdef'd variables of type set? --- Cython/Compiler/Builtin.py | 8 +++++-- Cython/Compiler/PyrexTypes.py | 8 +++++-- tests/run/set.pyx | 42 +++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 tests/run/set.pyx diff --git a/Cython/Compiler/Builtin.py b/Cython/Compiler/Builtin.py index 89ca0f97..8b9f16f7 100644 --- a/Cython/Compiler/Builtin.py +++ b/Cython/Compiler/Builtin.py @@ -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), diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 55ccc80d..da18bcfc 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -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 index 00000000..b5a21418 --- /dev/null +++ b/tests/run/set.pyx @@ -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 + -- 2.26.2