Ticket #14080: 14080_cdefs.patch

File 14080_cdefs.patch, 8.5 KB (added by jdemeyer, 7 years ago)
  • sage/combinat/degree_sequences.pyx

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1360249588 -3600
    # Node ID c137b6e209e3211404b6dccd1a631626c14c4425
    # Parent  a6fe6b3fabaeba221b0764f81e64ed3b6c8c4d11
    Simplify cdefs.pxi using Cython's libc module
    
    diff --git a/sage/combinat/degree_sequences.pyx b/sage/combinat/degree_sequences.pyx
    a b  
    372372        Freeing the memory
    373373        """
    374374        if seq != NULL:
    375             free(seq)
     375            sage_free(seq)
    376376
    377377cdef init(int n):
    378378    """
     
    387387    elif n == 1:
    388388        return [[0]]
    389389
    390     seq = <unsigned char *> malloc((n+1)*sizeof(unsigned char))
     390    sig_on()
     391    seq = <unsigned char *> sage_malloc((n+1)*sizeof(unsigned char))
    391392    memset(seq,0,(n+1)*sizeof(unsigned char))
     393    sig_off()
    392394
    393395    # We begin with one vertex of degree 0
    394396    seq[0] = 1
     
    396398    N = n
    397399    sequences = []
    398400    enum(1,0)
    399     free(seq)
     401    sage_free(seq)
    400402    return sequences
    401403
    402404cdef inline add_seq():
  • sage/ext/cdefs.pxi

    diff --git a/sage/ext/cdefs.pxi b/sage/ext/cdefs.pxi
    a b  
     1#
     2# Declare C library functions used in Sage
     3#
    14
    25include "python.pxi"
    36
    4 cdef extern from "stdlib.h":
    5     void free(void *ptr)
    6     void *malloc(size_t size)
    7     void *realloc(void *ptr, size_t size)
    8     size_t strlen(char *s)
    9     char *strcpy(char *dest, char *src)
     7from libc.stdio cimport *
     8from libc.string cimport strlen, strcpy, memset, memcpy
    109
    11 cdef extern from "string.h":
    12     void *memset(void *dest, int c, size_t n)
    13     void *memcpy(void *dest, void *src, size_t n)
     10from libc.math cimport sqrt
     11# Cython misdeclares these: http://trac.cython.org/cython_trac/ticket/801
     12cdef extern from "<math.h>":
     13    double frexp(double x, int* exponent)
     14    double ldexp(double x, int exponent)
    1415
    15 cdef extern from "stdio.h":
    16     ctypedef struct FILE
    17     cdef FILE *stdin
    18     cdef FILE *stdout
    19     cdef FILE *stderr
    20     int printf(char *format, ...)
    21     int fprintf(FILE *stream, char *format, ...)
    22     int sprintf(char *str, char *format, ...)
    23     FILE *fopen(char *path, char *mode)
    24     int fclose(FILE *stream)
    25     int fflush(FILE *stream)
    26     int scanf(char *format, ...)
    27 
    28 cdef extern from "math.h":
    29     double sqrt(double x)
    30     float roundf(float x)    # linux-ish and non-standard; avoid!
    31     double ldexp(double x, int exp)
    32     double frexp(double x, int *exp)
    3316
    3417from sage.libs.gmp.all cimport *
    35 cdef extern from "gmp.h":
    36     pass # cython bug sometimes includes this in the wrong place
    37 
    38 ##########################################################################
    39 # stdsage.pxi declares the macros, etc., that got used a lot in SAGE.
    40 ##########################################################################
    41    
     18cdef extern from "<gmp.h>":
     19    pass  # Cython bug sometimes includes this in the wrong place
  • sage/ext/gmp.pxi

    diff --git a/sage/ext/gmp.pxi b/sage/ext/gmp.pxi
    a b  
    1111
    1212include '../ext/interrupt.pxi'
    1313
     14cimport libc.stdlib
     15
    1416############ The following is the "one global set of vars"
    1517cdef extern from "gmp_globals.h":
    1618    cdef mpz_t u, v, q, u0, u1, u2, v0, v1, v2, t0, t1, t2, x, y, ssqr, m2
     
    4143    Convert a GMP integer to a Python string.
    4244    """
    4345    cdef char *s
     46    sig_on()
    4447    s = mpz_get_str(NULL, 10, x)
    4548    t = str(s)
    46     free(s)
     49    # Emulate sage_free() to avoid needing to include stdsage.pxi
     50    sig_block()
     51    libc.stdlib.free(s)
     52    sig_unblock()
     53    sig_off()
    4754    return t
    4855
    4956cdef int mpz_print(mpz_t x) except -1:
  • new file sage/ext/signals.pxi

    diff --git a/sage/ext/signals.pxi b/sage/ext/signals.pxi
    new file mode 100644
    - +  
     1# Declare system calls related to signal handling
     2
     3cdef extern from "<stdlib.h>":
     4    void abort()
     5
     6cdef extern from "<signal.h>":
     7    ctypedef void *sigset_t
     8    # Renaming of this struct is necessary because Cython folds the
     9    # "struct" namespace into the normal namespace.
     10    struct Sigaction "sigaction":
     11        void (*sa_handler)(int)
     12        sigset_t sa_mask
     13        int sa_flags
     14
     15    int SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK
     16    int SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGKILL
     17    int SIGSEGV, SIGPIPE, SIGALRM, SIGTERM, SIGBUS
     18    int signal_raise "raise"(int signum)
     19    int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
     20    int sigemptyset(sigset_t *set)
     21    int sigaddset(sigset_t *set, int signum)
     22    int sigaction(int signum, Sigaction *act, Sigaction *oldact)
     23
     24cdef extern from "<sys/time.h>":
     25    struct timespec:
     26        long tv_sec
     27        long tv_nsec
     28
     29cdef extern from "<sys/select.h>":
     30    ctypedef void *fd_set
     31    void FD_CLR(int fd, fd_set *set)
     32    bint FD_ISSET(int fd, fd_set *set)
     33    void FD_SET(int fd, fd_set *set)
     34    void FD_ZERO(fd_set *set)
     35    int FD_SETSIZE
     36
     37    int pselect(int nfds, fd_set *readfds, fd_set *writefds,
     38                fd_set *exceptfds, timespec *timeout,
     39                sigset_t *sigmask)
  • sage/gsl/probability_distribution.pyx

    diff --git a/sage/gsl/probability_distribution.pyx b/sage/gsl/probability_distribution.pyx
    a b  
    687687                map(float, parameters)
    688688            except StandardError:
    689689                raise TypeError, "F-distribution requires real parameters"
    690             self.parameters = <double *>malloc(sizeof(double)*2)
     690            self.parameters = <double *>sage_malloc(sizeof(double)*2)
    691691            self.parameters[0] = float(parameters[0])
    692692            self.parameters[1] = float(parameters[1])
    693693            self.distribution_type = F       
  • sage/libs/ecl.pyx

    diff --git a/sage/libs/ecl.pyx b/sage/libs/ecl.pyx
    a b  
    1414#rationals to SAGE types Integer and Rational. These parts could easily be
    1515#adapted to work with pure Python types.
    1616
    17 include '../ext/interrupt.pxi'
     17include "../ext/signals.pxi"
     18include "../ext/interrupt.pxi"
    1819include "../ext/cdefs.pxi"
    1920
    2021from sage.rings.integer cimport Integer
     
    3637cdef bint bint_rationalp(cl_object obj):
    3738    return not(cl_rationalp(obj) == Cnil)
    3839
    39 cdef extern from "stdlib.h":
    40     void abort()
    41 
    42 cdef extern from "signal.h":
    43     int signal_raise "raise"(int sig)
    44     #rename of struct is necessary because cython folds the "struct" namespace
    45     #into the normal namespace
    46     struct Sigaction "sigaction":
    47         pass
    48     int sigaction(int sig, Sigaction * act, Sigaction * oact)
    49     int SIGINT, SIGBUS, SIGSEGV
    50 
    5140cdef extern from "eclsig.c":
    5241    int ecl_sig_on() except 0
    5342    void ecl_sig_off()
  • sage/matrix/matrix_modn_dense_template.pxi

    diff --git a/sage/matrix/matrix_modn_dense_template.pxi b/sage/matrix/matrix_modn_dense_template.pxi
    a b  
    9494cdef sage.rings.fast_arith.arith_int ArithIntObj
    9595ArithIntObj  = sage.rings.fast_arith.arith_int()
    9696
    97 # for copying
    98 cdef extern from *:
    99     void* memcpy(void* dst, void* src, long n)
    100 
    101 # for pickling
    102 cdef extern from "stdio.h":
    103     int snprintf(char *str, size_t size, char *format, ...)
     97# for copying/pickling
     98from libc.string cimport memcpy
     99from libc.stdio cimport snprintf
    104100
    105101from sage.modules.vector_modn_dense cimport Vector_modn_dense
    106102
  • sage/modular/modsym/heilbronn.pyx

    diff --git a/sage/modular/modsym/heilbronn.pyx b/sage/modular/modsym/heilbronn.pyx
    a b  
    2727from sage.libs.flint.flint cimport *
    2828include "../../libs/flint/fmpz_poly.pxi"
    2929
     30cdef extern from "<math.h>":
     31    float roundf(float x)
     32
    3033cimport p1list
    3134import  p1list
    3235cdef p1list.export export
  • sage/tests/interrupt.pyx

    diff --git a/sage/tests/interrupt.pyx b/sage/tests/interrupt.pyx
    a b  
    1818
    1919import signal
    2020
    21 cdef extern from 'stdlib.h':
    22     void abort()
    23 
    24 cdef extern from 'signal.h':
    25     int SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGKILL, \
    26         SIGSEGV, SIGPIPE, SIGALRM, SIGTERM, SIGBUS
    27 
    2821cdef extern from '../tests/c_lib.h':
    2922    void ms_sleep(long ms)
    3023    void signal_after_delay(int signum, long ms)
     
    3427    ctypedef int volatile_int "volatile int"
    3528
    3629
     30include '../ext/signals.pxi'
    3731include '../ext/interrupt.pxi'
    3832include '../ext/stdsage.pxi'
    3933
  • setup.py

    diff --git a/setup.py b/setup.py
    a b  
    521521system_header_files = \
    522522    ['complex.h', 'signal.h', 'math.h', 'limits.h', 'stdlib.h',
    523523     'arpa/inet.h', 'float.h', 'string.h', 'stdint.h', 'stdio.h',
    524      'dlfcn.h', 'setjmp.h' ]
     524     'dlfcn.h', 'setjmp.h', 'errno.h' ]
    525525
    526526class DependencyTree:
    527527    """