On Oct 31, 2009, at 6:18 AM, Stefan Behnel wrote:

> Hi,
>
> I noticed that Robert used the PY_NEW() hack in the Alioth shootout
> implementations, which got me into rethinking how to deal with this.  
> I know
> that it's faster and all that, so there's a need to do this.  
> However, I
> dislike the fact that one needs to write a header file and a cdef- 
> external
> declaration just to get to this feature.

Yep, it's always been considered a hack, though a very useful one. It  
comes from way back in the day when it was (perceived to be) easier to  
work around Pyrex than modify it.

> To fix this, I pushed a change that optimises this
>
>       cdef type some_type = ...
>       obj = some_type.__new__(some_type)
>
> into a call to the tp_new slot. It requires that the name is used  
> twice
> (just like it works in Python), and it also requires the object to be
> either declared as 'type' or be the name of an extension type. This is
> because Python does the type checking in the Python __new__()  
> method, not
> in the slot function, so we have to make sure this optimisation  
> doesn't
> lead to errors being ignored that would otherwise be caught by a  
> real call
> to __new__().
>
> I think this is safe, but I wanted to mention it here so that others  
> can
> give it another bit of thought.
>
> I know that there are better ways to do this behind the scenes. If  
> we know
> that an extension type (hierarchy) does not define __init__(), we  
> can even
> optimise a simple type instantiation call into a tp_new() call.  
> However,
> the above is an explicit way of doing it, it's the normal Python  
> spelling,
> and it doesn't prevent further optimisations in the future. So I  
> think it's
> worth it.

Very much so. Thanks! The implementation certainly seems safe to me.

- Robert

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to