Skip Menu |

This queue is for tickets about the Inline-Python CPAN distribution.

Report information
The Basics
Id: 48280
Status: rejected
Priority: 0/
Queue: Inline-Python

People
Owner: Nobody in particular
Requestors: jspong [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.29
Fixed in: (no value)



Subject: Python destructors not called
Python destructors (object.__del__) don't seem to be executed by Inline::Python. If I have the following python script test.py: class Test(object): def __init__(self): print "Hi!" def __del__(self): print "Bye!" if __name__ == "__main__": t = Test() And the perl script test.pl: use strict; use warnings; use Inline::Python; Inline::Python::py_eval(<<'END'); from test import Test t = Test() END And I execute each one, you can see that executing python directly results in the destructor being called, but in perl, the destructor is never executed: -bash-3.00$ python test.py Hi! Bye! -bash-3.00$ perl test.pl Hi! -bash-3.00$
On Tue Jul 28 15:49:44 2009, https://www.google.com/accounts/o8/id?id=AItOawlexYTVVJ0Oz98YNS01DA5avM0engOM7NU wrote: Show quoted text
> Inline::Python::py_eval(<<'END'); > > from test import Test > > t = Test() > > END > > And I execute each one, you can see that executing python directly > results in the destructor being called, but in perl, the destructor
is Show quoted text
> never executed:
It seems like objects don't get destroyed if the variables are in the Python __main__ namespace. They do get destroyed (and the Python destructors executed) if they are inside functions, passed to perl, etc. So I'd think, that it's not so much a problem, since those objects would only get destroyed on program exit anyway. Do you need your destructors called in that case? If, I'll try to investigate, what's going on and if I can make it work.
From: jspong [...] gmail.com
On Wed Aug 05 08:15:11 2009, NINE wrote: Show quoted text
> On Tue Jul 28 15:49:44 2009, >
https://www.google.com/accounts/o8/id?id=AItOawlexYTVVJ0Oz98YNS01DA5avM0engOM7NU Show quoted text
> wrote:
> > Inline::Python::py_eval(<<'END'); > > > > from test import Test > > > > t = Test() > > > > END > > > > And I execute each one, you can see that executing python directly > > results in the destructor being called, but in perl, the destructor
> is
> > never executed:
> > It seems like objects don't get destroyed if the variables are in the > Python __main__ namespace. They do get destroyed (and the Python > destructors executed) if they are inside functions, passed to perl, > etc. So I'd think, that it's not so much a problem, since those > objects > would only get destroyed on program exit anyway. Do you need your > destructors called in that case? If, I'll try to investigate, what's > going on and if I can make it work.
The destructors are important to run to release external resources, but I don't necessarily need them to be in __main__. My issues must be occuring for another reason. I've verified that they do in fact get run if they cross into perl, which is where they're all handled in my code, so I personally don't really need this fixed. Thanks.
From: 'John Spong' <jspong [...] gmail.com>
On Wed Aug 05 14:10:26 2009, https://www.google.com/accounts/o8/id?id=AItOawlexYTVVJ0Oz98YNS01DA5avM0engOM7NU wrote: Show quoted text
> On Wed Aug 05 08:15:11 2009, NINE wrote:
> > On Tue Jul 28 15:49:44 2009, > >
>
https://www.google.com/accounts/o8/id?id=AItOawlexYTVVJ0Oz98YNS01DA5avM0engOM7NU Show quoted text
> > wrote:
> > > Inline::Python::py_eval(<<'END'); > > > > > > from test import Test > > > > > > t = Test() > > > > > > END > > > > > > And I execute each one, you can see that executing python directly > > > results in the destructor being called, but in perl, the
> destructor
> > is
> > > never executed:
> > > > It seems like objects don't get destroyed if the variables are in
> the
> > Python __main__ namespace. They do get destroyed (and the Python > > destructors executed) if they are inside functions, passed to perl, > > etc. So I'd think, that it's not so much a problem, since those > > objects > > would only get destroyed on program exit anyway. Do you need your > > destructors called in that case? If, I'll try to investigate, what's > > going on and if I can make it work.
> > The destructors are important to run to release external resources, > but > I don't necessarily need them to be in __main__. My issues must be > occuring for another reason. I've verified that they do in fact get > run > if they cross into perl, which is where they're all handled in my > code, > so I personally don't really need this fixed. Thanks.
I have verified that Inline::Python also does not handle cyclic garbage collection: ===== test.pl ===== use strict; use warnings; use Inline::Python; Inline::Python::eval_python("import destructor_test"); my $obj = Inline::Python::py_call_function("destructor_test", "get_object"); my $other = Inline::Python::py_call_function("destructor_test", "get_object", $obj); Inline::Python::py_call_method($obj, "attach", $other); ===== destructor_test.py ===== def get_object(other=None): c = Class() c.attach(other) return c class Class(object): def __del__(self): print "Destructor" def attach(self, other): self.other = other ============================== Running test.pl prints no output, therefore the python destructor is not getting called. If I remove the calls to "attach", both are deleted correctly.
I recently worked on fixing any memory leaks I could find in Inline::Python and learned a lot about the Python GC on the way. An important thing is: the garbage collector collects these (and for example closure call frames) not immediately, but at some arbitrary point when it feels like it. If your script terminates before that happens, Python does not call destructors. According to the docs, it's not guaranteed to run destructors on program exit and in fact it simply doesn't. So this is simply the way Python works and doesn't depend on Inline::Python.