RE: does python have useless destructors?

Discussion in 'Python' started by Delaney, Timothy C (Timothy), Jun 15, 2004.

  1. David Turner wrote:

    > This will be a pain for the Jython implementers. However, it is
    > doable. Note that I never said the objects couldn't be garbage
    > collected, just that __del__ had to be called at certain well-defined
    > times. What this will involve is the Jython compiler inserting a lot
    > of implicit try/finally constructs.
    >
    > Can anyone see a reason why this scheme wouldn't work?


    Yes - Jython cannot know about references to objects created *in Java
    code*. It is therefore impossible for Jython to maintain reference
    counts when an object is passed to Java (i.e. non-Python) code.

    Tim Delaney
     
    Delaney, Timothy C (Timothy), Jun 15, 2004
    #1
    1. Advertising

  2. Delaney, Timothy C (Timothy)

    David Turner Guest

    "Delaney, Timothy C (Timothy)" <> wrote in message news:<>...
    > David Turner wrote:
    >
    > > This will be a pain for the Jython implementers. However, it is
    > > doable. Note that I never said the objects couldn't be garbage
    > > collected, just that del had to be called at certain well-defined
    > > times. What this will involve is the Jython compiler inserting a lot
    > > of implicit try/finally constructs.
    > >
    > > Can anyone see a reason why this scheme wouldn't work?

    >
    > Yes - Jython cannot know about references to objects created *in Java
    > code*. It is therefore impossible for Jython to maintain reference
    > counts when an object is passed to Java (i.e. non-Python) code.
    >


    Thank you, Tim, that's the first substantial objection I've heard :).

    So, if Java code makes use of a Python object with __del__, all bets
    are off. This is certainly a limitation, but I'm not sure that it's a
    very serious one, as RAII objects tend to be used in localized
    contexts.

    I think the only available behaviour is simply to ignore the
    possibility of an object being referenced externally. Let Java code
    use Java idioms (try/finally).

    Regards
    David Turner
     
    David Turner, Jun 15, 2004
    #2
    1. Advertising

  3. Delaney, Timothy C (Timothy)

    Ype Kingma Guest

    David Turner wrote:

    > "Delaney, Timothy C (Timothy)" <> wrote in message

    news:<>...
    >> David Turner wrote:
    >>
    >> > This will be a pain for the Jython implementers. However, it is
    >> > doable. Note that I never said the objects couldn't be garbage
    >> > collected, just that del had to be called at certain well-defined
    >> > times. What this will involve is the Jython compiler inserting a lot
    >> > of implicit try/finally constructs.
    >> >
    >> > Can anyone see a reason why this scheme wouldn't work?

    >>
    >> Yes - Jython cannot know about references to objects created *in Java
    >> code*. It is therefore impossible for Jython to maintain reference
    >> counts when an object is passed to Java (i.e. non-Python) code.
    >>

    >
    > Thank you, Tim, that's the first substantial objection I've heard :).
    >
    > So, if Java code makes use of a Python object with __del__, all bets
    > are off. This is certainly a limitation, but I'm not sure that it's a
    > very serious one, as RAII objects tend to be used in localized
    > contexts.


    It's not that all bets are off. In Jython the __del__() method is called
    from java.lang.Object.finalize(), so you can place your bets there:
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize()

    This javadoc begins with:
    "Called by the garbage collector on an object when garbage collection
    determines that there are no more references to the object."

    The closest thing to a guarantee is to explicitly call the garbage
    collector:
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#gc()

    From Jython:

    import java
    java.lang.System.gc()

    I wouldn't bet against this, ie. the last time I tried the first call
    to this gc() called all __del__() methods instantly on non referenced
    Python objects.

    >
    > I think the only available behaviour is simply to ignore the
    > possibility of an object being referenced externally. Let Java code
    > use Java idioms (try/finally).


    For the moment, try/finally should also be a CPython idiom because
    the call to __del__() is not guaranteed there either.

    Kind regards,
    Ype


    --
    email at xs4all.nl
     
    Ype Kingma, Jun 15, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael P. Soulier

    does python have useless destructors?

    Michael P. Soulier, Jun 9, 2004, in forum: Python
    Replies:
    138
    Views:
    2,220
    Christos TZOTZIOY Georgiou
    Jun 22, 2004
  2. Delaney, Timothy C (Timothy)

    RE: does python have useless destructors?

    Delaney, Timothy C (Timothy), Jun 10, 2004, in forum: Python
    Replies:
    4
    Views:
    254
    Tim Bradshaw
    Jun 14, 2004
  3. Robert Brewer

    RE: does python have useless destructors?

    Robert Brewer, Jun 10, 2004, in forum: Python
    Replies:
    1
    Views:
    547
    Duncan Booth
    Jun 10, 2004
  4. Tim Peters
    Replies:
    2
    Views:
    309
    Paul Rubin
    Jun 12, 2004
  5. Tim Peters
    Replies:
    7
    Views:
    3,592
    David Turner
    Jun 18, 2004
Loading...

Share This Page