zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] More "typeset -p" and GLOBAL_EXPORT
Date: Wed, 13 Mar 2024 19:11:11 -0700	[thread overview]
Message-ID: <CAH+w=7adenmhor6ctccUgvha_M+DHX41oW7gioP=N91PLtXzhQ@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7b3OyC8bDfcN46LH-p8Dk6dWnS_JjYMgd9R2mngxwm__w@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2205 bytes --]

On Tue, Mar 12, 2024 at 1:38 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Tue, Mar 12, 2024 at 1:26 PM Stephane Chazelas <stephane@chazelas.org> wrote:
> >
> > $ zsh -c 'a() { export a; a=3; typeset -p a; }; b() { local a=2; a; typeset -p a; }; a=1; b; typeset -p a'
> > export -x a=3
> > typeset -x a=3
> > typeset a=1
> >
> > That export -x seems bogus BTW as export doesn't accept the -x
> > option.
>
> Indeed, that's a bug in the printparamnode() routine.

That's not the only bug.  As demonstrated by B02typeset.ztst in the test
  no array/hash in POSIX export/readonly -p
with a declaration in a function of
   local -rx zsh_exported_readonly_scalar=1
the output of "export -p" is
  typeset -rx zsh_exported_readonly_scalar=1

However, "local" is supposed to supersede the GLOBAL_EXPORT option, so
that should have output one of
  typeset +g -rx zsh_exported_readonly_scalar=1
or
  local -rx zsh_exported_readonly_scalar=1

The attached patch changes this (using "local").  The POSIX mode output --
  export zsh_exported_readonly_scalar=1
-- is explained by a comment:
         * The zsh variants of export -p/readonly -p also report other
         * flags to indicate other attributes or scope. The POSIX variants
         * don't.

There is also an AFAICT insurmountable problem:
  innie() { typeset -p $1 }
  outie() { local -x $1; innie $1 }
  outie var
displays
  export var=''

That is "correct" in that var is not local to outie, but that
statement cannot be evaluated to restore the state of $var anywhere
other than from functions called by innie.  Conversely:
  innie() { print ${(tP)1} }
  outie var
displays
  scalar-local-export

Which is also "correct" but fibs about $var being local to innie.
Finally we have
  innie() { typeset +m $1 }
  outie var
which agrees with the (t) flag by reporting
  local exported var

And
  innie() { typeset -m $1 }
  outie var
yielding
  var=''

No mention of export at all.

These are all long-standing behaviors and haven't caused anyone a
problem yet that I know of, so I'm not suggesting anything change,
just pointing out that this patch doesn't address them.

[-- Attachment #2: printparamflags.txt --]
[-- Type: text/plain, Size: 2733 bytes --]

diff --git a/Src/params.c b/Src/params.c
index 973df3fe5..f65aa1e80 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -6044,6 +6044,7 @@ printparamnode(HashNode hn, int printflags)
 {
     Param p = (Param) hn;
     Param peer = NULL;
+    int altname = 0;
 
     if (!(p->node.flags & PM_HASHELEM) &&
 	!(printflags & PRINT_WITH_NAMESPACE) && *(p->node.nam) == '.')
@@ -6089,16 +6090,26 @@ printparamnode(HashNode hn, int printflags)
 	if (printflags & PRINT_POSIX_EXPORT) {
 	    if (!(p->node.flags & PM_EXPORTED))
 		return;
+	    altname = 'x';
 	    printf("export ");
 	} else if (printflags & PRINT_POSIX_READONLY) {
 	    if (!(p->node.flags & PM_READONLY))
 		return;
+	    altname = 'r';
 	    printf("readonly ");
-	} else if (locallevel && p->level >= locallevel) {
-	    printf("typeset ");	    /* printf("local "); */
 	} else if ((p->node.flags & PM_EXPORTED) &&
 		   !(p->node.flags & (PM_ARRAY|PM_HASHED))) {
-	    printf("export ");
+	  if (p->level && p->level >= locallevel)
+		printf("local ");
+	    else {
+		altname = 'x';
+		printf("export ");
+	    }
+	} else if (locallevel && p->level >= locallevel) {
+	    if (p->node.flags & PM_EXPORTED)
+		printf("local ");
+	    else
+		printf("typeset ");	    /* printf("local "); */
 	} else if (locallevel) {
 	    printf("typeset -g ");
 	} else
@@ -6112,6 +6123,10 @@ printparamnode(HashNode hn, int printflags)
 
 	for (pmptr = pmtypes, i = 0; i < PMTYPES_SIZE; i++, pmptr++) {
 	    int doprint = 0;
+
+	    if (altname && altname == pmptr->typeflag)
+		continue;
+
 	    if (pmptr->flags & PMTF_TEST_LEVEL) {
 		if (p->level) {
 		    /*
diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index d90f17d13..914eea92b 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -311,7 +311,7 @@
  print $OUTER
 0:Export of tied parameters
 >i:n:n:e:r
->typeset -xT OUTER outer=( i n n e r )
+>local -xT OUTER outer=( i n n e r )
 >typeset -aT OUTER outer=( i n n e r )
 >OUTER=i:n:n:e:r
 >outer=( i n n e r )
@@ -1099,12 +1099,12 @@
  }
 0: no array/hash in POSIX export/readonly -p
 >zsh:
->typeset -arx zsh_exported_readonly_array=( 2 )
->typeset -Arx zsh_exported_readonly_hash=( [3]=3 )
->typeset -rx zsh_exported_readonly_scalar=1
->typeset -arx zsh_exported_readonly_array=( 2 )
->typeset -Arx zsh_exported_readonly_hash=( [3]=3 )
->typeset -rx zsh_exported_readonly_scalar=1
+>local -arx zsh_exported_readonly_array=( 2 )
+>local -Arx zsh_exported_readonly_hash=( [3]=3 )
+>local -rx zsh_exported_readonly_scalar=1
+>local -arx zsh_exported_readonly_array=( 2 )
+>local -Arx zsh_exported_readonly_hash=( [3]=3 )
+>local -rx zsh_exported_readonly_scalar=1
 >sh:
 >export zsh_exported_readonly_scalar=1
 >readonly zsh_exported_readonly_scalar=1

      parent reply	other threads:[~2024-03-14  2:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-12  4:13 "typeset -p" and no_GLOBAL_EXPORT, other misc Bart Schaefer
2024-03-12  8:49 ` Stephane Chazelas
2024-03-12 18:32   ` Bart Schaefer
2024-03-12 20:06     ` Stephane Chazelas
2024-03-12 20:31       ` Bart Schaefer
2024-03-12 20:48         ` Stephane Chazelas
2024-03-12 21:02         ` Bart Schaefer
2024-03-12 20:26     ` Stephane Chazelas
2024-03-12 20:38       ` Bart Schaefer
2024-03-12 20:58         ` Stephane Chazelas
2024-03-12 21:03           ` Bart Schaefer
2024-03-13  0:52         ` {PATCH] (for real this time) " Bart Schaefer
2024-03-14  2:11         ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7adenmhor6ctccUgvha_M+DHX41oW7gioP=N91PLtXzhQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).