File: cysignals_example.pyx

package info (click to toggle)
cysignals 1.12.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 500 kB
  • sloc: ansic: 854; python: 367; makefile: 186; sh: 20
file content (25 lines) | stat: -rw-r--r-- 659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from cysignals.signals cimport sig_check
from cysignals.memory cimport check_allocarray


def recip_sum(long count):
    cdef double s = 0
    cdef long i
    for i in range(1, count + 1):
        sig_check()
        s += 1 / <double>i
    return s


cdef long* safe_range_long(long count) except? NULL:
    """
    This function can be safely called within a sig_on block.

    With an ordinary malloc, this is not the case since the internal
    state of the heap would be messed up if an interrupt happens during
    malloc().
    """
    cdef long* a = <long*>check_allocarray(count, sizeof(long))
    for i in range(count):
        a[i] = i
    return a