zsh-workers
 help / color / mirror / code / Atom feed
From: "Timothée Mazzucotelli" <timothee.mazzucotelli@gmail.com>
To: Peter Stephenson <p.w.stephenson@ntlworld.com>
Cc: zsh-workers@zsh.org
Subject: Re: Feature request: ZSH_XTRACEFD variable
Date: Sat, 2 May 2020 20:02:51 +0200	[thread overview]
Message-ID: <CAD8ZDTp=rYsWiEkq3byjU=BRAA2iLwxt5QG_19MBc+Jo8dC-0w@mail.gmail.com> (raw)
In-Reply-To: <309829031.4459446.1587391766024@mail2.virginmedia.com>

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

I wrote such a test and noticed that file descriptors were being closed
each time ZSH_XTRACEFD was (re)assigned, even as a local variable.
So I removed the code lines closing the previous file descriptor in
xtracefdsetfn, and it seemed to work well.

However it seems that leaving the scope of a function where ZSH_XTRACEFD
was assigned as a local variable does not unset it,
and therefore leaves the file descriptor opened. I thought leaving the
scope of a function would implicitly "unset" all variables local to this
scope,
but it seems it's not the case.

I'm not sure how to fix that.
How to make sure the file descriptor of a local ZSH_XTRACEFD is closed when
leaving the function scope?
Or, how to make sure a local ZSH_XTRACEFD is unset when leaving the
function scope?
Or, how to call xtracefdunsetfn when leaving the function scope?

I pasted below the test that should pass if the local file descriptor was
properly closed.

  rm -f redir
  A() {
    local ZSH_XTRACEFD=5
    B
    print 'Function A to file descriptor 5'
    unset ZSH_XTRACEFD
    print 'Function A to file descriptor 2'
  }
  B() {
    local ZSH_XTRACEFD=6
    print 'Function B to file descriptor 6'
  }
  exec 4>redir4 5>redir5 6>redir6
  ZSH_XTRACEFD=4
  set -x
  print 'Main to file descriptor 4'
  A
  ZSH_XTRACEFD=5
  ZSH_XTRACEFD=6
  unset ZSH_XTRACEFD
  set +x
  cat redir4
  print
  cat redir5
  print
  cat redir6
0:Scoped ZSH_XTRACEFD correctly set and restored
>Main to file descriptor 4
>Function B to file descriptor 6
>Function A to file descriptor 5
>Function A to file descriptor 2
>+(eval):16> print 'Main to file descriptor 4'
>+(eval):17> A
>+A:1> local ZSH_XTRACEFD=5
>+(eval):18> ZSH_XTRACEFD=5
>+(eval):19> ZSH_XTRACEFD=6
>+(eval):20> unset ZSH_XTRACEFD
>
>+A:2> B
>+B:1> local ZSH_XTRACEFD=6
>+A:3> print 'Function A to file descriptor 5'
>+A:4> unset ZSH_XTRACEFD
>
>+B:2> print 'Function B to file descriptor 6'
?+A:5> print 'Function A to file descriptor 2'
?(eval):18: file descriptor 5 is not valid
?(eval):19: file descriptor 6 is not valid
?+(eval):21> set +x

  reply	other threads:[~2020-05-02 18:03 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-17 15:08 Timothée Mazzucotelli
2019-05-18  7:55 ` Stephane Chazelas
2019-05-20 10:34   ` Stephane Chazelas
2019-07-21 15:08     ` Timothée Mazzucotelli
2019-07-21 15:22       ` Peter Stephenson
2019-07-31 19:40         ` Timothée Mazzucotelli
2019-07-31 19:45           ` Timothée Mazzucotelli
2019-07-31 21:12             ` Sebastian Gniazdowski
2019-08-13  9:14           ` Peter Stephenson
2019-08-13 15:38           ` Peter Stephenson
2019-09-10 22:19             ` Timothée Mazzucotelli
2019-09-11  8:45               ` Peter Stephenson
2020-04-19 10:30             ` Timothée Mazzucotelli
2020-04-20 14:09               ` Peter Stephenson
2020-05-02 18:02                 ` Timothée Mazzucotelli [this message]
2020-05-02 21:15                   ` Bart Schaefer
2020-05-03  0:06                     ` Daniel Shahaf
2020-05-03  4:43                       ` Roman Perepelitsa
2020-05-03 16:21                         ` Daniel Shahaf
2020-05-03 19:54                         ` Peter Stephenson
2020-05-03 21:06                           ` Daniel Shahaf
2020-05-04  8:35                             ` Peter Stephenson
2020-05-05  0:03                               ` Daniel Shahaf
2020-05-05 16:36                                 ` Timothée Mazzucotelli
2020-05-05 16:47                                   ` Peter Stephenson
2020-05-05 22:19                                     ` Bart Schaefer
2020-09-03 13:51                                       ` Timothée Mazzucotelli
2020-09-04 19:48                                         ` Daniel Shahaf
2020-09-04 19:53                                           ` Bart Schaefer
2020-09-05  9:24                                             ` Timothée Mazzucotelli
2020-05-05 22:11                                   ` Bart Schaefer
2020-05-03  6:01                       ` Stephane Chazelas
2020-05-03  7:07                         ` Stephane Chazelas
2020-05-03 17:30                   ` Peter Stephenson
2020-05-03 21:23                     ` Daniel Shahaf
2020-05-04  8:26                       ` Peter Stephenson
2020-05-05 20:50               ` Daniel Shahaf

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='CAD8ZDTp=rYsWiEkq3byjU=BRAA2iLwxt5QG_19MBc+Jo8dC-0w@mail.gmail.com' \
    --to=timothee.mazzucotelli@gmail.com \
    --cc=p.w.stephenson@ntlworld.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).