bugGNU Octave - Bugs: bug #63753, wrong function called after...

 
 

bug #63753: wrong function called after changing directory

Submitter:  None
Submitted:  Fri 03 Feb 2023 09:26:39 PM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Need Info Assigned to:  None
Originator Name:  mark Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * 7.3.0
Operating System:  * Microsoft Windows Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 07 Feb 2023 05:04:38 PM UTC, comment #3: 

I still don't see a use case for this.  If the functions do different things then why would they have the same name?  Generally directories are used to organize related functions and then addpath() or rmpath() are used to add a group of functions at a time.  In this case, if you want to access functions in both directories 'a' and 'b' you would add both to your path with


addpath ('a:b')


BUT, you wouldn't want to have two functions with the same name because Octave would run only one of them.  Because 'a' was listed first it would be 'a/myfcn.m' which would be run.

Imagine a use case where you want to have trig functions calculated for either inputs in radians or inputs in degrees.  In this case, you could make a directory 'trigradian' and a directory 'trigdegree' and have a file sin.m in each of them.  But, you wouldn't add both of these to your path.  Instead, you would choose which convention you were going to use and then use 'addpath' on just one of the directories.

Incidentally, instead of "addpath ('.', 0)" it would be clearer to use "rehash" for what you are trying to do in comment #2.

It seems you have found a solution for your needs so I will close this report.

Rik <rik5>
Group administrator
Tue 07 Feb 2023 01:05:49 PM UTC, comment #2: 

In case it wasn't clear, this was meant to be a minimal demonstration of a problem with locating/calling functions after a "change directory", not a specific use case. The behavior is the same if you change the names to "myfcn".

And thank you, your "other coding strategy" comment helped me think of a workaround: prepending the path with '.' (even though the path should always begin with '.') produces the desired behavior (hopefully that's helpful to anyone else with this issue).


a/myfcn.m:
function test()
  printf('correct version\n');
end

b/myfcn.m:
function myfcn()
  printf('incorrect version, changing directory\n');
  cd('../a');
  addpath ('.', 0);
  myfcn();
  cd('../b');
end


Anonymous
Sun 05 Feb 2023 01:35:10 AM UTC, comment #1: 

For starters, test.m is also a function in core Octave so there are three versions of a function with the same name.  It's bad practice to shadow a core function with your own so I would at least re-code to use something generic like "myfcn.m".

The real question is what is the use case that can't be solved by by some other coding strategy?

Rik <rik5>
Group administrator
Fri 03 Feb 2023 09:26:39 PM UTC, original submission:  

If there are two functions with the same name in different directories, changing the directory does not result in the correct function being called. A simple example:


a/test.m:
function test()
  printf('correct version\n');
end

b/test.m:
function test()
  printf('incorrect version, changing directory\n');
  cd('../a');
  test();
  cd('../b');
end


Starting in directory 'b' and running 'test()', the expected output (and output in version 6.1) is:

>> test()
incorrect version, changing directory
correct version


The output in version 7.3 is:

>> test()
incorrect version, changing directory
incorrect version, changing directory
incorrect version, changing directory
...
incorrect version, changing directory
error: max_recursion_depth exceeded
error: called from
    test
    test at line 3 column 3


Adding 'keyboard' to debug the function prevents the error:


b/test.m:
function test()
  printf('incorrect version, changing directory\n');
  cd('../a');
  keyboard;
  test();
  cd('../b');
end

>> test();
incorrect version, changing directory
stopped in test at line 4 [\b\test.m]
4:   keyboard;
keyboard> return;
correct version


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 rik5 (Posted a comment)
  •  

    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 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-02-07 rik5 Open/ClosedOpen Closed
    2023-02-05 rik5 StatusNone Need Info

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code