Ejemplo n.º 1
0
    >>> raised[1] is None
    False
    """
    result = False
    should_raise_bool = True if should_raise else False  # help the type inference ...
    with nogil:
        print("WORKS")
        with cython.nogil:
            result = True
            if should_raise_bool:
                raise ValueError("RAISED!")
    return result


MyUnion = cython.union(n=cython.int, x=cython.double)
MyStruct = cython.struct(is_integral=cython.bint, data=MyUnion)
MyStruct2 = cython.typedef(MyStruct[2])


def test_struct(n, x):
    """
    >>> test_struct(389, 1.64493)
    (389, 1.64493)
    """
    a = cython.declare(MyStruct2)
    a[0] = MyStruct(is_integral=True, data=MyUnion(n=n))
    a[1] = MyStruct(is_integral=False, data={'x': x})
    return a[0].data.n, a[1].data.x


import cython as cy
Ejemplo n.º 2
0
# -*- coding: utf-8 -*-
# @Author: lcl1026504480
# @Date:   2019-08-29 17:11:37
# @Last Modified by:   lcl1026504480
# @Last Modified time: 2019-08-29 17:17:06
import cython
cython.declare(x=cython.int, x_ptr=cython.p_int)
x = 5
x_ptr = cython.address(x)
cython.declare(n=cython.longlong)
print(cython.sizeof(cython.longlong))
n = 400
print(cython.sizeof(n))
MyStruct = cython.struct(x=cython.int, y=cython.int, data=cython.double)
a = cython.declare(MyStruct)
T = cython.typedef(cython.p_int)  # ctypedef int* T
# t1 = cython.cast(T, t)
# t2 = cython.cast(T, t, typecheck=True)
Ejemplo n.º 3
0
    >>> test_with_nogil(nogil())
    WORKS
    True
    >>> raised
    [None]
    """
    result = False
    with nogil:
        print("WORKS")
        with cython.nogil:
            result = True
    return result

MyUnion = cython.union(n=cython.int, x=cython.double)
MyStruct = cython.struct(is_integral=cython.bint, data=MyUnion)
MyStruct2 = cython.typedef(MyStruct[2])

def test_struct(n, x):
    """
    >>> test_struct(389, 1.64493)
    (389, 1.64493)
    """
    a = cython.declare(MyStruct2)
    a[0] = MyStruct(is_integral=True, data=MyUnion(n=n))
    a[1] = MyStruct(is_integral=False, data={'x': x})
    return a[0].data.n, a[1].data.x

import cython as cy
from cython import declare, cast, locals, address, typedef, p_void, compiled
from cython import declare as my_declare, locals as my_locals, p_void as my_void_star, typedef as my_typedef, compiled as my_compiled
Ejemplo n.º 4
0
try:
    from typing import Optional
except ImportError:
    pass


def optional_pytypes(i: Optional[int], f: Optional[float]):
    pass


def optional_cython_types(i: Optional[cython.int], d: Optional[cython.double], f: Optional[cython.float]):
    pass


MyStruct = cython.struct(a=cython.int, b=cython.double)

def optional_cstruct(x: Optional[MyStruct]):
    pass


_ERRORS = """
15:29: Only Python type arguments can use typing.Optional[...]
15:54: Only Python type arguments can use typing.Optional[...]
15:82: Only Python type arguments can use typing.Optional[...]
21:24: Only Python type arguments can use typing.Optional[...]

# FIXME: these should be allowed!
11:24: Only Python type arguments can use typing.Optional[...]
11:42: Only Python type arguments can use typing.Optional[...]
"""