changeset 25762:97ea2329d5d8

Use nested functions to prevent leaking globals in BIST tests. * cellfun.cc (Fcellfun, Farrayfun): Use nested functions with access to enclosing functions variables, rather than globals, to pass pass information out of function and in to BIST test. * variables.cc (Fisglobal): Clear global variable created during BIST test.
author Rik <rik@octave.org>
date Thu, 09 Aug 2018 14:16:14 -0700
parents 7c0dabd59d18
children 8dc8edbffa17
files libinterp/corefcn/cellfun.cc libinterp/corefcn/variables.cc
diffstat 2 files changed, 37 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/cellfun.cc	Thu Aug 09 13:46:27 2018 -0700
+++ b/libinterp/corefcn/cellfun.cc	Thu Aug 09 14:16:14 2018 -0700
@@ -688,26 +688,34 @@
 /*
 
 %!function r = __f11 (x)
-%!  global __cellfun_test_num_outputs__;
-%!  __cellfun_test_num_outputs__ = nargout;
 %!  r = x;
 %!endfunction
 
 %!function __f01 (x)
-%!  global __cellfun_test_num_outputs__;
-%!  __cellfun_test_num_outputs__ = nargout;
+%!  ## Empty function
 %!endfunction
 
 %!test
-%! global __cellfun_test_num_outputs__;
-%! cellfun (@__f11, {1});
+%! __cellfun_test_num_outputs__ = -1;
+%!
+%! function r = __subf11 (x)
+%!   __cellfun_test_num_outputs__ = nargout;
+%!   r = x;
+%! endfunction
+%!
+%! cellfun ("__subf11", {1});
 %! assert (__cellfun_test_num_outputs__, 0);
-%! x = cellfun (@__f11, {1});
+%!
+%! __cellfun_test_num_outputs__ = -1;
+%! x = cellfun ("__subf11", {1});
 %! assert (__cellfun_test_num_outputs__, 1);
 
 %!test
-%! global __cellfun_test_num_outputs__;
-%! cellfun (@__f01, {1});
+%! __cellfun_test_num_outputs__ = -1;
+%! function __subf01 (x)
+%!   __cellfun_test_num_outputs__ = nargout;
+%! endfunction
+%! cellfun ("__subf01", {1});
 %! assert (__cellfun_test_num_outputs__, 0);
 
 %!error x = cellfun (@__f01, {1, 2})
@@ -1389,26 +1397,36 @@
 
 /*
 %!function r = __f11 (x)
-%!  global __arrayfun_test_num_outputs__;
-%!  __arrayfun_test_num_outputs__ = nargout;
 %!  r = x;
 %!endfunction
 
 %!function __f01 (x)
-%!  global __arrayfun_test_num_outputs__;
-%!  __arrayfun_test_num_outputs__ = nargout;
+%!  ## Empty function
 %!endfunction
 
 %!test
-%! global __arrayfun_test_num_outputs__;
-%! arrayfun (@__f11, {1});
+%! __arrayfun_test_num_outputs__ = -1;
+%!
+%! function r = __subf11 (x)
+%!   __arrayfun_test_num_outputs__ = nargout;
+%!   r = x;
+%! endfunction
+%!
+%! arrayfun ("__subf11", {1});
 %! assert (__arrayfun_test_num_outputs__, 0);
-%! x = arrayfun (@__f11, {1});
+%!
+%! __arrayfun_test_num_outputs__ = -1;
+%! x = arrayfun ("__subf11", {1});
 %! assert (__arrayfun_test_num_outputs__, 1);
 
 %!test
-%! global __arrayfun_test_num_outputs__;
-%! arrayfun (@__f01, {1});
+%! __arrayfun_test_num_outputs__ = -1;
+%!
+%! function __subf01 (x)
+%!   __arrayfun_test_num_outputs__ = nargout;
+%! endfunction
+%!
+%! arrayfun ("__subf01", {1});
 %! assert (__arrayfun_test_num_outputs__, 0);
 
 %!error x = arrayfun (@__f01, [1, 2])
--- a/libinterp/corefcn/variables.cc	Thu Aug 09 13:46:27 2018 -0700
+++ b/libinterp/corefcn/variables.cc	Thu Aug 09 14:16:14 2018 -0700
@@ -201,6 +201,7 @@
 %!test
 %! global x;
 %! assert (isglobal ("x"), true);
+%! clear -global x;  # cleanup after test
 
 %!error isglobal ()
 %!error isglobal ("a", "b")