[IPython-dev] Should IPython be a singleton?
Robert Kern
robert.kern at gmail.com
Wed Aug 26 19:27:00 EDT 2009
On 2009-08-26 18:17 PM, Brian Granger wrote:
>
> When tracking down problems like this, one thing you have to be very
> careful
> about is not creating references via your instrumentation. It's
> disturbingly
> easy to do. :-)
>
>
> Yes, it is very tough to do right.
>
> __builtins__.__IPYTHON__ and __builtins__.__IPYTHON__active are two
> culprits,
> and the remaining one is a cell object. I'm not sure where he is
> coming from.
>
>
> In my version, I have gotten rid of __IPYTHON__ and IPYTHON_active is an
> int.
>
> What is a cell object?
It is created when there are closures.
http://docs.python.org/c-api/cell.html
> Could I see the foo.py script?
Oops! Sorry. I thought I catted it. Here it is with a few more things cleaned up.
[~]$ cat foo.py
import gc
import sys
import types
from IPython.core.shell import InteractiveShell
real_main = sys.modules['__main__']
s = InteractiveShell('foo')
print s
# Clean up.
del s.shell
del s
del __builtins__.__IPYTHON__active
del __builtins__.__IPYTHON__
if hasattr(sys, 'ipcompleter'):
del sys.ipcompleter
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
sys.displayhook = sys.__displayhook__
sys.excepthook = sys.__excepthook__
sys.modules['__main__'] = real_main
for obj in gc.get_objects():
if getattr(type(obj), '__name__', None) == 'InteractiveShell':
print obj
refs = gc.get_referrers(obj)
print '%s references:' % len(refs)
for ref in refs:
print type(ref)
if isinstance(ref, dict):
print 'A dict with keys: %r' % (ref.keys(),)
elif isinstance(ref, list):
print 'A list of %s elements; probably gc.get_objects().' %
len(ref)
elif type(ref).__name__ == 'cell':
print 'A cell: %r' % ref
print ref.cell_contents
else:
print ref
print
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the IPython-dev
mailing list