[Pyrex] Pyrex 0.9: Compiled code produces segmentation fault

Matthias Baas baas at ira.uka.de
Tue Jan 27 20:59:05 CET 2004


Hi,

I've come across a weird bug that can lead to a crash when Pyrex 
generated code is executed.
I have an extension type "vec3" that represents vectors with 3 float 
components and an accompanying iterator class "vec3iter" to iterate over 
the components. But using the iterator can lead to a segmentation fault. 
  However, it doesn't always happen, sometimes the iterator works as 
expected but suddenly it crashes.
I've seen it crash on both, Windows (XP, MSVC6) and Linux. And it only 
occurs when using Python 2.2.2. All tests have been ok so far when using 
Python 2.3.
The problem also does not occur when using Pyrex 0.8.2 instead of 0.9.

Now here's an example showing the problem:

-----------------------Pyrex code------------------------------

cdef class vec3:
     "A vector class with 3 floating point components."

     cdef double x,y,z

     def __new__(self):
         self.x = 1.0
         self.y = 2.0
         self.z = 3.0

     def __iter__(self):
         return vec3iter(self)


cdef class vec3iter:
     "An iterator for the vec3 type."

     cdef int idx
     cdef vec3 v

     def __new__(self, v):
         self.idx = 0
         self.v   = v

     def __iter__(self):
         return self

     def __next__(self):
         if self.idx==0:
             self.idx=1
             return self.v.x
         elif self.idx==1:
             self.idx=2
             return self.v.y
         elif self.idx==2:
             self.idx=3
             return self.v.z
         else:
             raise StopIteration

-----------------------end of Pyrex code---------------------------

When the following is executed in Python 2.2.2 it always leads to a 
crash on my machine:

v = vec3()
for i in range(10000):
     x,y,z = v

If I add a print statement like this:

v = vec3()
for i in range(10000):
     print i,v
     x,y,z = v

Then I don't get a segmentation fault anymore but this weird exception:

0 <xtest.vec3 object at 0x00869D90>
1 <xtest.vec3 object at 0x00869D90>
2 <xtest.vec3 object at 0x00869D90>
...
332 <xtest.vec3 object at 0x00869D90>
333 <xtest.vec3 object at 0x00869D90>
334 <xtest.vec3 object at 0x00869D90>
335 stdout
Traceback (most recent call last):
   File "iterbug.py", line 12, in ?
     x,y,z = v
ValueError: too many values to unpack


Personally, I have no clue what the problem might be, is this a bug in 
Pyrex, Python or my code?

- Matthias -




More information about the Pyrex mailing list