Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'exception': 0.03; 'cpython': 0.05; 'mrab': 0.05; 'function,': 0.07; 'lines.': 0.07; 'objects,': 0.07; 'api': 0.09; 'python': 0.09; "'''": 0.09; 'absent': 0.09; 'concurrent': 0.09; 'deletion': 0.09; 'friday,': 0.09; 'garbage': 0.09; 'immutable': 0.09; 'internally': 0.09; 'objects.': 0.09; 'refresh': 0.09; 'structure,': 0.09; 'subject:set': 0.09; 'terms,': 0.09; 'to:addr:comp.lang.python': 0.09; 'tuple': 0.09; 'weak': 0.09; 'cc:addr:python-list': 0.10; 'aug': 0.13; 'library': 0.15; 'sat,': 0.15; '"set': 0.16; 'above?': 0.16; 'arrays.': 0.16; 'auxiliary': 0.16; 'chris,': 0.16; 'encouraging.': 0.16; 'introduces': 0.16; 'invisible': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterators': 0.16; 'justified': 0.16; 'set,': 0.16; 'storing': 0.16; 'wrote:': 0.17; 'hacking': 0.17; 'mechanism': 0.17; 'url:dev': 0.17; 'hack': 0.18; 'obviously': 0.18; 'otherwise,': 0.20; 'earlier': 0.21; 'cc:2**0': 0.23; "python's": 0.23; 'references': 0.23; 'patch': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'creating': 0.26; 'url:wiki': 0.26; '(see': 0.27; 'am,': 0.27; 'question': 0.27; 'first.': 0.27; 'set.': 0.27; 'then.': 0.27; 'interface': 0.27; "doesn't": 0.28; 'rest': 0.28; 'chris': 0.28; 'post': 0.28; 'container': 0.29; 'detector': 0.29; 'types.': 0.29; 'url:wikipedia': 0.29; 'included': 0.29; "skip:' 10": 0.30; 'unlike': 0.30; 'primary': 0.30; 'lists': 0.31; 'implement': 0.32; 'url:python': 0.32; 'structure': 0.32; 'could': 0.32; 'consist': 0.33; 'doubt': 0.33; 'instead,': 0.33; 'url:home': 0.33; 'problem': 0.33; 'themselves': 0.33; 'another': 0.33; 'agree': 0.34; 'changed': 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'list': 0.35; 'exist': 0.35; 'involving': 0.35; 'nature': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'created': 0.36; 'but': 0.36; 'url:org': 0.36; "didn't": 0.36; 'anything': 0.36; 'should': 0.36; 'possible': 0.37; 'beyond': 0.37; 'keeps': 0.37; 'does': 0.37; 'two': 0.37; 'uses': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'url:docs': 0.38; 'url:en': 0.38; 'release': 0.39; 'skip:" 10': 0.40; 'help': 0.40; 'your': 0.60; 'remove': 0.61; 'share': 0.61; 'first': 0.61; 'developed': 0.62; 'peace': 0.62; 'between': 0.63; 'url:%20': 0.63; 'skip:n 10': 0.63; 'url:png': 0.63; 'more': 0.63; 'visit': 0.64; 'charset:windows-1252': 0.65; 'august': 0.66; 'url:%1': 0.68; 'benefit': 0.70; 'disabled,': 0.84; 'it\x92s': 0.84; 'premise': 0.84; 'apparent': 0.91; 'reasoning': 0.91; 'shares': 0.91 Newsgroups: comp.lang.python Date: Thu, 23 Aug 2012 09:49:41 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=67.184.78.189; posting-account=MQ3pigoAAACeFzUFjVAePnOjOJMNlvq9 References: <7xy5le7cli.fsf@ruckus.brouhaha.com> <502dab6c$0$29978$c3e8da3$5496439d@news.astraweb.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 67.184.78.189 MIME-Version: 1.0 Subject: Re: set and dict iteration From: Aaron Brady To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 107 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345740590 news.xs4all.nl 6843 [2001:888:2000:d::a6]:54627 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27745 On Saturday, August 18, 2012 9:28:32 PM UTC-5, Aaron Brady wrote: > On Saturday, August 18, 2012 5:14:05 PM UTC-5, MRAB wrote: >=20 > > On 18/08/2012 21:29, Aaron Brady wrote: >=20 > > > On Friday, August 17, 2012 4:57:41 PM UTC-5, Chris Angelico wrote: >=20 > > >> On Sat, Aug 18, 2012 at 4:37 AM, Aaron Brady = wrote: >=20 > > >> > Is there a problem with hacking on the Beta? >=20 > > >> Nope. Hack on the beta, then when the release arrives, rebase your >=20 > > >> work onto it. I doubt that anything of this nature will be changed >=20 > > >> between now and then. >=20 > > >> ChrisA >=20 > > > Thanks Chris, your post was encouraging. >=20 > > > I have a question about involving the 'tp_clear' field of the types. >=20 > > > http://docs.python.org/dev/c-api/typeobj.html#PyTypeObject.tp_clear >=20 > > > ''' >=20 > > > ...The tuple type does not implement a tp_clear function, because it= =92s possible to prove that no reference cycle can be composed entirely of = tuples. >=20 > > > ''' >=20 > > > I didn't follow the reasoning in the proof; the premise is necessary = but IMHO not obviously sufficient. Nevertheless, the earlier diagram conta= ins an overt homogeneous reference cycle. >=20 > > > Reposting: http://home.comcast.net/~castironpi-misc/clpy-0062%20set%2= 0iterators.png >=20 > > > In my estimate, the 'tp_traverse' and 'tp_clear' fields of the set do= esn't need to visit the auxiliary collection; the same fields of the iterat= ors don't need to visit the primary set or other iterators; and references = in the linked list don't need to be included in the iterators' reference co= unts. >=20 > > > Can someone who is more familiar with the cycle detector and cycle br= eaker, help prove or disprove the above? >=20 > > In simple terms, when you create an immutable object it can contain >=20 > > only references to pre-existing objects, but in order to create a cycle >=20 > > you need to make an object refer to another which is created later, so >=20 > > it's not possible to create a cycle out of immutable objects. >=20 > > However, using Python's C API it _is_ possible to create such a cycle, >=20 > > by mutating an otherwise-immutable tuple (see PyTuple_SetItem and >=20 > > PyTuple_SET_ITEM). >=20 > Are there any precedents for storing uncounted references to PyObject's? >=20 > One apparent problematic case is creating an iterator to a set, then addi= ng it to the set. However the operation is a modification, and causes the = iterator to be removed from the secondary list before the set is examined f= or collection. >=20 > Otherwise, the iterator keeps a counted reference to the set, but the set= does not keep a counted reference to the iterator, so the iterator will al= ways be freed first. Therefore, the set's secondary list will be empty whe= n the set is freed. >=20 > Concurrent addition and deletion of iterators should be disabled, and the= iterators should remove themselves from the set's secondary list before th= ey decrement their references to the set. >=20 > Please refresh the earlier diagram; counted references are distinguished = separately. Reposting: http://home.comcast.net/~castironpi-misc/clpy-0062%= 20set%20iterators.png The patch for the above is only 40-60 lines. However it introduces two new= concepts. The first is a "linked list", a classic dynamic data structure, first devel= oped in 1955, cf. http://en.wikipedia.org/wiki/Linked_list . Linked lists = are absent in Python, including the standard library and CPython implementa= tion, beyond the weak reference mechanism and garbage collector. The "coll= ections.deque" structure shares some of the linked list interface but uses = arrays. The second is "uncounted references". The uncounted references are referen= ces to "set iterators" exclusively, exist only internally to "set" objects,= and are invisible to the rest of the program. The reason for the exceptio= n is that iterators are unique in the Python Data Model; iterators consist = of a single immutable reference, unlike both immutable types such as string= s and numbers, as well as container types. Counted references could be use= d instead, but would be consistently wasted work for the garbage collector,= though the benefit to programmers' peace of mind could be significant. Please share your opinion! Do you agree that the internal list resolves th= e inconsistency? Do you agree with the strategy? Do you agree that uncoun= ted references are justified to introduce, or are counted references prefer= able?