On Mon, Apr 10, 2017 at 5:57 AM, Clemens Hofreither <chofreit...@gmail.com> wrote: > > On Sun, Apr 9, 2017 at 8:00 AM, Robert Bradshaw <rober...@gmail.com> wrote: > > On Sun, Apr 2, 2017 at 9:42 AM, Clemens Hofreither <chofreit...@gmail.com> > > wrote: > >> > >> Hey all, > >> > >> (I hope this list isn't dead.) > >> > >> I'm reading up on the Cython codebase because I'm trying to implement > >> a small feature (more on that later). There's one thing that really > >> confuses me currently, and that's the distinction between > >> generate_post_assignment_code and generate_disposal_code. From the > >> comments, it appears that exactly one of these should be called, and > >> which one it is depends on whether a reference from the rhs was > >> absorbed into the lhs. > >> > >> So much for the theory, but in practice it seems to work differently. > >> I'm looking at NameNode.generate_assignment_code() and > >> IndexNode.generate_assignment_code(), which should do similar things > >> (generate code for an assignment) except that the lhs has different > >> structure. > >> > >> Yet they treat the cleanup of the rhs completely differently. > >> IndexNode always calls rhs.generate_disposal_code. NameNode almost > >> always calls rhs.generate_post_assignment_code. > >> > >> Can anyone shed light on why this cleanup is handled differently > >> depending on the target of the assignment? > > > > > > It's been a while since I've looked at this, but the best explanation is > > that generate_post_assignment_code is used when the rhs is "moved" into the > > lhs whereas generate_post_assignment_code is what happens for a "copy." This > > is mostly relevant for temps, e.g. when a temp holds a reference to a Python > > object, it can be transferred to the assignee (e.g. with NameNodes) or a new > > reference can be created for the assignee (e.g. with IndexNodes) which > > changes whether the temp lhs needs to be decref'ed. > > > > _______________________________________________ > > cython-devel mailing list > > cython-devel@python.org > > https://mail.python.org/mailman/listinfo/cython-devel > > > > Thanks for your answer. I understood as much about the difference with > temporaries and copies. > > My question was why the RIGHT-hand side is treated differently in > NameNode.generate_assignment_code() and > IndexNode.generate_assignment_code(). My understanding is that > NameNode/IndexNode here refers to the type of the left-hand side. But > I don't see why whether or not the RHS is a temporary should depend on > the type of the LHS. Therefore it seems confusing that the code in > these two methods uses different cleanup for the RHS.
One can generally "transfer" ownership to a NameNode, but this is not the case with an IndexNode (as it depends on what the [] operator was overloaded to do). One could always incref the NameNode and use generate_disposal_code, but using generate_post_assignment_code saves an unnecessary incref/decref pair. - Robert _______________________________________________ cython-devel mailing list cython-devel@python.org https://mail.python.org/mailman/listinfo/cython-devel