<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=utf-8" http-equiv=Content-Type><!-- flashmail style begin -->
<STYLE type=text/css>
body {border-width:0;margin:0}
img {border:0;margin:0;padding:0}
</STYLE>
<BASE target=_blank><!-- flashmail style end -->
<META name=GENERATOR content="MSHTML 11.00.10570.1001"></HEAD>
<BODY 
style="BORDER-LEFT-WIDTH: 0px; FONT-SIZE: 10.5pt; FONT-FAMILY: 微软雅黑; BORDER-RIGHT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; COLOR: #000000; MARGIN: 12px; LINE-HEIGHT: 1.5; BORDER-TOP-WIDTH: 0px" 
marginheight="0" marginwidth="0">
<DIV>Well I'm really shocked to find out what I thought was a "automatic close" 
is really the ref-couting GC of CPython, means that a lot of my code breaks in 
PyPy...</DIV>
<DIV>It really becomes a big problem after iterators heavily used in Python 
nowadays. Some builtin functions like zip, map, filter return iterators in 
Python 3 instead of lists in Python 2, means invisible bugs for code ported 
from Python 2, like zip(my_generator(), my_other_generator()) may leave the 
iterators open if exited from a for loop. Even in Python 2, functions in 
itertools may create these bugs.</DIV>
<DIV>In CPython, this kind of code will work because of the ref-counting GC, so 
it is not obvious in CPython, but they break in PyPy.</DIV>
<DIV> </DIV>
<DIV>I'm wondering since a ref-counting GC implemention is not possible for 
PyPy, is it possible to hack on the for loop to make it "try to" collect the 
generator? That may really save a lot of lives. If the generator is still 
referenced after the for loop, it may be the programmer's fault for not calling 
close(), but loop through a returned value is something different - sometimes 
you even do not know if it is a generator.</DIV>
<DIV> </DIV>
<DIV style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; COLOR: #c0c0c0" 
align=left>2016-10-21 
<HR id=SignNameHR 
style="BORDER-TOP: #c0c0c0 1px solid; HEIGHT: 1px; BORDER-RIGHT: 0px; WIDTH: 122px; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px" 
align=left>
<SPAN id=_FlashSignName>hubo</SPAN> </DIV>
<HR 
style="BORDER-TOP: #c0c0c0 1px solid; HEIGHT: 1px; BORDER-RIGHT: 0px; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px">

<BLOCKQUOTE id=ntes-flashmail-quote 
style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; PADDING-LEFT: 0px; MARGIN-LEFT: 0px">
  <DIV><STRONG>发件人:</STRONG>Armin Rigo <armin.rigo@gmail.com></DIV>
  <DIV><STRONG>发送时间:</STRONG>2016-10-18 16:01</DIV>
  <DIV><STRONG>主题:</STRONG>Re: [pypy-dev] RFC: draft idea for making for loops 
  automatically close iterators</DIV>
  <DIV><STRONG>收件人:</STRONG>"Nathaniel Smith"<njs@pobox.com></DIV>
  <DIV><STRONG>抄送:</STRONG>"PyPy Developer Mailing 
  List"<pypy-dev@python.org></DIV>
  <DIV> </DIV>
  <DIV>
  <DIV>Hi, </DIV>
  <DIV> </DIV>
  <DIV>On 17 October 2016 at 10:08, Nathaniel Smith <njs@pobox.com> wrote: </DIV>
  <DIV>> thought I'd send around a draft to see what you think. (E.g., would </DIV>
  <DIV>> this be something that makes your life easier?) </DIV>
  <DIV> </DIV>
  <DIV>As a general rule, PyPy's GC behavior is similar to CPython's if we </DIV>
  <DIV>tweak the program to start a chain of references at a self-referential </DIV>
  <DIV>object.  So for example, consider that the outermost loop of a program </DIV>
  <DIV>takes the objects like the async generators, and stores them inside </DIV>
  <DIV>such an object: </DIV>
  <DIV> </DIV>
  <DIV>    class A: </DIV>
  <DIV>         def __init__(self, ref): </DIV>
  <DIV>              self.ref = ref </DIV>
  <DIV>              self.myself = self </DIV>
  <DIV> </DIV>
  <DIV>and then immediately forget that A instance.  Then both this A </DIV>
  <DIV>instance and everything it refers to is kept alive until the next </DIV>
  <DIV>cyclic GC occurs.  PyPy just always exhibits that behavior instead of </DIV>
  <DIV>only when you start with reference cycles. </DIV>
  <DIV> </DIV>
  <DIV>So the real issue should not be "how to so something that will make </DIV>
  <DIV>PyPy happy", or not only---it should be "how to do something that will </DIV>
  <DIV>make CPython happy even in case of reference cycles".  If you don't, </DIV>
  <DIV>then arguably CPython is slightly broken. </DIV>
  <DIV> </DIV>
  <DIV>Yes, anything that can reduce file descriptor leaks in Python sounds good to me. </DIV>
  <DIV> </DIV>
  <DIV> </DIV>
  <DIV>A bientôt, </DIV>
  <DIV> </DIV>
  <DIV>Armin. </DIV>
  <DIV>_______________________________________________ </DIV>
  <DIV>pypy-dev mailing list </DIV>
  <DIV>pypy-dev@python.org </DIV>
  <DIV>https://mail.python.org/mailman/listinfo/pypy-dev </DIV></DIV></BLOCKQUOTE></BODY></HTML>