# 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
|
|
19 | 19 | def __call__(self, *args, **kwds): |
20 | 20 | return self.eval(*args, **kwds) |
21 | 21 | |
22 | | def eval(self,x,globals=None, locals=None): |
| 22 | def eval(self, x, globals=None, locals=None): |
23 | 23 | """ |
24 | 24 | EXAMPLES:: |
25 | 25 | |
26 | 26 | 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) |
29 | 29 | sage: import numpy |
30 | 30 | 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 |
33 | 33 | 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:... |
34 | 41 | """ |
35 | 42 | if len(x.splitlines()) == 1 and os.path.exists(x): |
36 | 43 | filename = x |
… |
… |
|
38 | 45 | if filename.lower().endswith('.f90'): |
39 | 46 | x = '!f90\n' + x |
40 | 47 | 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 | |
44 | 49 | from numpy import f2py |
45 | 50 | old_import_path=os.sys.path |
46 | 51 | cwd=os.getcwd() |
… |
… |
|
60 | 65 | |
61 | 66 | # if the first line has !f90 as a comment gfortran will treat it as |
62 | 67 | # fortran 90 code |
63 | | if x.startswith('!f90'): |
| 68 | if x.startswith('!f90'): |
64 | 69 | fname = os.path.join(tmp_filename() +'.f90') |
65 | 70 | else: |
66 | 71 | fname = os.path.join(tmp_filename() +'.f') |
67 | 72 | |
68 | 73 | 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) |
71 | 76 | |
72 | 77 | f2py.compile(x, name, extra_args = extra_args, source_fn=fname) |
73 | 78 | |
… |
… |
|
76 | 81 | os.unlink(log) |
77 | 82 | os.unlink(fname) |
78 | 83 | |
| 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 | |
79 | 92 | if self.verbose: |
80 | 93 | print log_string |
81 | 94 | |
… |
… |
|
88 | 101 | return |
89 | 102 | finally: |
90 | 103 | os.sys.path=old_import_path |
91 | | os.unlink(name + '.so') |
| 104 | os.unlink(soname) |
92 | 105 | |
93 | 106 | for k, x in m.__dict__.iteritems(): |
94 | 107 | if k[0] != '_': |