Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129484 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id AE1E51A00BC for ; Mon, 1 Dec 2025 20:07:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1764619636; bh=lIXs0ggJEYpWc6IsHfzNMhhWc4HXCa52Opz8T+ua9vg=; h=Date:From:To:In-Reply-To:References:Subject:From; b=jq9ZnTanlTOWQWtFaXPTqHVIHjy0ytXOOSpj6ecXQoYaYxBCE63+/6lS6Fmy2CooA tFmy1K8HxrWvWmNyZc4GduwajA4U5ny+/dkl3gl6/ViU002SBNynFgWzYY8wK7nce9 cCfm1tugbCWvh8IQQ6NGzrK8ww9bS9vtYMi4wuApyEE0kc5ri/ddPsT0DTxAtwi2Xc FLWNsM8lXeFpLCe8epUEhTyqRIo4YnVwQx7JqjbTotFWxzclnuxxM38nwPWmwWyFXz mXqCaDL7tYRaR8WFzHKNfK94fCwZFyEqbLUQvkJOM3r0hcPQZDuLMvKhQlNc3G3yMz w9jCxjD/PorTg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5D97E18006C for ; Mon, 1 Dec 2025 20:07:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fout-b5-smtp.messagingengine.com (fout-b5-smtp.messagingengine.com [202.12.124.148]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 1 Dec 2025 20:07:15 +0000 (UTC) Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfout.stl.internal (Postfix) with ESMTP id A14A51D00050 for ; Mon, 1 Dec 2025 15:07:09 -0500 (EST) Received: from phl-imap-01 ([10.202.2.91]) by phl-compute-04.internal (MEProxy); Mon, 01 Dec 2025 15:07:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to; s=fm1; t=1764619629; x=1764706029; bh=/PsfL3ISW5yWpl3VVqpsU 9mcknLEH9dChRNgez5XmxY=; b=QQ6cBzVxm0/JxRNO10hD+4k/dvknhs0aWwMo0 uPIjXl+81UT7reR1BpVqHD/poDCjjAJUO5+Sbhh6108A1USXoRj4lJt/bsk3MHQb pivVDFXUO4fcw9ZSiB3CljGY6SYUrCP+oPhESjVcU4NWC56Ow7t47wp9T8+kCeyr Zi+EkvkQh/EAIUn1E60dkX8UvYpktLy0lvMq/fNftp9wTDEnqY7fXD2cBuIs10tt VqoEMn1MKAjVmbJVBW0Vkzz/518FEJQaJyMhynFIDy2PO8JYl11GpoOiW0myDNnD 5C3R8xffmnKZCdgiHIOiDXUdLt8SPLGhZWdMRlbxyXY95M5pg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1764619629; x=1764706029; bh=/ PsfL3ISW5yWpl3VVqpsU9mcknLEH9dChRNgez5XmxY=; b=gT2Eg0FYFOFp0OTZj XoFdIF71A6MyxkRFWJCfxv1xIIwG//PV2+Fs9pGY+PVz3uH2pPv5U/48QzdI+yKI sGXjWvrKf3/+jVIlnd2c/znojTO7RfW2N3NmGDYwlTNZtTu02PTwFJqgQM0Ddobg 1yDTjIUs3xGJa/HUauD4NWHS/GrF+PjCvwWtLOdRfFlkEum9nNSh2GGzPK8oH+c4 q+9rFJp9RrZqW2E4iq95zPPDKAAadfdaZqhA+wPLJ+FOuXgo186InitquVUTMdyD T1m8HqNZ2KmFR4BeU57zhNj3aXqEnvEqdBr1h5YAIzS3MZfQ3b6Mk36aXGbjmipZ lqmlw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddvheekiedvucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepofggfffhvffkjghfufgtgfesthhqredtredtjeenucfhrhhomhepfdfnrghrrhih ucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhmqe enucggtffrrghtthgvrhhnpeffieeivdfhvdeguddttdegteeiueegvefhteehfeeffeet udeitdehtdegjeeuieenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomhdpnhgspghrtghp thhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsh eslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 021DB18C004E; Mon, 1 Dec 2025 15:07:09 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: ALvh3TDXSVwZ Date: Mon, 01 Dec 2025 14:06:27 -0600 To: "php internals" Message-ID: In-Reply-To: References: <74abada6-cb34-490e-abeb-517e39b3ffa0@app.fastmail.com> Subject: Re: [PHP-DEV] [RFC] Context Managers Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Mon, Dec 1, 2025, at 9:38 AM, Rowan Tommins [IMSoP] wrote: > On 01/12/2025 14:26, Tim D=C3=BCsterhus wrote: >> You are wrong here. Manually interacting with an Iterator is not just=20 >> safe, it is also necessary for some use cases. The simplest example=20 >> would be iterating two Iterators in lock-step (i.e. doing a `zip()`=20 >> operation / array_combine()).=20 > > > It is certainly *possible* to use the Iterator methods safely without = a=20 > foreach() construct or built-in aggregate; but it's also easy to miss=20 > the implications, and cause very confusing behaviour. For instance, yo= u=20 > could call next() in two different pieces of code, and each would miss=20 > half the items; or you could call rewind() on an iterator that was=20 > already in use elsewhere. > > The same is true of the ContextManager interface: you could certainly=20 > call enterContext() and exitContext() manually with no problems at all= .=20 > It might even be necessary, for much the same reasons as iterators: yo= u=20 > might want to write code that aggregates two context managers in a=20 > specific arrangement. > > > As I understand it, your concern is that someone will call=20 > enterContext() without exitContext(). You could do exactly the same=20 > thing if you manually handle an Iterator. > > Consider this method: > > function getDataIterator(): Generator { > =C2=A0 =C2=A0 $this->acquireLock(); > =C2=A0 =C2=A0 while ( $this->hasMoreResults() ) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 yield $this->getNextResult(); > =C2=A0 =C2=A0 } > =C2=A0 =C2=A0 $this->releaseLock(); > } > > If the user calls $it =3D $foo->getDataIterator(), they can call=20 > $it->current() and get a result, but never call $it->next(). They coul= d=20 > even use it in a foreach() loop but `break` out before consuming all t= he=20 > results. In such cases, the lock would never be released, unless there= 's=20 > an extra safety check in the destructor. Excellent example, thank you. > I think the currently proposed names actually make that *less* likely=20 > for a ContextManager: if you enter something, you probably want to exi= t=20 > it later. > > We could reinforce that further by also prefixing the method names wit= h=20 > "__", but we don't have any precedent for that, and misuse would still=20 > be possible. Python uses magic methods here, and we considered it, but given that you= will basically always want both methods we felt an interface was easier= and provided better type checks. If there were multiple callbacks (say= , a different one for a success exit and a fail exit), then magic method= s might make more sense. If you're suggesting having interface methods that begin with __, I agre= e that would be novel for no particular value. You can't screw up Conte= xtManagers through misuse any more than you can screw up Iterator or Arr= ayAccess, and we've survived just fine with those for literally decades. --Larry Garfield