Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] ERR_EXIT with "for" loops and shell functions (Re: Bug report)



On Sat, 27 Dec 2014 16:32:20 -0800
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Dec 26,  6:35pm, Bart Schaefer wrote:
> }
> } This is very tricky because we have to propagate the value of $? from
> } before the "for" keyword (including before the start of the function)
> } into the "do" body, but must *not* *use* the value of $? for deciding
> } whether to exit-on-error, because the nonzero value came from an "if"
> } test.  There is a "noerrexit" flag that is supposed to cover this case,
> } but it's not set when the very first "sublist" in a function body is
> } a for-loop (there are likely to be other similar cases).
> 
> The following patch attempts to fix all of this weirdness.

I had a look at this myself while in 2Gnetworkland, and sent a couple of
messages which didn't get through quite possibly because the gmail app
sent them to zsh-hackers@xxxxxxx for undetermined reasons.

However, you must have found everything I did since the tests I added
now pass.  They're all just for status, nothing specific to ERR_EXIT or
ERR_RETURN; I didn't find I needed to change anything there in the main
code, just for the status value itself.  They overlap with yours but that's no big deal.  Here they are.

diff --git a/Test/A07control.ztst b/Test/A07control.ztst
index 397a821..b1a2487 100644
--- a/Test/A07control.ztst
+++ b/Test/A07control.ztst
@@ -110,3 +110,56 @@
   done
 1:break error case -1
 ?(eval):break:2: argument is not positive: -1
+
+  false
+  for x in; do
+    print nothing executed
+  done
+0:Status 0 from for with explicit empty list
+
+  set --
+  false
+  for x; do
+    print nothing executed
+  done
+0:Status 0 from for with implicit empty list
+
+  (exit 2)
+  for x in 1 2; do
+    print $?
+  done
+0:Status from previous command propagated into for loop
+>2
+>0
+
+  false
+  for x in $(echo 1 2; (exit 3)); do
+    print $?
+  done
+0:Status from expansion propagated into for loop
+>3
+>0
+
+  false
+  for x in $(exit 4); do
+    print not executed
+  done
+0:Status from expansion not propagated after unexecuted for loop
+  
+  false
+  for x in NonExistentFilePrefix*(N); do
+    print not executed, either
+  done
+0:Status from before for loop not propagated if empty after expansion
+
+  for x in $(echo 1; false); do
+  done
+0:Status reset by empty list in for loop
+
+  false
+  for x in $(echo 1; false); do
+    echo $?
+    (exit 4)  
+  done
+4:Last status from loop body is kept even with other funny business going on
+>1

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author