From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 8194 invoked from network); 10 Dec 2022 15:03:31 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 10 Dec 2022 15:03:31 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1670684611; b=dVTu173v2yWovfApZrLq9Q4isBzQqXT0TUfG2XEkXQUDcfAu86ho3Z2Ds8Aiw2d9giU1N+cCOd /OJAFjoabRVbZEFZ8BkTIiNMnPzGtZytI48tkEvtFzS1dLjVztfkqWuPH1jOxErvysVrGqgMQW aC0Rw1LF2y9z21F+52wp/xzkAuuQcYYDgDL16FN65c56h3TKuoVZZKWVfK4exv71jhI4U9ZAmg u3sXx9eW5wF07Ahv3eOPzLZTC0rwn99FXi2TlElcZWaHw5qpJIjJ6/WbTQvluG0dGrtVGYDxib utWvEJpkE6m0qKBD5GkfumTpmANKZo85SUz9VgYS2DLEAg==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-pj1-f53.google.com) smtp.remote-ip=209.85.216.53; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1670684611; bh=n4+mIzHIV1EQHdoPSd7kJSacWWEati+bv1oxD8J8kKg=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=GfQ8xQY4kUsbS6sMm285kg8BRmRrhXzNyfaJ/5VP251sPbQJtF4OnAl3CnO+2X8Ew/AIfPOLdh DW/eP1kAhaXOTt/Bj4MgwYJp78+189VJDIplqSVA88z2enuF7xQqiJqAzpa9gQ1CG4lnYy1bZu fuVMnVu0ooBnLDvuqemvSWzE0aHcaruybxLit81G1OzZpwGkjbAofIalMCSnfK7GyZU0i3iR/G JpqOze2/XVnM9PD4cnFC14fkcbrqxZ0d6YNXn3fGqGc+ps2LetffDF9xhbyxcN9IWIACKlmDXT oQlXjjrumbfp8oM2cw3DsKumIxDe2oFZreu22Ny4+55P9g==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:To:Subject:Message-ID: Date:From:In-Reply-To:References:MIME-Version:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=nz+pP6F2YD2H9z9mvxsOydfgH9vZv62XKNPHVzfbM3Q=; b=H9zQ/0iW0+IZ7bpGSF/IRQ7VmJ GDV/EO7ZL+OYPWckgrmrTnIdXD3bfvvOLr9KMykoJIo2qogU22UqyT8/MW6kbGUFbQaZQBH6pp4/c JnB8mONsV15/rFOUOkCmAL+duCJ6sSv+C0wXVXd65g7ijBNNVkxiaW0b+ylCqLlfX2b/yOX0UlR9i 5bRquldqjksaXcmGN8ue4Tf53Ep2p6rjZtWAEZtY4j6hZRvu4MndTXorNp2OII6zbS+vu1ToYjTw4 2Z1arsVFL0M7yJsfRhmU10DgpfkWCNpmT0/5v63EiyXvnDmlIg/02Hdj+HSi/EBJqYtOZeF3rPK4X kpZMdciQ==; Received: by zero.zsh.org with local id 1p41Nx-0006fP-2u; Sat, 10 Dec 2022 15:03:29 +0000 Authentication-Results: zsh.org; iprev=pass (mail-pj1-f53.google.com) smtp.remote-ip=209.85.216.53; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-pj1-f53.google.com ([209.85.216.53]:55980) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1p41NL-0006MN-SM; Sat, 10 Dec 2022 15:02:53 +0000 Received: by mail-pj1-f53.google.com with SMTP id u5so7660936pjy.5 for ; Sat, 10 Dec 2022 07:02:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=nz+pP6F2YD2H9z9mvxsOydfgH9vZv62XKNPHVzfbM3Q=; b=PBjL9cBc6FtoHZMkuZB7/bHy7dL4Af4IIDLoOf2leX0cZFO7OCVa6OBU8u4Asl0MFI lQcdEPsnzO5+ajSWsmz1Oeq6Aj8ItE2Tri2D1HqyaAS9qzObruLpPZSnkalMOYi7VB4S WHgfa6QiyDOd1lRTaJip90wyivz+DIgrKbLq2iw5SXb0LgehskHP6gIiQr9Xmwfc0qJ3 zeTK33qJqQHuP1dUSdau+VV23CKB0a3XwhgtpVoL53Er4osgz11YuJudTlsrIU1Apwtl CCzI7BjSdyxTfpsSP57K1WNItCK4I8/JqIkK1ZqKbx7ekNAo4/m76XaukiKfLnAoPTXo 1q8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nz+pP6F2YD2H9z9mvxsOydfgH9vZv62XKNPHVzfbM3Q=; b=3RwajN/xpzMeeLuJROo0bMnpN+rqDoLgT69Tpp4+s+sY9jpy+tgicI7h2Wh+jt1IJO F1SiPmOQaC8/r9bUbua0fwzngXyg5kpQC5ROPlyw64C1xRruSioCO8xgIRfn3AOHcu6L NUA7vUKjfx/gy1RhojxE/5iRiNXp44+VHr4tiJCSxQeXmFu+ZurjmRwKQ+1+XQr7Cq3b LVHoVVw9NuWg2JVl4fWSs2qWo09B8rpQCnjKRgpXMTvHkIaZjrwcOmySNreTrDmkI4mv n3d8MfDOR9htTySqkcZFkspGNm9H1u9I1c+2PPl9OtwKhUvuoD/kG8gJ23bvTbM3uX1k g7Kw== X-Gm-Message-State: ANoB5pl/v5L8rv1qqlJJ9qy4qCY0rYjCzryB9+QoKfjiyp/ooKwrlRNZ jPeMg6LUEy5Gjp7gm5jIyZy0+0QbIakezZKbxT4sH5juzwI= X-Google-Smtp-Source: AA0mqf6z4fpJUKy0TOrll+Kylea1cjdUcpze2v+d5G6KMl/TQe8/NxZJHJ4K+/cATzj/iEdPzTEzrAL+PL557lOTgg4= X-Received: by 2002:a67:f144:0:b0:3b1:3ad7:b7d9 with SMTP id t4-20020a67f144000000b003b13ad7b7d9mr11263935vsm.67.1670684145856; Sat, 10 Dec 2022 06:55:45 -0800 (PST) MIME-Version: 1.0 References: <9e3026aa-39a1-dd50-4d29-a64724d4eaaf@irit.fr> <20221208082103.zg44mrv77jrizsaj@chazelas.org> <20221208100215.k2qcqdqgjlzwbdh7@chazelas.org> In-Reply-To: From: Philippe Altherr Date: Sat, 10 Dec 2022 15:55:34 +0100 Message-ID: Subject: Private variables not private enough? (Was: Get cursor position (Was: [bug report] prompt can erase messages written on the terminal by background processes)) To: Zsh hackers list Content-Type: multipart/alternative; boundary="0000000000000bb92405ef7a7783" X-Seq: 51176 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: --0000000000000bb92405ef7a7783 Content-Type: text/plain; charset="UTF-8" My code was mainly a proof of concept. I didn't put much thought into questions like "print -v" vs "typeset -g". I was just pleasantly surprised that the whole thing was at all possible :-) > It's a bit strange that the initialization is needed. Private variables > don't hide global variables of the same name, nor prevent their assignment, > but prevent their initial declaration. > This isn't exactly an effect of private, it's the previously observed > effect that there is no way to "unlocal" a parameter in the current > scope, so you have to add it to the desired scope before declaring it > local. Private variables are supposed to have a scope that is limited to the function where they are declared. Intuitively that implies that they should have no impact on the behavior of callees. However, that's not always the case. Consider the following example: zmodload zsh/param/private > (() { v=; () { typeset -g v=z } }; echo 1:v=$v) > (() { v=; () { print -v v z } }; echo 2:v=$v) > (() { () { typeset -g v=z } }; echo 3:v=$v) > (() { () { print -v v z } }; echo 4:v=$v) > (() { v=; local -P v=x; () { typeset -g v=z } }; echo 5:v=$v) > (() { v=; local -P v=x; () { print -v v z } }; echo 6:v=$v) > (() { local -P v=x; () { typeset -g v=z } }; echo 7:v=$v) > (() { local -P v=x; () { print -v v z } }; echo 8:v=$v) Here is the result 1:v=z > 2:v=z > 3:v=z > 4:v=z > 5:v=z > 6:v=z > 7:v= > (anon): v: attempt to assign private in nested scope Case 1 to 4 show that both "typeset -g" and "print -v" are able to set a global variable independent of whether the variable already exists (case 1-2) or not (case 3-4). Case 5 and 6 show that "typeset -g" and "print -v" are able to set an already existing global variable even if there is a private variable with the same name in a parent scope. In these cases, the private variable is truly private and doesn't affect the behavior of the callee. Case 7 and 8 show that the same isn't true if the global variable doesn't already exist. I find this very counterintuitive. With truly private variables, I don't see why case 7 and 8 should exhibit a different behavior than case 3 and 4. Philippe --0000000000000bb92405ef7a7783 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
My code was mainly a proof of concept. I didn't put mu= ch thought into questions like "print -v" vs "typeset -g&quo= t;. I was just=C2=A0pleasantly surprised=C2=A0that the whole thing was at a= ll possible :-)

> It's a bit strange that the initialization is needed. P= rivate variables don't hide global variables of the same name, nor prev= ent their assignment, but prevent their initial declaration.

This isn't exactly an effect of= private, it's the previously observed
effect that there is no way t= o "unlocal" a parameter in the current
scope, so you have to a= dd it to the desired scope before declaring it
local.
<= br>
Private variables are supposed to have a scope that is limite= d to the function where they are declared. Intuitively that implies that th= ey should have no impact on the behavior of callees. However, that's no= t always the case. Consider=C2=A0the following example:

zmodload zsh/param/private

(() = { v=3D; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 () { typeset -g v= =3Dz } }; echo 1:v=3D$v)
(() { v=3D; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 () { print =C2=A0 -v v z } }; echo 2:v=3D$v)
(() { =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 () { typeset -g v= =3Dz } }; echo 3:v=3D$v)
(() { =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 () { print =C2=A0 -v v z } }; echo 4:v=3D$v)

(() { v=3D; local -P v=3Dx; () { typese= t -g v=3Dz } }; echo 5:v=3D$v)
(() { v=3D; local -P v=3Dx; () { print = =C2=A0 -v v z } }; echo 6:v=3D$v)
(() { =C2=A0 =C2=A0 local -P v=3Dx; ()= { typeset -g v=3Dz } }; echo 7:v=3D$v)
(() { =C2=A0 =C2=A0 local -P v= =3Dx; () { print =C2=A0 -v v z } }; echo 8:v=3D$v)
= =C2=A0
Here is the result

1:v=3Dz
2:v=3D= z
3:v=3Dz
4:v=3Dz
5:v=3Dz
6:v=3Dz
7:v=3D
(anon): v: attem= pt to assign private in nested scope

Case 1 to 4 show that both "typeset -g" and "print -v"= are able to set a global variable independent=C2=A0of whether the variable= already=C2=A0exists (case 1-2) or not (case 3-4).

Case 5 and 6 show that "typeset -g" and "print -v" are= able to set an already existing global variable=C2=A0even if there is a pr= ivate variable with the same name in a parent scope. In these cases, the pr= ivate variable is truly private and doesn't affect the behavior of the = callee.

Case 7 and 8 show that the same isn't = true if the global variable doesn't already exist. I find this very cou= nterintuitive. With truly private variables, I don't see why case 7 and= 8 should exhibit a different behavior than case 3 and 4.

Philippe

--0000000000000bb92405ef7a7783--