[Cython] Possible bug (or wrong documentation) WRT "not None"

Samuele Kaplun Samuele.Kaplun at cern.ch
Tue Dec 2 14:13:42 CET 2014


Hi!

I would like to report what I think is a possible bug (in cython or 
corresponding documentation).

According to:
<http://docs.cython.org/src/userguide/extension_types.html#extension-types-and-none>

[...]
The self parameter of a method of an extension type is guaranteed never to be 
None.
[...]

However given this snippet:
[...]
cdef class test:
    def __sub__(self, test rhs not None):
        print "self is %s" % repr(self), "rhs is %s" % repr(rhs)
[...]

If I then load it python:
In [1]: import test
In [2]: x = test.test()
In [3]: None - x
self is None rhs is <test.test object at 0x7fdd9eeb7410>

Thus showing that in this case "self" was actually initialized with None.

If I explictly add "not None" as in:
[...]
cdef class test:
    def __sub__(self not None, test rhs not None):
        print "self is %s" % repr(self), "rhs is %s" % repr(rhs)
[...]
In [3]: None - x
TypeError: Argument 'self' must not be None
[...]

Which is good.

If I instead add "test self" as in:
[...]
cdef class test:
    def __sub__(test self not None, test rhs not None):
        print "self is %s" % repr(self), "rhs is %s" % repr(rhs)
[...]
In [3]: None - x
TypeError: Argument 'self' has incorrect type (expected test.test, got 
NoneType)
[...]

What's worse is that if I use "test self" (without not None), the same 
TypeError is not raised:
[...]
cdef class test:
    def __sub__(test self, test rhs not None):
        print "self is %s" % repr(self), "rhs is %s" % repr(rhs)
[...]
In [3]: None - x
self is None rhs is <test.test object at 0x7f17b36ff3f0>

Thus we have:
self -> not respecting None-checking (in disagreement with Documentation)
self not None ->  OK
test self -> not respecting type-checking!
test self non None -> OK (respecting type-checking, hence None-checking).

Hope that helps. Cheers,
	Samuele

-- 
Samuele Kaplun
Invenio Developer ** <http://invenio-software.org/>



More information about the cython-devel mailing list