Dag Sverre Seljebotn wrote:
> Lisandro Dalcin wrote:
>> On Tue, Sep 15, 2009 at 3:19 AM, Stefan Behnel <[email protected]> wrote:
>>>>> 1) When calling foo(a), we could emit a compile-time warning and
>>>>> generate C code with runtime type-check... What's the point of duck
>>>>> typing here iff foo() actually requires a tuple?
>>>> We don't want to generate so many warnings they become useless, I
>>>> personally wouldn't want that on by default. It's nice because a lot
>>>> of the inputs, return results, etc. are not typed, e.g. I can do foo
>>>> (bar(x)) where bar is any old Python function.
>>> -1 on "untyped" warnings. We still /discourage/ explicit typing in most 
>>> cases.
>>>
>> Final note: cdef functions with no argument typecheck at all are as
>> dangerous as Fortran 77... IMHO, Cython should do better, just because
>> Python/Cython users are not real programmers ;-) ...
>>
>>
>>
>> But the current status is far from optimal... cdef functions do not do
>> internally any typecheck on typed arguments (for performance
>> reasons?), then you can call it with anything, and you can easily
>> end-up with a segfault...
> 
> This got confusing to me. I always had the impression that:
> 
> a) def foo(MyType arg): ...
> 
> means that the foo function itself checks the type on entry.
> 
> b) cdef foo(MyType arg): ...
> 
> means that foo does not check the type, but the caller does! (Except, of 
> course, if the caller already know the type, which is a useful 
> optimization.)
> 
> Is this understanding (of current behaviour) true or false?

Answering my own question: It is true. The testcase below works, you do 
NOT end up with a segfault, because a check is inserted in "caller". 
Perfect IMO.


"""
 >>> caller(4)
Traceback (most recent call last):
     ...
TypeError: Cannot convert int to dagss.MyType
"""

cdef class MyType:
     cdef int value
     def __init__(self): self.value = 20

cdef foo(MyType p):
     print p.value

def caller(o):
     foo(o)



> 
> Whatever the <cast operator> does is something else entirely and much 
> less important IMO.
> 
>> I understand that we /discourage/ explicit typing and agree with that.
>> However, I think that if a user INSIST in using explicit typing for
>> some args in a cdef function, she should be prepared to make sure that
>> when calling that function, the argument have a proper, known type at
>> Cython compile-time... In fact, this is a way to discourage even more
>> explicit typing... And this will help catch nasty bugs when explicit
>> typing is used.
> 
> I think the behaviour described above is perfect, that is: If you do
> 
> cdef foo(MyType arg): ...
> 
> def caller(x):
>      foo(x)
> 
> then "caller" has code inserted to check that x is indeed MyType before 
> passing it. It is exactly the same case as
> 
> cdef MyType y = x
> 
> However, of course, if you do
> 
> def other_caller(MyType x):
>      print "entered"
>      foo(x)
> 
> then there is (of course) no need to do another type check beyond the 
> one that is done before "entered" is printed.

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

Reply via email to