[Cython] [Bug] Unable to cast to python type.

Carlos Pita carlosjosepita at gmail.com
Sun Oct 18 10:39:41 EDT 2015


I've been trying to understand the problem here. The cause is easy to
explain, for the cure I would like to consult the savvy before PR'ing.

The cause:
  * Parsing of '<...>' will instantiate a typecast from a basetype node and
a declarator node, then the typecast type will be inferred from these two
nodes.
  * ParseTreeTransform of 'cast(....)' will instead instantiate a typecast
node directly from a type (parsed as 'sizeof(...)'). So there will be no
self.base_type in this part of TypecastNode.analyse_types:

       if to_py and not from_py:
            ....
            elif self.operand.type.can_coerce_to_pyobject(env):
                self.result_ctype = py_object_type
                base_type = self.base_type.analyse(env)
                self.operand = self.operand.coerce_to(base_type, env)
            ....

The question: Why isn't the last line simply "self.operand =
self.operand.coerce_to(self.type, env)"? That is, what's the point of
coercing to the base type here instead of coercing to the type itself? A
fortiori, notice this is inside the to_py case, I'm not even sure that
base_type makes sense here. For example:

- Pointer base type cannot be a Python object (for <A*>).
- Const base type cannot be a Python object (for <const A>).
- Cannot cast to a function type (for <A()>).

(the compiler dixit)

Cheers
--
Carlos



On Fri, Oct 16, 2015 at 4:05 PM, Carlos Pita <carlosjosepita at gmail.com>
wrote:

> Hi,
>
> import cython as cy
> y = cy.cast('list', x)
>
> fails to compile with "AttributeError: 'TypecastNode' object has no
> attribute 'typecheck'".
>
> But the following examples do compile:
>
> y = <list> x
>
> y = cy.cast('int', x)
>
> Cheers
> --
> Carlos
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20151018/49342ca5/attachment.html>


More information about the cython-devel mailing list