Skip to content

Commit

Permalink
Make (Un)HideGlobalVariables obsolete
Browse files Browse the repository at this point in the history
These functions were never documented, and so far in all cases where they
appear, there seem to be better, more robust alternatives, such as reading and
evaluating external code inside a function context with suitable locals set,
or by adapting the (interface to) the external code.
  • Loading branch information
fingolfin committed Jan 18, 2019
1 parent eb31fc7 commit 2adb187
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 122 deletions.
39 changes: 0 additions & 39 deletions lib/global.gd
Original file line number Diff line number Diff line change
Expand Up @@ -274,42 +274,3 @@ DeclareGlobalFunction("BindConstant");
## </ManSection>
##
DeclareGlobalFunction("TemporaryGlobalVarName");


#############################################################################
##
#F HideGlobalVariables(<str1>[,<str2>,...]))
##
## <ManSection>
## <Func Name="HideGlobalVariables" Arg='str1[,str2,...]'/>
##
## <Description>
## temporarily makes global variables <Q>undefined</Q>. The arguments to
## <C>HideGlobalVariables</C> are strings. If there is a global variable defined
## whose identifier is equal to one of the strings it will be <Q>hidden</Q>.
## This means that identifier and value will be safely stored on a stack
## and the variable will be undefined afterwards. A call to
## <C>UnhideGlobalVariables</C> will restore the old values.
## The main purpose of hiding variables will be for the temporary creation
## of global variables for reading in data created by other programs.
## </Description>
## </ManSection>
##
DeclareGlobalFunction("HideGlobalVariables");


#############################################################################
##
#F UnhideGlobalVariables(<str1>[,<str2>,...])
#F UnhideGlobalVariables()
##
## <ManSection>
## <Func Name="UnhideGlobalVariables" Arg='str1[,str2,...]'/>
## <Func Name="UnhideGlobalVariables" Arg=''/>
##
## <Description>
## The second version unhides all variables that are still hidden.
## </Description>
## </ManSection>
##
DeclareGlobalFunction("UnhideGlobalVariables");
83 changes: 0 additions & 83 deletions lib/global.gi
Original file line number Diff line number Diff line change
Expand Up @@ -308,86 +308,3 @@ InstallGlobalFunction( TemporaryGlobalVarName,

return gvar;
end );


if IsHPCGAP then
BindThreadLocal("HIDDEN_GVARS",[]);
else
HIDDEN_GVARS:=[];
fi;

InstallGlobalFunction(HideGlobalVariables,function(arg)
local p,i;
p:=Length(HIDDEN_GVARS);
for i in arg do
if IsString(i) then
p:=p+1;
HIDDEN_GVARS[p]:=i;
p:=p+2;
if ISBOUND_GLOBAL(i) then
# variable is assigned
HIDDEN_GVARS[p-1]:=VALUE_GLOBAL(i);
if IS_READ_ONLY_GLOBAL(i) then
HIDDEN_GVARS[p]:=true;
MAKE_READ_WRITE_GLOBAL(i);
else
HIDDEN_GVARS[p]:=false;
fi;
else
HIDDEN_GVARS[p-1]:=fail; # needs to be assigned
HIDDEN_GVARS[p]:=fail;
fi;
# temporarily remove the variable
UNBIND_GLOBAL(i);
else
Error("HideGlobalVariables requires the names as strings");
fi;
od;
end);

InstallGlobalFunction(UnhideGlobalVariables,function(arg)
local p,str,all,l,which;
all:=Length(arg)=0; # doe we want to unhide all?
which:=arg;
l:=Length(HIDDEN_GVARS);
p:=l-2;
while p>0 do
str:=HIDDEN_GVARS[p];
# do we want to unhide the variable?
if all or str in which then
# remove the value
if ISBOUND_GLOBAL(str) then
if IS_READ_ONLY_GLOBAL(str) then
MAKE_READ_WRITE_GLOBAL(str);
fi;
UNBIND_GLOBAL(str);
fi;

if HIDDEN_GVARS[p+2]<>fail then
#reassign a value
ASS_GVAR(str,HIDDEN_GVARS[p+1]);
if HIDDEN_GVARS[p+2]=true then
MAKE_READ_ONLY_GLOBAL(str);
fi;
fi;

# remove the corresponding "HIDDEN_GVARS" entry
if not all then
if p+2<l then
# move
HIDDEN_GVARS{[p..l-3]}:=HIDDEN_GVARS{[p+3..l]};
fi;
# remove
Unbind(HIDDEN_GVARS[l-2]);
Unbind(HIDDEN_GVARS[l-1]);
Unbind(HIDDEN_GVARS[l]);
l:=l-3;
which:=Filtered(which,i->i<>str);
fi;
fi;
p:=p-3;
od;
if all then
HIDDEN_GVARS:=[];
fi;
end);
45 changes: 45 additions & 0 deletions lib/obsolete.gd
Original file line number Diff line number Diff line change
Expand Up @@ -690,3 +690,48 @@ DeclareObsoleteSynonym( "RecFields", "RecNames" );
if GAPInfo.CommandLineOptions.D then InfoRead1 := Print; fi;
if not IsBound(InfoRead1) then InfoRead1 := Ignore; fi;
if not IsBound(InfoRead2) then InfoRead2 := Ignore; fi;


#############################################################################
##
#F HideGlobalVariables(<str1>[,<str2>,...]))
##
## <ManSection>
## <Func Name="HideGlobalVariables" Arg='str1[,str2,...]'/>
##
## <Description>
## temporarily makes global variables <Q>undefined</Q>. The arguments to
## <C>HideGlobalVariables</C> are strings. If there is a global variable defined
## whose identifier is equal to one of the strings it will be <Q>hidden</Q>.
## This means that identifier and value will be safely stored on a stack
## and the variable will be undefined afterwards. A call to
## <C>UnhideGlobalVariables</C> will restore the old values.
## The main purpose of hiding variables will be for the temporary creation
## of global variables for reading in data created by other programs.
## </Description>
## </ManSection>
##
## This function was never documented.
##
## Still used in anupq, nq, resclasses, rcwa (01/2019)
DeclareGlobalFunction("HideGlobalVariables");


#############################################################################
##
#F UnhideGlobalVariables(<str1>[,<str2>,...])
#F UnhideGlobalVariables()
##
## <ManSection>
## <Func Name="UnhideGlobalVariables" Arg='str1[,str2,...]'/>
## <Func Name="UnhideGlobalVariables" Arg=''/>
##
## <Description>
## The second version unhides all variables that are still hidden.
## </Description>
## </ManSection>
##
## This function was never documented.
##
## Still used in anupq, nq, resclasses, rcwa (01/2019)
DeclareGlobalFunction("UnhideGlobalVariables");
91 changes: 91 additions & 0 deletions lib/obsolete.gi
Original file line number Diff line number Diff line change
Expand Up @@ -958,3 +958,94 @@ BIND_GLOBAL( "SetFeatureObj", function ( obj, filter, val )
ResetFilterObj( obj, filter );
fi;
end );


if IsHPCGAP then
BindThreadLocal("HIDDEN_GVARS",[]);
else
HIDDEN_GVARS:=[];
fi;

InstallGlobalFunction(HideGlobalVariables,function(arg)
local p,i;

InfoObsolete(1, "This usage of `HideGlobalVariables` is no longer",
"supported and will be removed eventually." );

p:=Length(HIDDEN_GVARS);
for i in arg do
if IsString(i) then
p:=p+1;
HIDDEN_GVARS[p]:=i;
p:=p+2;
if ISBOUND_GLOBAL(i) then
# variable is assigned
HIDDEN_GVARS[p-1]:=VALUE_GLOBAL(i);
if IS_READ_ONLY_GLOBAL(i) then
HIDDEN_GVARS[p]:=true;
MAKE_READ_WRITE_GLOBAL(i);
else
HIDDEN_GVARS[p]:=false;
fi;
else
HIDDEN_GVARS[p-1]:=fail; # needs to be assigned
HIDDEN_GVARS[p]:=fail;
fi;
# temporarily remove the variable
UNBIND_GLOBAL(i);
else
Error("HideGlobalVariables requires the names as strings");
fi;
od;
end);

InstallGlobalFunction(UnhideGlobalVariables,function(arg)
local p,str,all,l,which;

InfoObsolete(1, "This usage of `UnhideGlobalVariables` is no longer",
"supported and will be removed eventually." );

all:=Length(arg)=0; # doe we want to unhide all?
which:=arg;
l:=Length(HIDDEN_GVARS);
p:=l-2;
while p>0 do
str:=HIDDEN_GVARS[p];
# do we want to unhide the variable?
if all or str in which then
# remove the value
if ISBOUND_GLOBAL(str) then
if IS_READ_ONLY_GLOBAL(str) then
MAKE_READ_WRITE_GLOBAL(str);
fi;
UNBIND_GLOBAL(str);
fi;

if HIDDEN_GVARS[p+2]<>fail then
#reassign a value
ASS_GVAR(str,HIDDEN_GVARS[p+1]);
if HIDDEN_GVARS[p+2]=true then
MAKE_READ_ONLY_GLOBAL(str);
fi;
fi;

# remove the corresponding "HIDDEN_GVARS" entry
if not all then
if p+2<l then
# move
HIDDEN_GVARS{[p..l-3]}:=HIDDEN_GVARS{[p+3..l]};
fi;
# remove
Unbind(HIDDEN_GVARS[l-2]);
Unbind(HIDDEN_GVARS[l-1]);
Unbind(HIDDEN_GVARS[l]);
l:=l-3;
which:=Filtered(which,i->i<>str);
fi;
fi;
p:=p-3;
od;
if all then
HIDDEN_GVARS:=[];
fi;
end);

0 comments on commit 2adb187

Please sign in to comment.