On 10/03/2018 10:50 PM, Christian Ferrari wrote:
[snip]
 > I guess the capsule is meant to be consumed by the other API and using a
 > method makes is explicit that a new object is instantiated every time. I
 > really don't care about this, let's do whatever is more Pythonic.

Dear Daniele & Federico,
I'm not a "pythonist" at all, I can't provide suggestions about "Python style". In oo context, typically I expect to call a method when I need to retrieve something, that's why I proposed a method instead of a property. Anyway, my obscure question was related to garbage collection: who is in charge of destroying the capsule and how it must be destroyed?
Looking at this example:

pgconn = xx._get_pgconn()
do_something(pgconn)
pgconn = yy._get_pgconn()

will the first capsule be automatically destroyed by the Python runtime when pgconn is assigned to another one (yy....)? Or some explicit statement must be executed? If everything is automatically managed by the Python runtime, I will be very happy. If "do_something()" must release the capsule to avoid memory leaks, I have to implement it.

The capsule is a standard reference-counted Python object. If the API just use the pointer inside it and does not keep a reference to the Python object itself, it will be free'd by the gc when the reference count goes to 0. In your example, the first capsule is garbage collected when you assign the second one to the same variable "pgconn".

@Daniele, I see that a capsule has an implicit state, "invalid" if the pointer inside it is null. Does it makes sense to reuse the same capsule and keep a reference to it to be able to invalidate it when the connection is closed?

federico

--
Federico Di Gregorio                         federico.digrego...@dndg.it
DNDG srl                                                  http://dndg.it
 The only thing I see is if you are pumping so much data into the
  database all the time when do you expect to look at it?
                                                        -- Charlie Clark

Reply via email to