Permalink
Browse files

Merge pull request #1059 from fperez/__IPYTHON__

Switch to simple `__IPYTHON__` global to indicate an IPython Shell object has been created.  Note that this does *not* try to track whether user code is being executed by ipython via %run, nor whether the Shell object itself is running an interactive event loop or not.

So the answer for how people should query whether IPython objects are active is now simply

```
try:
  __IPYTHON__
except NameError:
  print 'not in IPython'
```

We do not attempt to track activity levels anymore, as we realized that logic was ultimately to brittle and error prone to be of any real use.
  • Loading branch information...
2 parents 762b0ea + 7bd8fa3 commit 487b6b96e5d72a641ea66701210f17b7f895c966 @fperez fperez committed Dec 6, 2011
View
28 IPython/core/builtin_trap.py
@@ -88,17 +88,12 @@ def add_builtin(self, key, value):
self._orig_builtins[key] = orig
bdict[key] = value
- def remove_builtin(self, key):
+ def remove_builtin(self, key, orig):
"""Remove an added builtin and re-set the original."""
- try:
- orig = self._orig_builtins.pop(key)
- except KeyError:
- pass
+ if orig is BuiltinUndefined:
+ del __builtin__.__dict__[key]
else:
- if orig is BuiltinUndefined:
- del __builtin__.__dict__[key]
- else:
- __builtin__.__dict__[key] = orig
+ __builtin__.__dict__[key] = orig
def activate(self):
"""Store ipython references in the __builtin__ namespace."""
@@ -107,22 +102,11 @@ def activate(self):
for name, func in self.auto_builtins.iteritems():
add_builtin(name, func)
- # Keep in the builtins a flag for when IPython is active. We set it
- # with setdefault so that multiple nested IPythons don't clobber one
- # another.
- __builtin__.__dict__.setdefault('__IPYTHON__active', 0)
-
def deactivate(self):
"""Remove any builtins which might have been added by add_builtins, or
restore overwritten ones to their previous values."""
- # Note: must iterate over a static keys() list because we'll be
- # mutating the dict itself
remove_builtin = self.remove_builtin
- for key in self._orig_builtins.keys():
- remove_builtin(key)
+ for key, val in self._orig_builtins.iteritems():
+ remove_builtin(key, val)
self._orig_builtins.clear()
self._builtins_added = False
- try:
- del __builtin__.__dict__['__IPYTHON__active']
- except KeyError:
- pass
View
14 IPython/core/interactiveshell.py
@@ -605,6 +605,20 @@ def init_logstart(self):
self.magic_logstart()
def init_builtins(self):
+ # A single, static flag that we set to True. Its presence indicates
+ # that an IPython shell has been created, and we make no attempts at
+ # removing on exit or representing the existence of more than one
+ # IPython at a time.
+ builtin_mod.__dict__['__IPYTHON__'] = True
+
+ # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to
+ # manage on enter/exit, but with all our shells it's virtually
+ # impossible to get all the cases right. We're leaving the name in for
+ # those who adapted their codes to check for this flag, but will
+ # eventually remove it after a few more releases.
+ builtin_mod.__dict__['__IPYTHON__active'] = \
+ 'Deprecated, check for __IPYTHON__'
+
self.builtin_trap = BuiltinTrap(shell=self)
def init_inspector(self):
View
5 IPython/core/tests/test_interactiveshell.py
@@ -271,3 +271,8 @@ def test_1(self):
"""
cmd = ur'''python -c "'åäö'" '''
_ip.shell.system_raw(cmd)
+
+
+def test__IPYTHON__():
+ # This shouldn't raise a NameError, that's all
+ __IPYTHON__
View
6 IPython/frontend/terminal/interactiveshell.py
@@ -341,9 +341,6 @@ def interact(self, display_banner=None):
more = False
- # Mark activity in the builtins
- __builtin__.__dict__['__IPYTHON__active'] += 1
-
if self.has_readline:
self.readline_startup_hook(self.pre_readline)
hlen_b4_cell = self.readline.get_current_history_length()
@@ -413,9 +410,6 @@ def interact(self, display_banner=None):
hlen_b4_cell = \
self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
- # We are off again...
- __builtin__.__dict__['__IPYTHON__active'] -= 1
-
# Turn off the exit flag, so the mainloop can be restarted if desired
self.exit_now = False

0 comments on commit 487b6b9

Please sign in to comment.