Пример #1
0
def test_cast(x):
    """
    >>> test_cast(1.5)
    1
    """
    n = cython.cast(cython.int, x)
    return n
Пример #2
0
def multi_args_init_cast():
    """
    >>> multi_args_init_cast()
    True
    """
    f = Foo(10, 20, 30)
    return cython.cast(Foo, f) is f
Пример #3
0
def _complex_dblquad(func, a, b, gfun, hfun, kwargs={}):
    """Integrate real and imaginary part of the given function."""
    if cython_imported and cython.compiled:
        # pure python formulation of: cdef void *f_ptr = <void*>func
        f_ptr = cython.declare(cython.p_void, cython.cast(cython.p_void, func))

        func_capsule = PyCapsule_New(f_ptr, cython.NULL, cython.NULL)

        current_module = sys.modules[__name__]

        ll_real_2d_func_c = LowLevelCallable.from_cython(current_module,
                                                         '_real_2d_func_c',
                                                         func_capsule)
        ll_imag_2d_func_c = LowLevelCallable.from_cython(current_module,
                                                         '_imag_2d_func_c',
                                                         func_capsule)
        real, real_tol = dblquad(ll_real_2d_func_c, a, b, gfun, hfun, **kwargs)
        imag, imag_tol = dblquad(ll_imag_2d_func_c, a, b, gfun, hfun, **kwargs)
    else:
        real, real_tol = dblquad(
            _real_2d_func, a, b, gfun, hfun, (func,), **kwargs)
        imag, imag_tol = dblquad(
            _imag_2d_func, a, b, gfun, hfun, (func,), **kwargs)

    return real + 1j*imag, real_tol, imag_tol
Пример #4
0
def test_cast(x):
    """
    >>> test_cast(1.5)
    1
    """
    n = cython.cast(cython.int, x)
    return n
Пример #5
0
def _real_1d_func_c(n, arr, func_ptr):
    """Return real part of a 1d function.

    Cython implementation.
    """
    # pure python formulation of:
    # return (<Beam2dCartesian>func_ptr)(arr[0]).real
    return cython.cast(Beam2d, func_ptr)._integrand(arr[0]).real
Пример #6
0
def get_column_letter(idx):
    """Convert a column index into a column letter
    (3 -> 'C')
    """
    int_idx = cython.cast(cython.int, idx)  # may also raise ValueError
    if 1 <= int_idx <= MAX_COL_INDEX:
        return _STRING_COL_CACHE[int_idx]
    raise ValueError(f"Invalid column index {idx}")
Пример #7
0
def _real_2d_func_c(n, arr, func_ptr):
    """Return real part of a 2d function.

    Cython implementation.
    """
    # pure python formulation of:
    # return (<Beam3d>func_ptr)(arr[0], arr[1]).real
    return cython.cast(Beam3d, func_ptr)._integrand(arr[0], arr[1]).real
Пример #8
0
def same_type_cast():
    """
    >>> same_type_cast()
    True
    """

    f = EmptyClass()
    return f is cython.cast(EmptyClass, f)
Пример #9
0
def none_cast():
    """
    >>> none_cast() is None
    True
    """

    f = None
    return cython.cast(EmptyClass, f)
Пример #10
0
    def peek(self) -> cython.int:
        value: cython.int = cast(cython.Py_ssize_t, cqueue.queue_peek_head(self._c_queue))

        if value == 0:
            # this may mean that the queue is empty,
            # or that it happens to contain a 0 value
            if cqueue.queue_is_empty(self._c_queue):
                raise IndexError("Queue is empty")
        return value
Пример #11
0
def init_thread_local():
    if cython.compiled:
        global _thread_local_c
        _thread_local.state = _thread_state()
        _thread_local_c = cython.cast(cython.p_void,
                                      _thread_local.state)  # type: ignore
    _get_thread_local().ref_enabled = None
    _get_thread_local().immutable_ctx = False
    _get_thread_local().record_lookups = None
Пример #12
0
    def init(self, cid, sio: object, data_format):
        self.cid = cid
        self.sio = sio  # type: CySimpleIO
        self.data_format = data_format

        if self.sio and cy.cast(cy.bint,
                                self.sio.definition.has_output_declared):
            self._payload = SimpleIOPayload(
                self.sio, self.sio.definition.all_output_elem_names, self.cid,
                self.data_format)
            self._has_sio_output = True
Пример #13
0
def test_cast(x):
    """
    >>> test_cast(1.5)
    1
    >>> test_cast(None) # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    TypeError: ...
    """
    n = cython.cast(cython.int, x)
    return n
Пример #14
0
    def _extract_payload_attrs(self, item: object) -> dict:
        """ Extract response attributes from a single object. Used with items other than dicts.
        """
        extracted: dict = {}
        is_dict: bint = isinstance(item, dict)

        # Use a different function depending on whether the object is dict-like or not.
        # Note that we need .get to be able to provide a default value.
        has_get = hasattr(item, 'get')  # type: bool
        name = None

        for name in self.all_output_elem_names:  # type: str
            if is_dict:
                value = cy.cast(dict, item).get(name, _not_given)
            elif has_get:
                value = item.get(name, _not_given)
            else:
                value = getattr(item, name, _not_given)
            if value is not _not_given:
                extracted[name] = value

        return extracted
Пример #15
0
 def receive(self, windowed_value):
     self.update_counters_start(windowed_value)
     for consumer in self.consumers:
         cython.cast(Operation, consumer).process(windowed_value)
     self.update_counters_finish()
Пример #16
0
 def output(self, windowed_value, output_index=0):
     cython.cast(Receiver,
                 self.receivers[output_index]).receive(windowed_value)
Пример #17
0
 def pop(self) -> cython.int:
     if cqueue.queue_is_empty(self._c_queue):
         raise IndexError("Queue is empty")
     return cast(cython.Py_ssize_t, cqueue.queue_pop_head(self._c_queue))
Пример #18
0
 def append(self, value: cython.int):
     if not cqueue.queue_push_tail(self._c_queue,
             cast(cython.p_void, cast(cython.Py_ssize_t, value))):
         raise MemoryError()
Пример #19
0
 def output(self, windowed_value, output_index=0):
     # type: (WindowedValue, int) -> None
     cython.cast(Receiver,
                 self.receivers[output_index]).receive(windowed_value)
Пример #20
0
 def output(self, windowed_value, output_index=0):
   cython.cast(Receiver, self.receivers[output_index]).receive(windowed_value)
Пример #21
0
 def receive(self, windowed_value):
   self.update_counters_start(windowed_value)
   for consumer in self.consumers:
     cython.cast(Operation, consumer).process(windowed_value)
   self.update_counters_finish()
Пример #22
0
def _get_thread_local():
    if cython.compiled:
        assert _thread_local_c != cython.NULL  # type: ignore
        return cython.cast(_thread_state, _thread_local_c)  # type: ignore
    else:
        return _thread_local  # type: ignore