[Cython] CF based type inference

Stefan Behnel stefan_ml at behnel.de
Wed May 9 15:27:25 CEST 2012

Vitja Makarov, 09.05.2012 15:16:
> 2012/5/9 Vitja Makarov:
>>>> On 9 May 2012 13:39, Vitja Makarov wrote:
>>>>> 2012/5/9 Vitja Makarov:
>>>>>> 2012/5/9 Stefan Behnel:
>>>>>>>  def test():
>>>>>>>      x = 1    # inferred as int
>>>>>>>      del x    # error: Deletion of non-Python, non-C++ object
>>>>>>> http://trac.cython.org/cython_trac/ticket/768
>>>>>>> It might be enough to infer "object" for names that are being del-ed for
>>>>>>> now, and to fix "del" The Right Way when we split entries.
>>>>>> Do you mean that `x` should be inferred as "python object" in your example?
>>>>>> Yes, we may add workaround for del <unspecified_type> case.
>>>>>> Del is represented now by NameDeletion with the same rhs and lhs.
>>>>>> We can add method infer_type() to NameAssignment and use it instead of
>>>>>> Node.infer_type()

Yes, looks ok.

>>>>> Here I've tried to fix it, now deletion always infers as python_object
>>>>> https://github.com/vitek/cython/commit/225c9c60bed6406db46e87da31596e053056f8b7
>>>>> That may break C++ object deletion
>> Yeah, this code doesn't work now:
>> cdef extern from "foo.h":
>>    cdef cppclass Foo:
>>        Foo()
>> def foo():
>>    foo = new Foo()
>>    print typeof(foo)
>>    del foo
>> And I'm not sure how to fix it.
> I've fixed cppclasses:
> https://github.com/vitek/cython/commit/f5acf44be0f647bdcbb5a23c8bfbceff48f4414e

Sure, that makes sense. If the type cannot be del-ed, we'll get an error
elsewhere - not a concern of type inference.

> About memoryviews:
> from cython cimport typeof
> def foo(float[::1] a):
>     b = a
>     #del b
>     print typeof(b)
>     print typeof(a)
> In this example `b` is inferred as 'Python object' and not
> `float[::1]`, is that correct?

I think it currently is, but it may no longer be in the future. See the
running ML thread about the future of the buffer syntax and the memoryview

If we're up to changing this, it would be good to give it a suitable
behaviour right for the next release, so that users don't start relying on
the above.


More information about the cython-devel mailing list