On Thu, 25 May 2017, Sven Schreiber wrote:
Am 24.05.2017 um 16:36 schrieb Allin Cottrell:
> On Wed, 24 May 2017, Sven Schreiber wrote:
>
>> I'm seeing a difference in the output between what (I think) is the same
>> code, depending on whether it is called as a function package or whether
>> it is run as unpackaged scripts. With the package (run with gretlcli.exe)
>> from the CLI I'm getting warnings:
>>
>> <gretl-output>
>> In regard to function VARboot (package johansensmall):
>> Warnung: erzeugte nichtendliche Werte
>> </gretl-output>
>>
>> (meaning "created non-finite values")
>>
>> And when I run the some code with gretlcli but not in a packaged form,
>> those warnings are absent. I have inserted a "set warnings off" into
that
>> code where I think it could be necessary, and I would like to mute those
>> warnings also in the package.
>>
>> So my question is: What could explain this different behavior? Is it
>> intended? How to switch off the warnings inside a package?
>
> I created a trivial gfn that produces a matrix with a NaN in it, then ran a
> script that calls it, as follows:
>
> include ./warntest.gfn
> nulldata 10
> set verbose off
> matrix M = myfun()
> print M
>
> In this case I saw the message
>
> In regard to function myfun (package warntest):
> Warning: generated non-finite values
>
> However, if I inserted "set warnings off" in the caller that suppressed the
> message. So I'm not sure what's happening in your case. Could you perhaps
> send me a caller for johansensmall.gfn that provokes a warning?
>
(BTW, I'm using a private new version johansensmall.gfn, the earlier one
doesn't have the set warnings off thing.)
I think I see the same thing as you when I let the caller do set warnings
off. But first I'd like to handle this in the package without bothering the
caller. And secondly I don't understand why it makes a difference when the
code is packaged.
A few comments on this. (I know that not all of the following will
be news to Sven, but this is partly just for the record.)
Not so long ago, we enabled the command "set verbose off", which
combines the two commands (still OK individually)
set echo off
set messages off
One point that transpires from the above is that "set verbose off"
does _not_ turn off warnings. That seems to me defensible: "echo"
(printing the input statements before the output) and "messages"
(confirmatory output such as "Generated matrix m") can fairly be
called verbose. But warnings are not just verbosity: they're telling
the user that something has gone wrong, although it falls short of
an outright error condition.
Nonetheless, we support the command "set warnings off", for the
benefit of users who know very well what they're doing and want to
cut down on what they regard as unwanted noise. But this command is
"top down". If you say "set warnings off" in a top-level
("main")
script, that turns off warnings in any and all functions called by
that script. But if you issue that same statement inside a function
it applies _only_ inside the function.
Consider the following hansl fragment:
<hansl>
set verbose off
function matrix myfun (void)
# set warnings off
matrix m = {1, 0/0}
return m
end function
nulldata 10
# set warnings off
matrix M = myfun()
</hansl>
As it stands, this produces:
<output>
? run warnfunc.inp
/home/allin/stats/extra-gfn/mypkg/warnfunc.inp
In regard to function myfun:
Warning: generated non-finite values
Warning: generated non-finite values
</output>
If you uncomment "set warnings off" inside myfun(), the output
becomes:
<output>
? run warnfunc.inp
/home/allin/stats/extra-gfn/mypkg/warnfunc.inp
Warning: generated non-finite values
</output>
Note that the first warning - the one issued inside myfun() -- has
been suppressed, but we still get a warning when a matrix containing
a "NaN" value is returned to the main script: the suppression of
warnings applies only within the function. But if we uncomment the
"warnings off" statement in the caller, then the warnings are fully
suppressed.
This may be debatable but it's by design. The idea is that if a
function (a built-in function, a function in a gfn package or
whatever) returns to the ultimate caller a matrix containing NaNs,
the user should be given fair warning. Since NaNs propagate on
matrix operations, failure to indicate their origin could quickly
become quite confusing.
If you want to say, in effect, "Look I know that the return value of
this function can sometimes contain NaNs, but I can handle that, I
don't want gretl to make a fuss", then OK, suppress warnings in the
caller script.
But if you want to say, "The return value of my function may
sometimes contain NaNs but please don't bother the user about that:
look, I'll put 'set warnings off' in my function code", then that
won't work, and it's not supposed to work.
So far as I can tell, there's no difference in this respect between
function code packaged in a gfn file and function code in a regular
script: the same rules apply. One difference is that in non-packaged
code you could do:
<hansl>
set warnings off
function matrix myfun (void)
matrix m = {1, 0/0}
return m
end function
nulldata 10
matrix M = myfun()
</hansl>
In the above, warrnings are set "off" globally (prior to both
function code and main script). There's no way to do that from
inside a function package, and IMO there shouldn't be.
Allin