[Scipy-tickets] [SciPy] #1501: import in member function causes object to persist

SciPy Trac scipy-tickets@scipy....
Tue Aug 23 06:42:43 CDT 2011

#1501: import in member function causes object to persist
 Reporter:  gertjan                     |       Owner:  somebody   
     Type:  defect                      |      Status:  new        
 Priority:  normal                      |   Milestone:  Unscheduled
Component:  Other                       |     Version:  0.8.0      
 Keywords:  import, garbage collection  |  
 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:

 class A:
   def __init__( self ):
     import scipy
   def __del__( self ):
     print 'DELETED'

 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:

 a = A()
 print '1.'
 del a
 print '2.'

 # 1.
 # 2.

 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.

 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.

 I'm running Python 2.7.1 with SciPy 0.8.0.


Ticket URL: <http://projects.scipy.org/scipy/ticket/1501>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.

More information about the Scipy-tickets mailing list