need help with function handle tests

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

need help with function handle tests

John W. Eaton
Administrator
I'm trying to get a better understanding of function handles so I can
properly fix bugs #57439 and #57941 (at least).

Could someone please run the attached testscript.m file in a recent
version of Matlab and report the result to me?

If there are errors, please make a note of what the errors are and
include that with your info, then comment out any lines that cause
trouble so I can see all the things that work as I expect.

Please also correct the code if I have any errors with the handles to
class methods as I'm not sure exactly how those are supposed to work.

To run the tests, unpack the attached tar file, then in Matlab, cd to
the top-level directory (fhdl-tests) that contains the testscript.m file
and execute

   testscript

at the Matlab prompt.

Thanks,

jwe


fhdl-tests.tar.gz (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: need help with function handle tests

siko1056
On 3/11/20 6:31 AM, John W. Eaton wrote:

> I'm trying to get a better understanding of function handles so I can
> properly fix bugs #57439 and #57941 (at least).
>
> Could someone please run the attached testscript.m file in a recent
> version of Matlab and report the result to me?
>
> If there are errors, please make a note of what the errors are and
> include that with your info, then comment out any lines that cause
> trouble so I can see all the things that work as I expect.
>
> Please also correct the code if I have any errors with the handles to
> class methods as I'm not sure exactly how those are supposed to work.
>
> To run the tests, unpack the attached tar file, then in Matlab, cd to
> the top-level directory (fhdl-tests) that contains the testscript.m file
> and execute
>
>   testscript
>
> at the Matlab prompt.
>
> Thanks,
>
> jwe
>

The following output is from R2019a:


>> testscript
ordfun: 1.000000
fh_anon: -0.000000
privfun: 1.000000
subfun: 1.000000
pkgfun: 1.000000
myclass.lcl: 1.000000
Error using myclass.stat_meth
Too many input arguments.

Error in testscript (line 25)
fh_stat_meth (x);


NOTE: @myclass.stat_meth does not have any input parameters.  Same error
in (line 76).

Change testscript (line 25 and 76)  fh_stat_meth ();

Then no further errors.  Great work!  Verbose output attached.

Kai

fhdl-tests.output.txt (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: need help with function handle tests

John W. Eaton
Administrator
On 3/10/20 10:04 PM, Kai Torben Ohlhus wrote:

> The following output is from R2019a:
>
>
>>> testscript
> ordfun: 1.000000
> fh_anon: -0.000000
> privfun: 1.000000
> subfun: 1.000000
> pkgfun: 1.000000
> myclass.lcl: 1.000000
> Error using myclass.stat_meth
> Too many input arguments.
>
> Error in testscript (line 25)
> fh_stat_meth (x);
>
>
> NOTE: @myclass.stat_meth does not have any input parameters.  Same error
> in (line 76).
>
> Change testscript (line 25 and 76)  fh_stat_meth ();
>
> Then no further errors.  Great work!  Verbose output attached.

Thanks.

After running the test script, what happens if you do

   %% Move out of other-dir where nonexistent_fun.m is located
   cd ..
   %% Does fh_missing still point to other-dir/nonexistent_fun?
   fh_missing ();
   functions (fh_missing)

?

I'm sure I'll have more questions and will probably post another version
of the script.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: need help with function handle tests

siko1056


On 3/11/20 12:40 PM, John W. Eaton wrote:

> After running the test script, what happens if you do
>
>   %% Move out of other-dir where nonexistent_fun.m is located
>   cd ..
>   %% Does fh_missing still point to other-dir/nonexistent_fun?
>   fh_missing ();
>   functions (fh_missing)
>
> ?
>
> I'm sure I'll have more questions and will probably post another version
> of the script.
>
> jwe
>

Again R2019a:

>> cd ..
>> fh_missing ();
'nonexistent_fun' is not found in the current folder or on the MATLAB
path, but exists in:
    /home/siko1056/Downloads/fhdl-tests/other-dir

Change the MATLAB current folder or add its folder to the MATLAB path.

>> functions (fh_missing)

ans =

  struct with fields:

    function: 'nonexistent_fun'
        type: 'simple'
        file: ''


Please ask more questions.  Happy to help.

Kai

Reply | Threaded
Open this post in threaded view
|

Re: need help with function handle tests

John W. Eaton
Administrator
On 3/11/20 1:08 AM, Kai Torben Ohlhus wrote:

>
>
> On 3/11/20 12:40 PM, John W. Eaton wrote:
>> After running the test script, what happens if you do
>>
>>    %% Move out of other-dir where nonexistent_fun.m is located
>>    cd ..
>>    %% Does fh_missing still point to other-dir/nonexistent_fun?
>>    fh_missing ();
>>    functions (fh_missing)
>>
>> ?
>>
>> I'm sure I'll have more questions and will probably post another version
>> of the script.
>>
>> jwe
>>
>
> Again R2019a:
>
>>> cd ..
>>> fh_missing ();
> 'nonexistent_fun' is not found in the current folder or on the MATLAB
> path, but exists in:
>      /home/siko1056/Downloads/fhdl-tests/other-dir
>
> Change the MATLAB current folder or add its folder to the MATLAB path.
>
>>> functions (fh_missing)
>
> ans =
>
>    struct with fields:
>
>      function: 'nonexistent_fun'
>          type: 'simple'
>          file: ''
OK, thanks.

I'm attaching an expanded script with more options to see what happens
with different cases (adding main-dir to the path, using a relative name
for it, cd to main-dir instead of adding it to the path, clearing
functions after handles are defined).

This time, cd to the top directory after unpacking and execute the
"doit.m" script.  That will call the "testscript.m" with various
combinations of options and should create a diary file as a transcript
of all the action.  Send that to me and I'll see whether I can make some
sense of it.

jwe

fhdl-tests.tar.gz (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: need help with function handle tests

siko1056
On 3/12/20 2:00 AM, John W. Eaton wrote:

> I'm attaching an expanded script with more options to see what happens
> with different cases (adding main-dir to the path, using a relative name
> for it, cd to main-dir instead of adding it to the path, clearing
> functions after handles are defined).
>
> This time, cd to the top directory after unpacking and execute the
> "doit.m" script.  That will call the "testscript.m" with various
> combinations of options and should create a diary file as a transcript
> of all the action.  Send that to me and I'll see whether I can make some
> sense of it.
>
> jwe

There were two typos in "testscript.m" (line 84 and 168)

- if (clear_functions_after_main_dir)
+ if (clear_function_after_main_dir)

- cd (top_dir)
+ cd (topdir)

After these fixes, I run all the cells.  If an error occurred I ran manually

   clear all
   cd ..

and started in the next cell (to avoid double execution) until
completion.  Output attached.

Kai

fhdl_tests.out.zip (33K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: need help with function handle tests

John W. Eaton
Administrator
On 3/11/20 10:46 PM, Kai Torben Ohlhus wrote:

> There were two typos in "testscript.m" (line 84 and 168)
>
> - if (clear_functions_after_main_dir)
> + if (clear_function_after_main_dir)
>
> - cd (top_dir)
> + cd (topdir)
>  > After these fixes, I run all the cells.  If an error occurred I ran
manually
>
>     clear all
>     cd ..
>
> and started in the next cell (to avoid double execution) until
> completion.  Output attached.

Thanks.  I had some trouble making sense of the output, probably mostly
due to not writing the scripts to be clear enough about what was being
executed.  Also, it doesn't help that there was no attempt at error
recovery.  So I'm attaching another attempt that I hope will produce
clearer output and not fail on any errors when using the function
handles (calls to functions or the handles themselves are now  inside
try/catch blocks).

I would appreciate it if someone could unpack the attached tar file,
execute the run_tests function in the top-level directory, and post the
resulting diary file.

Thanks,

jwe



fhdl-tests.tar.gz (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: need help with function handle tests

rdzman
Hadn’t seen a response yet, so here you go. I had to change all instances of printf() to fprintf() first, but here are the results.

    Ray


On Mar 13, 2020, at 10:23 AM, John W. Eaton <[hidden email]> wrote:

On 3/11/20 10:46 PM, Kai Torben Ohlhus wrote:

There were two typos in "testscript.m" (line 84 and 168)
- if (clear_functions_after_main_dir)
+ if (clear_function_after_main_dir)
- cd (top_dir)
+ cd (topdir)
> After these fixes, I run all the cells.  If an error occurred I ran
manually
   clear all
   cd ..
and started in the next cell (to avoid double execution) until
completion.  Output attached.

Thanks.  I had some trouble making sense of the output, probably mostly due to not writing the scripts to be clear enough about what was being executed.  Also, it doesn't help that there was no attempt at error recovery.  So I'm attaching another attempt that I hope will produce clearer output and not fail on any errors when using the function handles (calls to functions or the handles themselves are now  inside try/catch blocks).

I would appreciate it if someone could unpack the attached tar file, execute the run_tests function in the top-level directory, and post the resulting diary file.

Thanks,

jwe


<fhdl-tests.tar.gz>


diary (476K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: need help with function handle tests

John W. Eaton
Administrator
On 3/16/20 1:00 PM, Ray Daniel Zimmerman wrote:
> Hadn’t seen a response yet, so here you go. I had to change all
> instances of printf() to fprintf() first, but here are the results.

Thanks.  The end of the diary file seems to be cut off.  I don't see the
final "END TESTCASE" marker.  Maybe Matlab is not flushing all output
unless you use "diary off" or exit Matlab?

jwe



Reply | Threaded
Open this post in threaded view
|

Re: need help with function handle tests

rdzman
Is this better?

   Ray



> On Mar 16, 2020, at 11:40 AM, John W. Eaton <[hidden email]> wrote:
>
> On 3/16/20 1:00 PM, Ray Daniel Zimmerman wrote:
>> Hadn’t seen a response yet, so here you go. I had to change all instances of printf() to fprintf() first, but here are the results.
>
> Thanks.  The end of the diary file seems to be cut off.  I don't see the final "END TESTCASE" marker.  Maybe Matlab is not flushing all output unless you use "diary off" or exit Matlab?
>
> jwe
>
>


diary (70K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Help needed with function handles (was: Re: need help with function handle tests)

John W. Eaton
Administrator
On 3/16/20 7:50 PM, Ray Daniel Zimmerman wrote:
> Is this better?

Yes, thanks.

 From what I can tell, ALL function handles have the following fields
(as reported by the calls to 'functions'):

   function: NAME or CODE
       type: anonymous, simple, scopedfunction, nested, classsimple
       file: NAME, may be empty


They may have the additional fields for specific function handle types,
and field values may provide more info about what should happen when
performing searches for functions):

   simple (ordinary functions including package functions and static
class methods)

     no extra fields
     file: '' (for package functions and static class methods)


   anonymous:

            workspace: {[1x1 struct]}
     within_file_path: '' (when is this empty or not?)

   scopedfunction (private, sub, and local functions)

     examples:

     parentage: {'privfun'}
     parentage: {'subfun'  'parentfun'}
     parentage: {'lcl'  'myclass.myclass%fromTopLevel'}

   nested:

    workspace: {[1x1 struct]}

   classsimple (class method):

        class: 'myclass'
         file: ''


It looks like we need to change the implementation of function handles
to store the above info directly and perform lookups somewhat
differently.  Instead of deferring most lookups until a function handle
is used, we need to look for functions in the scope where the handle is
created so we can determine whether there is a nested or scoped
(private, sub or local) function that would take precedence over any
globally visible function.  Then if functions are cleared, we can reload
the necessary functions to recover the function definition.  I believe
that the persistent variable values in the tests I posted show that
function handles don't grab a copy (or reference count that would
prevent clearing) of the functions they refer to and that the functions
are reloaded if they are cleared.  And this reloading may happen even if
the load path changes so that the function referenced by the handle is
no longer visible in the load path.

Please correct me if any of the above doesn't look right.

If someone has some insight about how the rules for function handles are
really simple, please let me know, because at the moment, it seems that
the rules are somewhat complex and there are a lot of cases to consider.

jwe