<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/scipy/ticket/1501" rel="nofollow">http://projects.scipy.org/scipy/ticket/1501</a> on 2011-08-23 by trac user gertjan, assigned to unknown.</em></p>
<p dir="auto">I noticed that some of my objects were not destructed in time, and, tracking down the origin of the problem, narrowed in on the following import statement:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="class A:    
  def __init__( self ):
    import scipy
  def __del__( self ): 
    print 'DELETED'"><pre class="notranslate"><code class="notranslate">class A:    
  def __init__( self ):
    import scipy
  def __del__( self ): 
    print 'DELETED'
</code></pre></div>
<p dir="auto">The import is postponed to the first instantiation of A. This is something I do quite often if it is not trivial that A is going to be instantiated at all, or if importing the particular module takes a long time. This does not usually (and AFAIK should not) cause any trouble. In this case, however, it does:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="a = A()
print '1.'
del a
print '2.'

#1.
#2.
# DELETED"><pre class="notranslate"><code class="notranslate">a = A()
print '1.'
del a
print '2.'

#1.
#2.
# DELETED
</code></pre></div>
<p dir="auto">In (other) words the import statement causes the object to persist even when all references should have gone. I assume that this is unintentional, if perhaps not unknown, but I couldn't find earlier mention of it.</p>
<p dir="auto">Besides seeing this fixed I would be very interested to learn what causes this problem, as I cannot think of any plausible mechanism myself and I would like to understand the implications. Before delving into SciPy's enormous code base myself I thought better to turn here where one might have the answer ready.</p>
<p dir="auto">I'm running Python 2.7.1 with SciPy 0.8.0.</p>
<p dir="auto">Thanks!</p>
<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/scipy/ticket/1104" rel="nofollow">http://projects.scipy.org/scipy/ticket/1104</a> on 2010-01-28 by trac user stephaned, assigned to unknown.</em></p>
<p dir="auto">Hi,</p>
<p dir="auto">With the following code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="class App:
    def __init__(self):
        import scipy
        ##scipy.pkgload.parent_frame = None

    def __del__(self):
        print &quot;app deleted&quot;

app = App()
print &quot;Deleting app&quot;
del app
print &quot;Leaving Python&quot;"><pre class="notranslate"><code class="notranslate">class App:
    def __init__(self):
        import scipy
        ##scipy.pkgload.parent_frame = None

    def __del__(self):
        print "app deleted"

app = App()
print "Deleting app"
del app
print "Leaving Python"
</code></pre></div>
<p dir="auto">Here is the output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Deleting app
Leaving Python
app deleted     &lt;-- WRONG"><pre class="notranslate"><code class="notranslate">Deleting app
Leaving Python
app deleted     &lt;-- WRONG
</code></pre></div>
<p dir="auto">The app object is not deleted by "del app" but when Python leaves.</p>
<p dir="auto">This is because "app" has a refcount of 2.</p>
<p dir="auto">Going further, I found that "scipy.pkgload.parent_frame = None" solves the issue.</p>
<p dir="auto">I also tried to transform "scipy.pkgload.parent_frame" to a "weakref.proxy", but got "cannot create weak reference to 'frame' object".</p>
<p dir="auto">Cheers.<br>
Stephane</p>