[Numpy-discussion] f2py: optional parameters and present() bug?

Garry Willgoose garry.willgoose@newcastle.edu...
Wed Apr 23 04:11:00 CDT 2008


in a F90 routine called from python the present() test (for optional  
parameters) always returns true whether the parameter is present in  
the function call or not. My F90 test code looks like this

subroutine test(test1,test2,test3)
   integer,intent(in) :: test1
   integer,intent(in),optional :: test2
   integer,intent(inout),optional :: test3
     write (*,*) test1,present(test2),present(test3)
     if (present(test2)) write (*,*) 'test2',test2
     if (present(test3)) then
       write (*,*) 'test3',test3
       test3=5
     end if
end subroutine test

The output from python calls to this routine (its inside a module  
tsdtm.tsg ... just to explain why the function looks like it does). I  
have just created this with f2py automatically generating the  
interface with no hand modification of the interfaces. What this test  
shows is that present() always returns true no matter (1) the intent  
(2) whether the parameter is present in the call, or (3) the type of  
the parameter (real, integer ... though I haven't tested arrays etc)  
as well.

Secondly I can't see how to get the returned value of 'test3'. It  
doesn't seem to be returned as a result of the function?

The docs say f2py handles optional parameters and other than present 
() my testing suggests that optional parameters seem otherwise OK.  
I'm on Mac OSX and using numpy 1.0.3. Any thoughts?

 >>> print tsimdtm.tsg.test.__doc__
test - Function signature:
   test(test1,[test2,test3])
Required arguments:
   test1 : input int
Optional arguments:
   test2 : input int
   test3 : in/output rank-0 array(int,'i')
 >>> tsimdtm.tsg.test(1)
  1 T T
  test2 0
  test3 0
 >>> tsimdtm.tsg.test(1,2)
  1 T T
  test2 2
  test3 0
 >>> tsimdtm.tsg.test(1,2,3)
  1 T T
  test2 2
  test3 3
 >>> tsimdtm.tsg.test(1,test2=2)
  1 T T
  test2 2
  test3 0
 >>> tsimdtm.tsg.test(1,test3=3)
  1 T T
  test2 0
  test3 3
 >>> tsimdtm.tsg.test(1,test2=2,test3=3)
  1 T T
  test2 2
  test3 3




====================================================================
Prof Garry Willgoose,
Australian Professorial Fellow in Environmental Engineering,
Director, Centre for Climate Impact Management (C2IM),
School of Engineering, The University of Newcastle,
Callaghan, 2308
Australia.

Centre webpage: www.c2im.org.au

Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574  
(Fri PM-Mon)
FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal  
and Telluric)
Env. Engg. Secretary: (International) +61 2 4921 6042

email:  garry.willgoose@newcastle.edu.au;  
g.willgoose@telluricresearch.com
email-for-life: garry.willgoose@alum.mit.edu
personal webpage: www.telluricresearch.com/garry
====================================================================
"Do not go where the path may lead, go instead where there is no path  
and leave a trail"
                           Ralph Waldo Emerson
====================================================================







More information about the Numpy-discussion mailing list