On 3/4/18 8:26 AM, Ooomzay wrote:
On Sunday, 4 March 2018 03:16:31 UTC, Paul Rubin  wrote:
Chris Angelico <ros...@gmail.com> writes:
Yep, cool. Now do that with all of your smart pointers being on the
heap too. You are not allowed to use ANY stack objects.  ANY. Got it?
That's both overconstraining and not even that big a problem the way you
phrase it.

1) Python has both the "with" statement and try/finally.  Both of these
run code at the exit from a syntactically defined scope.  So they are
like stack allocation in C++, where a deallocator can run when the scope
exits.

2) Even with no scope-based de-allocation, it's common to put smart
pointers into containers like lists and vectors.  So you could have a
unique_ptr to a filestream object, and stash the unique_ptr someplace as
a vector element, where the vector itself could be part of some even
more deeply nested structure.  At some point, the big structure gets
deleted (maybe through a manually-executed delete statement).  When that
happens, if the nested structures are all standard containers full of
unique_ptrs, the top-level finalizer will end up traversing the entire
tree and freeing up the file handles and whatever else might be in
there.

It occurs to me, maybe #2 above is closer to what the OP is really after
in Python.
Yep. C++ smart pointers are a good analogue to python references for
purposes of this PEP.

I guess it's doable, but refcounts don't seem like the right
way.
Well refcounts are definitely "doable": This is how the reference python 
implementation, CPython, currently manages to comply with this PEP and can
therefore be used for RAII.



Are you including cyclic references in your assertion that CPython behaves as you want?

--Ned.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to