[Numpy-discussion] doctest fortran I/O synchronization

David M. Cooke cookedm at physics.mcmaster.ca
Fri Oct 10 09:40:05 CDT 2003


On Fri, Oct 10, 2003 at 10:17:54AM -0400, Todd Miller wrote:
> I'm trying to make a doctest to verify that the different flow patterns
> of f2py interfaces work with different varieties of numarrays (normal,
> byte-swapped, misaligned, dis-contiguous, type-converted).  I'm trying
> to test this out under Linux with g77, and (it seems like) I'm having
> trouble synchronizing the Fortran I/O with Python's C I/O.
> 
> Given foo.f:
> 
>       subroutine in_c(a,m,n)
>       real*8 a(n,m)
> Cf2py intent(in,c) a
> Cf2py depend(a) ::  n=shape(a,0),  m=shape(a,1)
>       do j=1,m
>          do i=1,n
>             write (6,1) a(i,j)
>  1          format( $, 1F3.0, ', ')
>          enddo
>          print *,''
>       enddo
>       end
> 
> And given f2py_tests.py:
> 
> """
> >>> foo.in_f(a)
>  0.,  5., 10.,
>  1.,  6., 11.,
>  2.,  7., 12.,
>  3.,  8., 13.,
>  4.,  9., 14.,
> """
> import foo, numarray
> 
> def test():
>     import doctest
>     global a
>     t = doctest.Tester(globs=globals())
>     a = numarray.arange(15., shape=(3,5))
>     t.runstring(__doc__, "c_array")
>     return t.summarize()
> 
> I get this:
> 
> [jmiller at halloween ~/f2py_tests]$ python f2py_tests.py
>  0.,  5., 10.,
>  1.,  6., 11.,
>  2.,  7., 12.,
>  3.,  8., 13.,
>  4.,  9., 14.,
> *****************************************************************
> Failure in example: foo.in_f(a)
> from line #1 of c_array
> Expected:
>  0.,  5., 10.,
>  1.,  6., 11.,
>  2.,  7., 12.,
>  3.,  8., 13.,
>  4.,  9., 14.,
> Got:
> *****************************************************************
> 1 items had failures:
>    1 of   1 in c_array
> ***Test Failed*** 1 failures.
> 
> Where it appears that the output from the first example somehow escapes
> the C I/O system I presume doctest is using.  The actual test I'm

doctest uses Python's I/O system: it assigns a new object to
sys.stdout. Your code uses Fortran's output, which would go the same
place a printf in C would: to the program's stdout (file descriptor 1).

You'd need to run the code in a separate process, and capture the
output. Something along the lines of this:

import commands
def test_f2py():
    """
    put your doctest here
    """
    output = commands.getoutput('python f2pytest1.py')
    print output

Or, set your test up to write output to a file instead of stdout, then
read that file (that's probably better).

> writing has multiple examples, and the fortran I/O *does* make it into
> the doctest after the first example  but remains out of sync.

It's out of sync because it's not going through Python; Python has
absolutely no clue that the Fortran code wrote anything.

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca




More information about the Numpy-discussion mailing list