Skip to content

Commit

Permalink
Merge pull request #1059 from fperez/__IPYTHON__
Browse files Browse the repository at this point in the history
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
fperez committed Dec 6, 2011
2 parents 762b0ea + 7bd8fa3 commit 487b6b9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
28 changes: 6 additions & 22 deletions IPython/core/builtin_trap.py
Expand Up @@ -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."""
Expand All @@ -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
14 changes: 14 additions & 0 deletions IPython/core/interactiveshell.py
Expand Up @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions IPython/core/tests/test_interactiveshell.py
Expand Up @@ -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__
6 changes: 0 additions & 6 deletions IPython/frontend/terminal/interactiveshell.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 487b6b9

Please sign in to comment.