Ticket #13157: 13157_inline_fortran.patch

File 13157_inline_fortran.patch, 3.5 KB (added by jdemeyer, 7 years ago)
  • sage/misc/inline_fortran.py

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1345628310 -7200
    # Node ID b7b88bae573b49d7ed2e97b1a984f7fb7041a100
    # Parent  d5446b8024815063e032899eb796d249dda8099e
    Fix inline_fortran
    
    diff --git a/sage/misc/inline_fortran.py b/sage/misc/inline_fortran.py
    a b  
    1919    def __call__(self, *args, **kwds):
    2020        return self.eval(*args, **kwds)
    2121
    22     def eval(self,x,globals=None, locals=None):
     22    def eval(self, x, globals=None, locals=None):
    2323        """
    2424        EXAMPLES::
    2525       
    2626            sage: from sage.misc.inline_fortran import InlineFortran, _example
    27             sage: test_fortran = InlineFortran(globals())   # optional -- fortran
    28             sage: test_fortran(_example)                    # optional -- fortran
     27            sage: fortran = InlineFortran(globals())
     28            sage: fortran(_example)
    2929            sage: import numpy
    3030            sage: n = numpy.array(range(10),dtype=float)
    31             sage: fib(n,int(10))                            # optional -- fortran
    32             sage: n                                         # optional -- fortran
     31            sage: fib(n,int(10))
     32            sage: n
    3333            array([  0.,   1.,   1.,   2.,   3.,   5.,   8.,  13.,  21.,  34.])
     34
     35        TESTS::
     36
     37            sage: fortran.eval("SYNTAX ERROR !@#$")
     38            Traceback (most recent call last):
     39            ...
     40            RuntimeError: failed to compile Fortran code:...
    3441        """
    3542        if len(x.splitlines()) == 1 and os.path.exists(x):
    3643            filename = x
     
    3845            if filename.lower().endswith('.f90'):
    3946                x = '!f90\n' + x
    4047        global count
    41         # On linux g77_shared should be a script that runs sage_fortran -shared
    42         # On OS X it should be a script that runs gfortran -bundle -undefined dynamic_lookup
    43         path = os.environ['SAGE_LOCAL']+'/bin/sage-g77_shared'
     48
    4449        from numpy import f2py
    4550        old_import_path=os.sys.path
    4651        cwd=os.getcwd()
     
    6065
    6166        # if the first line has !f90 as a comment gfortran will treat it as
    6267        # fortran 90 code
    63         if x.startswith('!f90'):       
     68        if x.startswith('!f90'):
    6469            fname = os.path.join(tmp_filename() +'.f90')
    6570        else:
    6671            fname = os.path.join(tmp_filename() +'.f')
    6772
    6873        log = tmp_filename()
    69         extra_args = '--quiet --f77exec=%s --f90exec=%s %s %s  1>&2 >"%s"'%(
    70                     path, path, s_lib_path, s_lib, log)
     74        extra_args = '--quiet --f77exec=sage-inline-fortran --f90exec=sage-inline-fortran %s %s >"%s" 2>&1'%(
     75            s_lib_path, s_lib, log)
    7176
    7277        f2py.compile(x, name, extra_args = extra_args, source_fn=fname)
    7378
     
    7681        os.unlink(log)
    7782        os.unlink(fname)
    7883
     84        # f2py.compile() doesn't raise any exception if it fails.
     85        # So we manually check whether the compiled file exists.
     86        # NOTE: the .so extension is used, even on OS X where .dylib
     87        # would be expected.
     88        soname = name + '.so'
     89        if not os.path.isfile(soname):
     90            raise RuntimeError("failed to compile Fortran code:\n" + log_string)
     91
    7992        if self.verbose:
    8093            print log_string
    8194       
     
    88101            return
    89102        finally:
    90103            os.sys.path=old_import_path
    91             os.unlink(name + '.so')
     104            os.unlink(soname)
    92105       
    93106        for k, x in m.__dict__.iteritems():
    94107            if k[0] != '_':