Commit f7efb99a authored by Stefan Behnel's avatar Stefan Behnel

make ctuples available in pure python code

parent 6a67efec
...@@ -16,6 +16,8 @@ Features added ...@@ -16,6 +16,8 @@ Features added
* Cpdef enums are now first-class iterable, callable types in Python. * Cpdef enums are now first-class iterable, callable types in Python.
* Ctuples can now be declared in pure Python code.
* Posix declarations for DLL loading and stdio extensions were added. * Posix declarations for DLL loading and stdio extensions were added.
Patch by Lars Buitinck. Patch by Lars Buitinck.
......
...@@ -7172,6 +7172,16 @@ class TupleNode(SequenceNode): ...@@ -7172,6 +7172,16 @@ class TupleNode(SequenceNode):
node.is_partly_literal = True node.is_partly_literal = True
return node return node
def analyse_as_type(self, env):
# ctuple type
if not self.args:
return None
item_types = [arg.analyse_as_type(env) for arg in self.args]
if any(t is None for t in item_types):
return None
entry = env.declare_tuple_type(self.pos, item_types)
return entry.type
def coerce_to(self, dst_type, env): def coerce_to(self, dst_type, env):
if self.type.is_ctuple: if self.type.is_ctuple:
if dst_type.is_ctuple and self.type.size == dst_type.size: if dst_type.is_ctuple and self.type.size == dst_type.size:
......
...@@ -222,6 +222,8 @@ The Python types int, long and bool are interpreted as C ``int``, ``long`` ...@@ -222,6 +222,8 @@ The Python types int, long and bool are interpreted as C ``int``, ``long``
and ``bint`` respectively. Also, the Python builtin types ``list``, ``dict``, and ``bint`` respectively. Also, the Python builtin types ``list``, ``dict``,
``tuple``, etc. may be used, as well as any user defined types. ``tuple``, etc. may be used, as well as any user defined types.
Typed C-tuples can be declared as a tuple of C types.
Extension types and cdef functions Extension types and cdef functions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
......
...@@ -141,6 +141,7 @@ cpdef (int, double) cpdef_ctuple_return_type(int x, double y): ...@@ -141,6 +141,7 @@ cpdef (int, double) cpdef_ctuple_return_type(int x, double y):
""" """
return x, y return x, y
@cython.infer_types(True) @cython.infer_types(True)
def test_type_inference(): def test_type_inference():
""" """
...@@ -155,6 +156,28 @@ def test_type_inference(): ...@@ -155,6 +156,28 @@ def test_type_inference():
assert cython.typeof(xo) == "tuple object", cython.typeof(xo) assert cython.typeof(xo) == "tuple object", cython.typeof(xo)
@cython.locals(a=(int,int), b=(cython.long,cython.float))
def test_pure_python_declaration(x, y):
"""
>>> test_pure_python_declaration(1, 2)
(int, int)
(long, float)
((1, 2), (1, 2.0))
>>> test_pure_python_declaration(1.0, 2.0)
(int, int)
(long, float)
((1, 2), (1, 2.0))
>>> test_pure_python_declaration('x', 'y')
Traceback (most recent call last):
TypeError: an integer is required
"""
a = (x, y)
b = (x, y)
print(cython.typeof(a))
print(cython.typeof(b))
return (a, b)
def test_equality((int, int) ab, (int, int) cd, (int, int) ef): def test_equality((int, int) ab, (int, int) cd, (int, int) ef):
""" """
>>> test_equality((1, 2), (3, 4), (5, 6)) >>> test_equality((1, 2), (3, 4), (5, 6))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment