* [BUG] functions can't create global readonly variables
@ 2016-01-02 3:27 Martijn Dekker
2016-01-02 4:27 ` Bart Schaefer
0 siblings, 1 reply; 7+ messages in thread
From: Martijn Dekker @ 2016-01-02 3:27 UTC (permalink / raw)
To: zsh-workers
It is apparently impossible to create global readonly variables from
within a function.
If a variable is created with a command like
readonly var=value
then the entire variable gets a function-local scope, as if 'local' were
used.
If a global variable is set first, then set to readonly with 'readonly'
or 'typeset -r', then the read-only attribute only lasts within the
function, and the global variable remains read/write.
I don't know if this is intended behaviour for native zsh mode; the
'zshbuiltins' man page does not mention anything about this. But it is
certainly a bug for POSIX/'emulate sh' mode.
Steps to reproduce:
$ fn() { readonly var=foo; var=bar; }
$ fn
fn: read-only variable: var
$ echo $var
$ var=writtento
$ echo $var
writtento
$ fn() { var=foo; readonly var; var=bar; }
$ fn
fn: read-only variable: var
$ echo $var
foo
$ var=writtento
$ echo $var
writtento
Confirmed in zsh 4.1.1 through current.
Thanks,
- Martijn
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [BUG] functions can't create global readonly variables
2016-01-02 3:27 [BUG] functions can't create global readonly variables Martijn Dekker
@ 2016-01-02 4:27 ` Bart Schaefer
2016-01-02 18:18 ` Peter Stephenson
2016-01-02 19:39 ` Martijn Dekker
0 siblings, 2 replies; 7+ messages in thread
From: Bart Schaefer @ 2016-01-02 4:27 UTC (permalink / raw)
To: zsh-workers
On Jan 2, 4:27am, Martijn Dekker wrote:
}
} It is apparently impossible to create global readonly variables from
} within a function.
typeset -gr var=value
or
readonly -g var=value
} I don't know if this is intended behaviour for native zsh mode; the
} 'zshbuiltins' man page does not mention anything about this.
Of course it does:
readonly
Same as typeset -r.
typeset ...
Set or display attributes and values for shell parameters.
A parameter is created for each NAME that does not already refer
to one. When inside a function, a new parameter is created for
every NAME (even those that already exist), and is unset again
when the function completes.
} But it is certainly a bug for POSIX/'emulate sh' mode.
I think it's undefined behavior for POSIX/'emulate sh'. Certainly bash
agrees with us:
$ foo() { typeset -r bar; typeset -p bar; }
$ foo
declare -r bar
$ typeset -p bar
bash: typeset: bar: not found
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [BUG] functions can't create global readonly variables
2016-01-02 4:27 ` Bart Schaefer
@ 2016-01-02 18:18 ` Peter Stephenson
2016-01-02 20:36 ` Bart Schaefer
2016-01-02 19:39 ` Martijn Dekker
1 sibling, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2016-01-02 18:18 UTC (permalink / raw)
To: zsh-workers
On Fri, 1 Jan 2016 20:27:10 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> I think it's undefined behavior for POSIX/'emulate sh'. Certainly bash
> agrees with us:
>
> $ foo() { typeset -r bar; typeset -p bar; }
> $ foo
> declare -r bar
> $ typeset -p bar
> bash: typeset: bar: not found
Bash is treating "typeset -r bar" differently from "readonly bar".
"readonly" affects any existing parameter or creates its own.
I think readonly is equivalent to "typeset -rg", so it stomps
on any existing global, too (unless it was already readonly).
We could make this optional behaviour for compatibility, possibly with
POSIX_BUILTINS which is a real ragbag of POSIX funnies.
pws
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [BUG] functions can't create global readonly variables
2016-01-02 4:27 ` Bart Schaefer
2016-01-02 18:18 ` Peter Stephenson
@ 2016-01-02 19:39 ` Martijn Dekker
1 sibling, 0 replies; 7+ messages in thread
From: Martijn Dekker @ 2016-01-02 19:39 UTC (permalink / raw)
To: zsh-workers
Bart Schaefer schreef op 02-01-16 om 05:27:
> On Jan 2, 4:27am, Martijn Dekker wrote:
> } I don't know if this is intended behaviour for native zsh mode; the
> } 'zshbuiltins' man page does not mention anything about this.
>
> Of course it does:
I blame insufficient caffeination at the time of writing.
> } But it is certainly a bug for POSIX/'emulate sh' mode.
>
> I think it's undefined behavior for POSIX/'emulate sh'. Certainly bash
> agrees with us:
>
> $ foo() { typeset -r bar; typeset -p bar; }
> $ foo
> declare -r bar
> $ typeset -p bar
> bash: typeset: bar: not found
But 'typeset' is not POSIX. When using the POSIX 'readonly' special
builtin, bash acts like other POSIX shells and unlike zsh:
$ foo() { readonly bar=baz; typeset -p bar; }
$ foo
declare -r bar="baz"
$ typeset -p bar
declare -r bar="baz"
Thanks,
- M.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [BUG] functions can't create global readonly variables
2016-01-02 18:18 ` Peter Stephenson
@ 2016-01-02 20:36 ` Bart Schaefer
2016-01-03 19:00 ` Peter Stephenson
0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2016-01-02 20:36 UTC (permalink / raw)
To: zsh-workers
On Jan 2, 6:18pm, Peter Stephenson wrote:
}
} I think readonly is equivalent to "typeset -rg", so it stomps
} on any existing global, too (unless it was already readonly).
}
} We could make this optional behaviour for compatibility, possibly with
} POSIX_BUILTINS which is a real ragbag of POSIX funnies.
Yes, this seems like a job for POSIX_BUILTINS.
I would note that even with -g there is no way to create an actual global
variable in the event that some intervening function has declared a name
to be local.
BIN_EXPORT was defined but never used, so ...
What additional test case should we have?
diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index dc0b947..fb630a7 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -1465,7 +1465,10 @@ cancels both tt(-p) and tt(-u).
The tt(-c) or tt(-l) flags cancel any and all of tt(-kpquz).
)
cindex(parameters, marking readonly)
-alias(readonly)(typeset -r)
+item(tt(readonly))(
+Same as tt(typeset -r). With the tt(POSIX_BUILTINS) option set, same
+as tt(typeset -gr).
+)
alias(rehash)(hash -r)
findex(return)
cindex(functions, returning from)
diff --git a/Src/builtin.c b/Src/builtin.c
index 05907f1..557487c 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -62,7 +62,7 @@ static struct builtin builtins[] =
BUILTIN("enable", 0, bin_enable, 0, -1, BIN_ENABLE, "afmprs", NULL),
BUILTIN("eval", BINF_PSPECIAL, bin_eval, 0, -1, BIN_EVAL, NULL, NULL),
BUILTIN("exit", BINF_PSPECIAL, bin_break, 0, 1, BIN_EXIT, NULL, NULL),
- BUILTIN("export", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, BIN_EXPORT, "E:%F:%HL:%R:%TUZ:%afhi:%lprtu", "xg"),
+ BUILTIN("export", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "E:%F:%HL:%R:%TUZ:%afhi:%lprtu", "xg"),
BUILTIN("false", 0, bin_false, 0, -1, 0, NULL, NULL),
/*
* We used to behave as if the argument to -e was optional.
@@ -106,7 +106,7 @@ static struct builtin builtins[] =
BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL),
BUILTIN("r", 0, bin_fc, 0, -1, BIN_R, "IlLnr", NULL),
BUILTIN("read", 0, bin_read, 0, -1, 0, "cd:ek:%lnpqrst:%zu:AE", NULL),
- BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux", "r"),
+ BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, BIN_READONLY, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux", "r"),
BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"),
BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL, NULL),
BUILTIN("set", BINF_PSPECIAL | BINF_HANDLES_OPTS, bin_set, 0, -1, 0, NULL, NULL),
@@ -2533,6 +2533,10 @@ bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func)
if (OPT_ISSET(ops,'f'))
return bin_functions(name, argv, ops, func);
+ /* POSIX handles "readonly" specially */
+ if (func == BIN_READONLY && isset(POSIXBUILTINS) && !OPT_PLUS(ops, 'g'))
+ ops->ind['g'] = 1;
+
/* Translate the options into PM_* flags. *
* Unfortunately, this depends on the order *
* these flags are defined in zsh.h */
diff --git a/Src/hashtable.h b/Src/hashtable.h
index b6346bb..3606e97 100644
--- a/Src/hashtable.h
+++ b/Src/hashtable.h
@@ -53,7 +53,7 @@
#define BIN_LOGOUT 19
#define BIN_TEST 20
#define BIN_BRACKET 21
-#define BIN_EXPORT 22
+#define BIN_READONLY 22
#define BIN_ECHO 23
#define BIN_DISABLE 24
#define BIN_ENABLE 25
diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index 7d65cc8..681fe73 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -479,12 +479,12 @@
setopt POSIXBUILTINS
readonly pbro
print ${+pbro} >&2
- (typeset pbro=3)
+ (typeset -g pbro=3)
(pbro=4)
- readonly -p | grep pbro >&2 # shows up as "readonly" although unset
- typeset -r pbro # idempotent (no error)...
+ readonly -p pbro >&2 # shows up as "readonly" although unset
+ typeset -gr pbro # idempotent (no error)...
print ${+pbro} >&2 # ...so still readonly...
- typeset +r pbro # ...can't turn it off
+ typeset -g +r pbro # ...can't turn it off
)
1:readonly with POSIX_BUILTINS
?0
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [BUG] functions can't create global readonly variables
2016-01-02 20:36 ` Bart Schaefer
@ 2016-01-03 19:00 ` Peter Stephenson
2016-01-03 19:21 ` Bart Schaefer
0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2016-01-03 19:00 UTC (permalink / raw)
To: zsh-workers
By the way it seems that, in bash at least, "readonly" breaks
encapsulation:
$ readonly foo=empty
$ fn() { local foo=bar; echo $foo; }
$ fn
bash: local: foo: readonly variable
empty
The only use I can see of this is for exported variables, where you
might want to ensure a specific value was always exported to an external
programme.
pws
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [BUG] functions can't create global readonly variables
2016-01-03 19:00 ` Peter Stephenson
@ 2016-01-03 19:21 ` Bart Schaefer
0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2016-01-03 19:21 UTC (permalink / raw)
To: zsh-workers
On Jan 3, 7:00pm, Peter Stephenson wrote:
}
} $ readonly foo=empty
} $ fn() { local foo=bar; echo $foo; }
} $ fn
} bash: local: foo: readonly variable
} empty
I suppose if you consider locality to be a property of the variable,
then making the variable readonly means you can't change that property.
Fortunately readonly in a nested function, where the name is declared
local in the outer function, does work like zsh's "typeset -gr" and
does not force the readonly into truly global scope.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2016-01-03 19:21 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-02 3:27 [BUG] functions can't create global readonly variables Martijn Dekker
2016-01-02 4:27 ` Bart Schaefer
2016-01-02 18:18 ` Peter Stephenson
2016-01-02 20:36 ` Bart Schaefer
2016-01-03 19:00 ` Peter Stephenson
2016-01-03 19:21 ` Bart Schaefer
2016-01-02 19:39 ` Martijn Dekker
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).