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

SciPy Trac scipy-tickets@scipy....
Tue Aug 23 13:27:40 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  |  

Comment(by gertjan):

 To answer your question first, the code provided is simply the minimum at
 which the problem occurs. Practically, in my application, a member
 function imports scipy.sparse and constructs a csr_matrix, which is then
 returned; no further reference to scipy required. Because this function
 would not in all situations be called it was simply an optimization to
 postpone import to the first call. Indeed the problem is easily fixed by
 moving the import statement out of the class, but even then, if this is
 unexpected behaviour it is something other people can trip over as well.
 Besides, I would just like to understand what is going on.

 I noticed the PackageLoader in `__init__.py` but that is precisely as far
 as I went, when I decided to turn here instead. I believe I have a good
 understanding of how (default, non-cyclic) garbage collection works, and
 the notes and warnings in the linked document do not make me believe
 otherwise. The thing that is beyond me is how a reference to A() ends up
 being held by the scipy module (which.. what else can keep it alive?) when
 there is no obvious way to get hold of it. The only mechanism I can think
 of is that the encompassing frame is somehow taken from sys, but.. why?

 Indeed, perhaps this will be soved with the (as I understand) imminent
 disappearance of the PackageLoader. Then still, you can only be really
 sure that a bug is gone after you what it was. ;) Maybe I'll do some
 digging after all, when I find time.

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

More information about the Scipy-tickets mailing list