zsh-workers
 help / color / mirror / code / Atom feed
* typeset -F3 SECONDS doesn't work the first time
@ 2012-06-26 16:26 Mikael Magnusson
  2012-06-27 17:50 ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Mikael Magnusson @ 2012-06-26 16:26 UTC (permalink / raw)
  To: zsh workers

I think the code in typeset_single() near
  if (!readonly && !strcmp(pname, "SECONDS"))
is to blame, it does something magic if you're changing the type of
$SECONDS and then returns without checking any other options given to
typeset. I tried removing the return pm; and setting err=0; but that
segfaults.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: typeset -F3 SECONDS doesn't work the first time
  2012-06-26 16:26 typeset -F3 SECONDS doesn't work the first time Mikael Magnusson
@ 2012-06-27 17:50 ` Bart Schaefer
  2012-06-27 17:58   ` Mikael Magnusson
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2012-06-27 17:50 UTC (permalink / raw)
  To: zsh workers

On Jun 26,  6:26pm, Mikael Magnusson wrote:
} Subject: typeset -F3 SECONDS doesn't work the first time
}
} I think the code in typeset_single() near
}   if (!readonly && !strcmp(pname, "SECONDS"))
} is to blame, it does something magic if you're changing the type of
} $SECONDS and then returns without checking any other options given to
} typeset. I tried removing the return pm; and setting err=0; but that
} segfaults.

Try this:

                } else if (!setsecondstype(pm, on, off)) {
                    if (value && !setsparam(pname, ztrdup(value)))
                        return NULL;
                    /* return pm; */
                    usepm = 1;
                    err = 0;
                }

(Not provided as a patch because I don't have time right now to test it.)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: typeset -F3 SECONDS doesn't work the first time
  2012-06-27 17:50 ` Bart Schaefer
@ 2012-06-27 17:58   ` Mikael Magnusson
  2012-06-28 16:54     ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Mikael Magnusson @ 2012-06-27 17:58 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh workers

On 27/06/2012, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Jun 26,  6:26pm, Mikael Magnusson wrote:
> } Subject: typeset -F3 SECONDS doesn't work the first time
> }
> } I think the code in typeset_single() near
> }   if (!readonly && !strcmp(pname, "SECONDS"))
> } is to blame, it does something magic if you're changing the type of
> } $SECONDS and then returns without checking any other options given to
> } typeset. I tried removing the return pm; and setting err=0; but that
> } segfaults.
>
> Try this:
>
>                 } else if (!setsecondstype(pm, on, off)) {
>                     if (value && !setsparam(pname, ztrdup(value)))
>                         return NULL;
>                     /* return pm; */
>                     usepm = 1;
>                     err = 0;
>                 }
>
> (Not provided as a patch because I don't have time right now to test it.)

This appears to work.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: typeset -F3 SECONDS doesn't work the first time
  2012-06-27 17:58   ` Mikael Magnusson
@ 2012-06-28 16:54     ` Bart Schaefer
  2012-06-28 17:04       ` Mikael Magnusson
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2012-06-28 16:54 UTC (permalink / raw)
  To: zsh workers

On Jun 27,  7:58pm, Mikael Magnusson wrote:
}
} >                 } else if (!setsecondstype(pm, on, off)) {
} >                     if (value && !setsparam(pname, ztrdup(value)))
} >                         return NULL;
} >                     /* return pm; */
} >                     usepm = 1;
} >                     err = 0;
} >                 }
} >
} > (Not provided as a patch because I don't have time right now to test it.)
} 
} This appears to work.

I've now tried this with various typeset flags, assigning a value to
SECONDS in the typeset call, etc., and haven't gotten it to break.

Is there any circumstance in which setsparam() [which is a macro around
assignsparam()] could render pm invalid?  It doesn't look like it from
examination of the code, but maybe this should be

                } else if (!setsecondstype(pm, on, off)) {
                    if (value && !(pm = setsparam(pname, ztrdup(value))))
                        return NULL;
                    usepm = 1;
                    err = 0;
                }

(Note the assignment to pm.)

Aside:  Exporting SECONDS doesn't do what you might think.  The value at
the time of the export is enshrined in the environment and never changes,
and is thereafter the starting clock for newly-started shells.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: typeset -F3 SECONDS doesn't work the first time
  2012-06-28 16:54     ` Bart Schaefer
@ 2012-06-28 17:04       ` Mikael Magnusson
  2012-06-28 18:06         ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Mikael Magnusson @ 2012-06-28 17:04 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh workers

On 28/06/2012, Bart Schaefer <schaefer@brasslantern.com> wrote:

> Aside:  Exporting SECONDS doesn't do what you might think.  The value at
> the time of the export is enshrined in the environment and never changes,
> and is thereafter the starting clock for newly-started shells.

Another aside:
% while =sleep 0.1; do zsh -c 'typeset -F4 SECONDS; echo $SECONDS';
done | head -n 13|xargs
0.0020 0.0020 0.0019 0.0019 0.0019 0.0023 0.0019 0.0013 0.0023 0.0020
0.0020 0.0019 0.0019
but:
% while sleep 0.1; do SECONDS=15 zsh -c 'typeset -F4 SECONDS; echo
$SECONDS'; done | head -n 13 | xargs
15.1560 15.2605 15.3650 15.4694 15.5738 15.6783 15.7827 15.8871
15.9916 15.0961 15.2004 15.3047 15.4091


-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: typeset -F3 SECONDS doesn't work the first time
  2012-06-28 17:04       ` Mikael Magnusson
@ 2012-06-28 18:06         ` Bart Schaefer
  2012-06-28 18:11           ` Mikael Magnusson
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2012-06-28 18:06 UTC (permalink / raw)
  To: zsh workers

On Jun 28,  7:04pm, Mikael Magnusson wrote:
}
} Another aside:
} % while =sleep 0.1; do zsh -c 'typeset -F4 SECONDS; echo $SECONDS';
} done | head -n 13|xargs
} 0.0020 0.0020 0.0019 0.0019 0.0019 0.0023 0.0019 0.0013 0.0023 0.0020
} 0.0020 0.0019 0.0019
} but:
} % while sleep 0.1; do SECONDS=15 zsh -c 'typeset -F4 SECONDS; echo
} $SECONDS'; done | head -n 13 | xargs
} 15.1560 15.2605 15.3650 15.4694 15.5738 15.6783 15.7827 15.8871
} 15.9916 15.0961 15.2004 15.3047 15.4091

I'm not sure what you're illustrating.  Did you intend to switch from
/bin/sleep to the builtin sleep in the second example?  Does a value
for sleep of less than 1 second even have any useful effect?

OTOH, it doesn't seem to matter much to the output.  I'm not sure where
the fractional part of $SECONDS is coming from when an initial value is
assigned.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: typeset -F3 SECONDS doesn't work the first time
  2012-06-28 18:06         ` Bart Schaefer
@ 2012-06-28 18:11           ` Mikael Magnusson
  2012-06-28 22:00             ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Mikael Magnusson @ 2012-06-28 18:11 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh workers

On 28/06/2012, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Jun 28,  7:04pm, Mikael Magnusson wrote:
> }
> } Another aside:
> } % while =sleep 0.1; do zsh -c 'typeset -F4 SECONDS; echo $SECONDS';
> } done | head -n 13|xargs
> } 0.0020 0.0020 0.0019 0.0019 0.0019 0.0023 0.0019 0.0013 0.0023 0.0020
> } 0.0020 0.0019 0.0019
> } but:
> } % while sleep 0.1; do SECONDS=15 zsh -c 'typeset -F4 SECONDS; echo
> } $SECONDS'; done | head -n 13 | xargs
> } 15.1560 15.2605 15.3650 15.4694 15.5738 15.6783 15.7827 15.8871
> } 15.9916 15.0961 15.2004 15.3047 15.4091
>
> I'm not sure what you're illustrating.  Did you intend to switch from
> /bin/sleep to the builtin sleep in the second example?  Does a value
> for sleep of less than 1 second even have any useful effect?
>
> OTOH, it doesn't seem to matter much to the output.  I'm not sure where
> the fractional part of $SECONDS is coming from when an initial value is
> assigned.

Right, the fractional part was what I wanted to show. sleep in both
pipelines can sleep for subsecond intervals, but afaik there's no
builtin sleep command in zsh.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: typeset -F3 SECONDS doesn't work the first time
  2012-06-28 18:11           ` Mikael Magnusson
@ 2012-06-28 22:00             ` Bart Schaefer
  0 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2012-06-28 22:00 UTC (permalink / raw)
  To: zsh workers

On Jun 28,  8:11pm, Mikael Magnusson wrote:
}
} Right, the fractional part was what I wanted to show. sleep in both
} pipelines can sleep for subsecond intervals, but afaik there's no
} builtin sleep command in zsh.

Hrm, dunno what I was thinking there except that I couldn't figure
out why you used =sleep in one example but not the other.

In any case init.c:setupvals() loads the shtimer global directly to
initialize SECONDS and then a bit later calls createparamtable() to
copy the environment, which goes through params.c:intsecondssetfn()
which resets shtimer.tv_sec and zeroes shtimer.tv_usec.

So this probably has to do with rounding error when converting shtimer
to double by typecast and division in params.c:floatsecondsgetfn().

Same thing happens without the code snippet from workers/30540 so that's
not involved.


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-06-28 22:01 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-26 16:26 typeset -F3 SECONDS doesn't work the first time Mikael Magnusson
2012-06-27 17:50 ` Bart Schaefer
2012-06-27 17:58   ` Mikael Magnusson
2012-06-28 16:54     ` Bart Schaefer
2012-06-28 17:04       ` Mikael Magnusson
2012-06-28 18:06         ` Bart Schaefer
2012-06-28 18:11           ` Mikael Magnusson
2012-06-28 22:00             ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).