bugGNU Octave - Bugs: bug #55599, print_usage() within subfunction...

 
 

bug #55599: print_usage() within subfunction results error

Submitter:  None
Submitted:  Tue 29 Jan 2019 08:00:17 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Unexpected Error or Warning
Status:  Fixed Assigned to:  None
Originator Name:  Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * dev
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Thu 07 Feb 2019 06:07:09 PM UTC, comment #8: 

I agree with the relative priority, I was just wondering whether this would be a somewhat useful feature to have.

John W. Eaton <jwe>
Group administrator
Thu 07 Feb 2019 05:46:05 PM UTC, comment #7: 

Since print_usage is meant to help users call functions correctly, it's not that urgent to make it work for subfunctions and nested functions. That usage would be more useful for the programmer, not the end user, since they can't call the function directly anyway.

A more worthwhile goal would be to make print_usage work on methods defined in a classdef class definition.

Mike Miller <mtmiller>
Group Member
Thu 07 Feb 2019 05:24:11 PM UTC, comment #6: 

I could see making print_usage automatically work its way up to the parent function.  But, there's probably other things I'd work on first.  In this case, it doesn't seem that hard to provide the name of the parent function when writing the subfunctions and nested functions.

If you want a generic m-file solution you can write


    print_usage (mfilename ());


rather than, say,



    print_usage ("gradient");



Rik <rik5>
Group administrator
Thu 07 Feb 2019 04:57:06 PM UTC, comment #5: 

Maybe it would make sense for print_usage to be a built-in function so that it could easily find the parent calling function from the call stack.  Or, to provide sufficient access to the call stack and/or info about the currently executing function (not just in the form currently provided by dbstack) to allow print_usage to remain a .m file but to find the ultimate parent function.  Then we could continue to write "print_usage()" in sub or nested functions and automatically find the doc string of the parent.

John W. Eaton <jwe>
Group administrator
Thu 07 Feb 2019 04:45:54 PM UTC, comment #4: 

I applied the other parts of your patch to the development branch here (https://hg.savannah.gnu.org/hgweb/octave/rev/f2bb4f2093b9).  Thanks for providing the code to fix the problem, rather than just the diagnosis.

Marking as fixed and closing report.

Rik <rik5>
Group administrator
Thu 07 Feb 2019 02:51:22 AM UTC, comment #3: 

I fixed the problem in qmr.m here (https://hg.savannah.gnu.org/hgweb/octave/rev/736f26d34f79).

Rik <rik5>
Group administrator
Tue 29 Jan 2019 09:06:10 AM UTC, comment #2: 

Besides, another typo bug found in "qmr.m"

octave:1>  close all
octave:2>  qmr
error: print: no figure to print
error: called from
    print at line 347 column 5
    qmr at line 265 column 5


due to missing the underscore between "print" and "usage".

Here is a patch:

--- a/scripts/sparse/qmr.m
+++ b/scripts/sparse/qmr.m
@@ -262,7 +262,7 @@
       printf ("to a solution with relative residual %e\n", res1);
     endif
   else
-    print usage();
+    print_usage ();
   endif

 endfunction


Anonymous
Tue 29 Jan 2019 08:10:27 AM UTC, comment #1: 

By the way, in version 4.4.1, "residue.m" also has a "print_usage()" within its subfunction "rresidue".
But it has been removed in changeset https://hg.savannah.gnu.org/hgweb/octave/rev/2b9a30925a9c for "Don't do input validation within subfunction"

Anonymous
Tue 29 Jan 2019 08:00:17 AM UTC, original submission:  

Here is how to produce this error:

octave:1>  gradient (ones (4,5,3), 1, 1)
error: 'fullname' undefined near line 55 column 33
error: called from
    print_usage at line 55 column 16
    gradient>matrix_gradient at line 102 column 5
    gradient at line 74 column 35


and here is another simpler example:
first, create an Octave function file named "tmp.m":

$  cat tmp.m
function tmp
##        print_usage()
        tmp1
end

function tmp1
        print_usage()
end


then, invoke this function "tmp" in Octave:

octave:2>  tmp
error: 'fullname' undefined near line 55 column 33
error: called from
    print_usage at line 55 column 16
    tmp>tmp1 at line 7 column 2
    tmp at line 3 column 2



The reason is that there is an useless variable named "fullpath",
and the variable "fullname" may be undefined if the line 41 in "print_usage.m" has a false value:

$  grep fullpath -w -C3 -n /usr/share/octave/4.4.1/m/help/print_usage.m
37-    else
38-      error ("Octave:invalid-context", "print_usage: invalid function\n");
39-    endif
40:    fullpath = evalin ("caller", 'mfilename ("fullpath")');
41:    if (strcmp (fullpath(end-length(name)+1:end), name))
42:      fullname = [fullpath ".m"];
43-    endif
44-  elseif (! ischar (name))
45-    error ("Octave:invalid-input-arg",


Here is a possible patch for this bug:

--- a/scripts/help/print_usage.m
+++ b/scripts/help/print_usage.m
@@ -37,9 +37,9 @@
     else
       error ("Octave:invalid-context", "print_usage: invalid function\n");
     endif
-    fullpath = evalin ("caller", 'mfilename ("fullpath")');
-    if (strcmp (fullpath(end-length(name)+1:end), name))
-      fullname = [fullpath ".m"];
+    fullname = evalin ("caller", 'mfilename ("fullpath")');
+    if (strcmp (fullname(end-length(name)+1:end), name))
+      fullname = [fullname ".m"];
     endif
   elseif (! ischar (name))
     error ("Octave:invalid-input-arg",



After applying the above patch, the "undefined variable" error disappears, but it would print some less useful information:

octave:4>  gradient (ones (4,5,3), 1, 1)
error: print_usage: 'matrix_gradient' not found
octave:4>  tmp
error: print_usage: 'tmp1' not found


This is because these subfunctions do not have their own docstrings.
Therefore, it seems that in this case it would better to invoke "print_usage" with the corresponding "main function" name, just like "plotmatrix.m":

$  grep print_usage -w -C2 -n /usr/share/octave/4.4.1/m/plot/draw/plotmatrix.m
73-
74-  if (nargin > 3 || nargin < 1)
75:    print_usage ();
76-  endif
77-
--
147-        break;
148-      else
149:        print_usage ("plotmatrix");
150-      endif
151-    endif
--
160-    Y = varargin{2};
161-  else
162:    print_usage ("plotmatrix");
163-  endif
164-


Below is some more possible patch:

diff -ur a/scripts/general/gradient.m b/scripts/general/gradient.m
--- a/scripts/general/gradient.m
+++ b/scripts/general/gradient.m
@@ -99,7 +99,7 @@
   endif

   if (nargin > 2 && nargin != nd + 1)
-    print_usage ();
+    print_usage ("gradient");
   endif

   ## cell d stores a spacing vector for each dimension
diff -ur a/scripts/plot/draw/reducepatch.m b/scripts/plot/draw/reducepatch.m
--- a/scripts/plot/draw/reducepatch.m
+++ b/scripts/plot/draw/reducepatch.m
@@ -197,7 +197,7 @@
              "FACES, second argument must be a matrix containing VERTICES"]);
     endif
   else
-    print_usage ();
+    print_usage ("reducepatch");
   endif

   ## get reduction_factor
diff -ur a/scripts/statistics/quantile.m b/scripts/statistics/quantile.m
--- a/scripts/statistics/quantile.m
+++ b/scripts/statistics/quantile.m
@@ -406,7 +406,7 @@
 function inv = __quantile__ (x, p, method = 5)

   if (nargin < 2 || nargin > 3)
-    print_usage ();
+    print_usage ("quantile");
   endif

   if (isinteger (x) || islogical (x))


Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-02-07 rik5 StatusConfirmed Fixed
        Open/ClosedOpen Closed
        Release4.4.1 dev
    2019-02-07 rik5 StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code