Fernando Perez, 17.11.2010 09:41:
> On Mon, Nov 15, 2010 at 9:04 AM, Darren Dale wrote:
>> def test():
>> sentinal = '3'
>> with open('foo.txt', 'w') as f:
>> sentinal in ('1', '2')
>
> as a possible workaround until the actual bug is fixed, remember that
> 'x in y' is mostly sugar for 'y.__contains__(x)',
I agree that it provides a work-around, but it's worth knowing that there's
a major performance difference. The 'in' operator is a lot faster,
especially on literal sequences, where Cython explodes it into separate
comparisons. Even in CPython, 'in' is much faster than looking up and
calling the bound Python method.
> Also worth mentioning that if you assign the tuple to a variable, the
> bug doesn't show up:
>
> dreamweaver[~]> cat bad.pyx
> def test():
> sentinel = '3'
> with open('foo.txt', 'w') as f:
> a = ('1', '2')
> sentinel in a
>
> dreamweaver[~]> cython bad.pyx
> dreamweaver[~]> # no crash, though I didn't test that it behaves correctly.
I think this is a better work-around. It's still slower in Cython (even
though I just implemented interning of constant tuples for 0.13.1), but it
should be much faster than calling __contains__().
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev