I've attached a patch for cdef'ed sets. It should work whether you're
running 2.3 or 2.5 (In 2.3 nothing will happen and "cdef set s" should
give you a syntax error). I also had to modify PyrexTypes.py to make
sure that PyAnySet_CheckExact() was being passed, instead of
PySet_CheckExact() (there are 2 ways to check for the type of a set:
PyAnySet_CheckExact and PyFrozenSet_CheckExact). Reviews welcome!
didier
On Wed, Oct 1, 2008 at 11:50 PM, Lisandro Dalcin <[EMAIL PROTECTED]> wrote:
> On Thu, Oct 2, 2008 at 12:28 AM, didier deshommes <[EMAIL PROTECTED]> wrote:
>>
>> Ok, I've got a patch and I've looked at the generated C code, compiled
>> the module and verified the behavior of the function. How do I write
>> tests for cython? Do I just edit tests/run/dict.pyx and look at the
>> generated code?
>
> More or less, yes. Write a 'set.pyx' file like this:
>
> __doc__ = u"""
>>>> test_set()
> True
> """
>
> def test_set():
> cdef set s1 = set()
> cdef set s2 = set()
> # then do stuff with s1 and s2, like s1.add(1)
> # finally return True
> return True
>
>
> Of course, I do not know if this can be readily incorporated in Cython
> test suite, provided that this code is going to fail in the case of a
> Python 2.3 runtime.
>
>
> --
> Lisandro Dalcín
> ---------------
> Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
> Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
> Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
> PTLC - Güemes 3450, (3000) Santa Fe, Argentina
> Tel/Fax: +54-(0)342-451.1594
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>
diff -r 2976d81a9ac9 Cython/Compiler/Builtin.py
--- a/Cython/Compiler/Builtin.py Thu Oct 02 12:33:14 2008 -0400
+++ b/Cython/Compiler/Builtin.py Thu Oct 02 13:37:52 2008 -0400
@@ -105,13 +105,22 @@ builtin_types_table = [
("keys", "O", "O", "PyDict_Keys"),
("values","O", "O", "PyDict_Values")]),
- ("set", "PySet_Type", []),
+ #("set", "PySet_Type", []),
("frozenset", "PyFrozenSet_Type", []),
("slice", "PySlice_Type", []),
("file", "PyFile_Type", []),
]
+
+import types
+if hasattr(types, 'GetSetDescriptorType'):
+ builtin_types_table.append(
+ ("set", "PySet_Type", [("clear", "O", "i", "PySet_Clear"),
+ ("discard", "OO", "i", "PySet_Discard"),
+ ("add", "OO", "i", "PySet_Add"),
+ ("pop", "O", "O", "PySet_Pop")])
+ )
builtin_structs_table = [
('Py_buffer', 'Py_buffer',
diff -r 2976d81a9ac9 Cython/Compiler/PyrexTypes.py
--- a/Cython/Compiler/PyrexTypes.py Thu Oct 02 12:33:14 2008 -0400
+++ b/Cython/Compiler/PyrexTypes.py Thu Oct 02 13:37:52 2008 -0400
@@ -295,7 +295,10 @@ 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'
+ 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):
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev