[Cython] memoryview slices can't be None?

Robert Bradshaw robertwb at math.washington.edu
Wed Feb 8 09:22:34 CET 2012

On Sat, Feb 4, 2012 at 11:39 AM, Dag Sverre Seljebotn
<d.s.seljebotn at astro.uio.no> wrote:
>> Block-local declarations are definitely something we want, although I
>> think it would require some more (non-trivial) changes to the
>> compiler.
> Note that my proposal was actually not about block-local declarations.
> Block-local:
> {
>   int x = 4;
> }
> /* x not available here */
> My idea was much more like hints to control flow analysis. That is, I wanted
> to have this raise an error:
> x = 'adf'
> if foo():
>    cdef int x = y
> print x # type of x not known
> This is OK:
> if foo():
>    cdef int x = y
> else:
>    cdef int x = 4
> print x # ok, type the same anyway -- so type "escapes" block
> And I would allow
> cdef str x = y
> if foo:
>    cdef int x = int(x)
>    return g(x) # x must be int
> print x # x must be str at this point
> The reason for this madness is simply that control statements do NOT create
> blocks in Python, and making it so in Cython is just confusing. It would
> bring too much of C into the language for my taste.

I think the above examples (especially the last one) are a bit
confusing as well. Introducing the notion of (implicit) block scoping
is not very Pythonic. We would need something to be able to support
local cdef classes, but I think a with statement is more appropriate
for that as there's a notion of doing non-trivial work when exiting
the block.

> I think that in my Cython-utopia, Symtab.py is only responsible for
> resolving the scope of *names*, and types of things are not bound to blocks,
> just to the state at control flow points.
> Of course, implementing this would be a nightmare.
>> Maybe the cleanup code from functions, as well as the temp handling
>> etc could be re-factored to a BlockNode, that all block nodes could
>> subclass. They'd have to instantiate new symbol table environments as
>> well. I'm not yet entirely sure what else would be involved in the
>> implementation of that.
>>> But I like int[:] as a way of making it pure Python syntax compatible as
>>> well. Perhaps the two are orthogonal -- a) make variable declaration a
>>> statement, b) make cython.int[:](x) do, essentially, a cdef declaration,
>>> for
>>> Python compatability.
>> Don't we have cython.declare() for that? e.g.
>>     arr = cython.declare(cython.int[:])
>> That would also be treated as a statement like normal declarations (if
>> and when implemented).
> This was what I said, but it wasn't what I meant. Sorry. I'll try to explain
> better:
> 1)  There's no way to have the above actually do the right thing in Python.
> With "arr = cython.int[:](arr)" one could actually return a NumPy or
> NumPy-like array that works in Python (since "arr" might not have the
> "shape" attribute before the conversion, all we know is that it exports the
> buffer interface...).
> 2) I don't like the fact that we overload the assignment operator to acquire
> a view. "cdef np.ndarray[int] x = y" is fine since if you do "x.someattr"
> then a NumPy subclass could provide someattr and it works fine. Acquiring a
> view is just something different.
> 3) Hence I guess I like "arr = int[:](arr)" better both for Cython and
> Python; at least if "arr" is always type-inferred to be int[:], even if arr
> was an "object" further up in the code (really, if you do "x = f(x)" at the
> top-level of the function, then x can just take the identity of another
> variable from that point on -- I don't know if the current control flow
> analysis and type inferences does this though?)
> Dag Sverre
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> http://mail.python.org/mailman/listinfo/cython-devel

More information about the cython-devel mailing list