<s> import math <EOL> import errno <EOL> import py <EOL> import sys <EOL> from pypy . rpython . lltypesystem import lltype , rffi <EOL> from pypy . tool . sourcetools import func_with_new_name <EOL> from pypy . rlib import rposix <EOL> from pypy . translator . tool . cbuild import ExternalCompilationInfo <EOL> math_frexp = rffi . llexternal ( '<STR_LIT>' , [ rffi . DOUBLE , rffi . INTP ] , rffi . DOUBLE , <EOL> sandboxsafe = True ) <EOL> math_modf = rffi . llexternal ( '<STR_LIT>' , [ rffi . DOUBLE , rffi . DOUBLEP ] , rffi . DOUBLE , <EOL> sandboxsafe = True ) <EOL> math_ldexp = rffi . llexternal ( '<STR_LIT>' , [ rffi . DOUBLE , rffi . INT ] , rffi . DOUBLE , <EOL> sandboxsafe = True ) <EOL> unary_math_functions = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> binary_math_functions = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> def ll_math_frexp ( x ) : <EOL> exp_p = lltype . malloc ( rffi . INTP . TO , <NUM_LIT:1> , flavor = '<STR_LIT>' ) <EOL> mantissa = math_frexp ( x , exp_p ) <EOL> exponent = rffi . cast ( lltype . Signed , exp_p [ <NUM_LIT:0> ] ) <EOL> lltype . free ( exp_p , flavor = '<STR_LIT>' ) <EOL> return ( mantissa , exponent ) <EOL> def ll_math_modf ( x ) : <EOL> intpart_p = lltype . malloc ( rffi . DOUBLEP . TO , <NUM_LIT:1> , flavor = '<STR_LIT>' ) <EOL> fracpart = math_modf ( x , intpart_p ) <EOL> intpart = intpart_p [ <NUM_LIT:0> ] <EOL> lltype . free ( intpart_p , flavor = '<STR_LIT>' ) <EOL> return ( fracpart , intpart ) <EOL> def ll_math_ldexp ( x , exp ) : <EOL> _error_reset ( ) <EOL> r = math_ldexp ( x , exp ) <EOL> _check_error ( r ) <EOL> return r <EOL> def _error_reset ( ) : <EOL> rposix . set_errno ( <NUM_LIT:0> ) <EOL> ERANGE = errno . ERANGE <EOL> def _check_error ( x ) : <EOL> errno = rposix . get_errno ( ) <EOL> if errno : <EOL> if errno == ERANGE : <EOL> if not x : <EOL> return <EOL> raise OverflowError ( "<STR_LIT>" ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if sys . platform [ : <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> eci = ExternalCompilationInfo ( libraries = [ ] ) <EOL> else : <EOL> eci = ExternalCompilationInfo ( libraries = [ '<STR_LIT:m>' ] ) <EOL> def new_unary_math_function ( name ) : <EOL> c_func = rffi . llexternal ( name , [ rffi . DOUBLE ] , rffi . DOUBLE , <EOL> compilation_info = eci , sandboxsafe = True ) <EOL> def ll_math ( x ) : <EOL> _error_reset ( ) <EOL> r = c_func ( x ) <EOL> _check_error ( r ) <EOL> return r <EOL> return func_with_new_name ( ll_math , '<STR_LIT>' + name ) <EOL> def new_binary_math_function ( name ) : <EOL> if sys . platform == '<STR_LIT:win32>' and name in ( '<STR_LIT>' , ) : <EOL> cname = '<STR_LIT:_>' + name <EOL> else : <EOL> cname = name <EOL> c_func = rffi . llexternal ( cname , [ rffi . DOUBLE , rffi . DOUBLE ] , rffi . DOUBLE , <EOL> compilation_info = eci , sandboxsafe = True ) <EOL> def ll_math ( x , y ) : <EOL> _error_reset ( ) <EOL> r = c_func ( x , y ) <EOL> _check_error ( r ) <EOL> return r <EOL> return func_with_new_name ( ll_math , '<STR_LIT>' + name ) <EOL> for name in unary_math_functions : <EOL> globals ( ) [ '<STR_LIT>' + name ] = new_unary_math_function ( name ) <EOL> for name in binary_math_functions : <EOL> globals ( ) [ '<STR_LIT>' + name ] = new_binary_math_function ( name ) </s>
