[Pyrex] __delitem__ doesn't seem to work if __setitem__ isn't present

Richard Boulton richard at tartarus.org
Fri Jul 1 18:27:12 CEST 2005


I believe I've found a bug in pyrex.  The two following test cases
demonstrate the problem, and a workaround.

I'm using pyrex 0.9.3, on Debian (stable).

Firstly, a testcase which doesn't work as expected:

====BEGIN:foo.pyx====
cdef class foo:
    def __delitem__(self, key):
    print "Delitem %s" % key
====END:foo.pyx====
====BEGIN:test.py====
#!/usr/bin/python
import foo
bar = foo.foo()
del bar[0]
====END:test.py====

Compiling foo.pyx to foo.so, and running ./test.py produces:

$ ./test.py
Traceback (most recent call last):
  File "./test.py", line 4, in ?
    del bar[0]
TypeError: object doesn't support item deletion


I would expect it to print "Delitem 0"



I have found that adding a __setitem__ method makes the __delitem__
method work as expected:

====BEGIN:foo.pyx====
cdef class foo:
    def __setitem__(self, key, value):
        print "Setitem %s=%s" % (key, value)
    def __delitem__(self, key):
        print "Delitem %s" % key
====END:foo.pyx====
Compiling this, and running the same test.py program produces the
expected result:

$ ./test.py
Delitem 0


I've no idea how to go about fixing this, but I thought I should let the
list know.

(You might ask why I have an extension type which supports item deletion
but not assignment.  The answer is that I have a type which has some
characteristics of a set, and some of an ordered sequence.)

Other than this oddity, I've been finding that pyrex is a great tool -
I'm very glad I found it before I attempted to wrap the C-library I'm
working on by hand (or by SWIG).

-- 
Richard Boulton <richard at tartarus.org>




More information about the Pyrex mailing list