From bc5975b56f6151ca13f35b9e99a68fbe31bf5f22 2013-10-29 16:14:15 From: Thomas Kluyver Date: 2013-10-29 16:14:15 Subject: [PATCH] Fix imports of builtins module --- diff --git a/IPython/config/loader.py b/IPython/config/loader.py index c528fa8..06cb397 100644 --- a/IPython/config/loader.py +++ b/IPython/config/loader.py @@ -23,7 +23,6 @@ Authors # Imports #----------------------------------------------------------------------------- -import __builtin__ as builtin_mod import argparse import copy import os @@ -33,6 +32,7 @@ import sys from IPython.utils.path import filefind, get_ipython_dir from IPython.utils import py3compat, warn from IPython.utils.encoding import DEFAULT_ENCODING +from IPython.utils.py3compat import builtin_mod from IPython.utils.traitlets import HasTraits, List, Any, TraitError #----------------------------------------------------------------------------- diff --git a/IPython/core/builtin_trap.py b/IPython/core/builtin_trap.py index ad0eb2d..0498423 100644 --- a/IPython/core/builtin_trap.py +++ b/IPython/core/builtin_trap.py @@ -18,10 +18,9 @@ Authors: # Imports #----------------------------------------------------------------------------- -import __builtin__ - from IPython.config.configurable import Configurable +from IPython.utils.py3compat import builtin_mod from IPython.utils.traitlets import Instance #----------------------------------------------------------------------------- @@ -78,7 +77,7 @@ class BuiltinTrap(Configurable): def add_builtin(self, key, value): """Add a builtin and save the original.""" - bdict = __builtin__.__dict__ + bdict = builtin_mod.__dict__ orig = bdict.get(key, BuiltinUndefined) if value is HideBuiltin: if orig is not BuiltinUndefined: #same as 'key in bdict' @@ -91,9 +90,9 @@ class BuiltinTrap(Configurable): def remove_builtin(self, key, orig): """Remove an added builtin and re-set the original.""" if orig is BuiltinUndefined: - del __builtin__.__dict__[key] + del builtin_mod.__dict__[key] else: - __builtin__.__dict__[key] = orig + builtin_mod.__dict__[key] = orig def activate(self): """Store ipython references in the __builtin__ namespace.""" diff --git a/IPython/core/completer.py b/IPython/core/completer.py index 663d858..9e9fa34 100644 --- a/IPython/core/completer.py +++ b/IPython/core/completer.py @@ -66,7 +66,6 @@ Notes: # Imports #----------------------------------------------------------------------------- -import __builtin__ import __main__ import glob import inspect @@ -83,6 +82,7 @@ from IPython.utils import generics from IPython.utils import io from IPython.utils.dir2 import dir2 from IPython.utils.process import arg_split +from IPython.utils.py3compat import builtin_mod from IPython.utils.traitlets import CBool, Enum #----------------------------------------------------------------------------- @@ -353,7 +353,7 @@ class Completer(Configurable): match_append = matches.append n = len(text) for lst in [keyword.kwlist, - __builtin__.__dict__.keys(), + builtin_mod.__dict__.keys(), self.namespace.keys(), self.global_namespace.keys()]: for word in lst: diff --git a/IPython/core/displayhook.py b/IPython/core/displayhook.py index 5ba0cfa..0ecfb3b 100644 --- a/IPython/core/displayhook.py +++ b/IPython/core/displayhook.py @@ -23,13 +23,12 @@ Authors: #----------------------------------------------------------------------------- from __future__ import print_function -import __builtin__ - import sys from IPython.config.configurable import Configurable from IPython.utils import io +from IPython.utils.py3compat import builtin_mod from IPython.utils.traitlets import Instance from IPython.utils.warn import warn @@ -90,7 +89,7 @@ class DisplayHook(Configurable): # If something injected a '_' variable in __builtin__, delete # ipython's automatic one so we don't clobber that. gettext() in # particular uses _, so we need to stay away from it. - if '_' in __builtin__.__dict__: + if '_' in builtin_mod.__dict__: try: del self.shell.user_ns['_'] except KeyError: @@ -206,7 +205,7 @@ class DisplayHook(Configurable): # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise # we cause buggy behavior for things like gettext). - if '_' not in __builtin__.__dict__: + if '_' not in builtin_mod.__dict__: self.___ = self.__ self.__ = self._ self._ = result @@ -270,7 +269,7 @@ class DisplayHook(Configurable): # Release our own references to objects: self._, self.__, self.___ = '', '', '' - if '_' not in __builtin__.__dict__: + if '_' not in builtin_mod.__dict__: self.shell.user_ns.update({'_':None,'__':None, '___':None}) import gc # TODO: Is this really needed? diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index a24b1f3..91c8343 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -17,7 +17,6 @@ from __future__ import absolute_import from __future__ import print_function -import __builtin__ as builtin_mod import __future__ import abc import ast @@ -69,6 +68,7 @@ from IPython.utils.ipstruct import Struct from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename from IPython.utils.pickleshare import PickleShareDB from IPython.utils.process import system, getoutput +from IPython.utils.py3compat import builtin_mod from IPython.utils.strdispatch import StrDispatch from IPython.utils.syspathcontext import prepended_to_syspath from IPython.utils.text import (format_screen, LSString, SList, diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index 75e16f2..e8ba0b6 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -15,7 +15,6 @@ from __future__ import print_function #----------------------------------------------------------------------------- # Stdlib -import __builtin__ as builtin_mod import ast import bdb import os @@ -44,6 +43,7 @@ from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, line_cell_magic, on_off, needs_local_scope) from IPython.testing.skipdoctest import skip_doctest from IPython.utils import py3compat +from IPython.utils.py3compat import builtin_mod from IPython.utils.contexts import preserve_keys from IPython.utils.io import capture_output from IPython.utils.ipstruct import Struct diff --git a/IPython/kernel/zmq/displayhook.py b/IPython/kernel/zmq/displayhook.py index 5ec9698..77f2002 100644 --- a/IPython/kernel/zmq/displayhook.py +++ b/IPython/kernel/zmq/displayhook.py @@ -1,9 +1,9 @@ -import __builtin__ import sys from IPython.core.displayhook import DisplayHook from IPython.kernel.inprocess.socket import SocketABC from IPython.utils.jsonutil import encode_images +from IPython.utils.py3compat import builtin_mod from IPython.utils.traitlets import Instance, Dict from .session import extract_header, Session @@ -21,7 +21,7 @@ class ZMQDisplayHook(object): if obj is None: return - __builtin__._ = obj + builtin_mod._ = obj sys.stdout.flush() sys.stderr.flush() msg = self.session.send(self.pub_socket, u'pyout', {u'data':repr(obj)}, diff --git a/IPython/kernel/zmq/ipkernel.py b/IPython/kernel/zmq/ipkernel.py index 0977f3f..3108c1d 100755 --- a/IPython/kernel/zmq/ipkernel.py +++ b/IPython/kernel/zmq/ipkernel.py @@ -7,7 +7,6 @@ from __future__ import print_function # Standard library imports -import __builtin__ import sys import time import traceback @@ -29,6 +28,7 @@ from IPython.config.configurable import Configurable from IPython.core.error import StdinNotImplementedError from IPython.core import release from IPython.utils import py3compat +from IPython.utils.py3compat import builtin_mod from IPython.utils.jsonutil import json_clean from IPython.utils.traitlets import ( Any, Instance, Float, Dict, List, Set, Integer, Unicode, @@ -356,13 +356,13 @@ class Kernel(Configurable): raw_input = input = lambda prompt='' : self._no_raw_input() if py3compat.PY3: - self._sys_raw_input = __builtin__.input - __builtin__.input = raw_input + self._sys_raw_input = builtin_mod.input + builtin_mod.input = raw_input else: - self._sys_raw_input = __builtin__.raw_input - self._sys_eval_input = __builtin__.input - __builtin__.raw_input = raw_input - __builtin__.input = input + self._sys_raw_input = builtin_mod.raw_input + self._sys_eval_input = builtin_mod.input + builtin_mod.raw_input = raw_input + builtin_mod.input = input # Set the parent message of the display hook and out streams. shell.set_parent(parent) @@ -392,10 +392,10 @@ class Kernel(Configurable): finally: # Restore raw_input. if py3compat.PY3: - __builtin__.input = self._sys_raw_input + builtin_mod.input = self._sys_raw_input else: - __builtin__.raw_input = self._sys_raw_input - __builtin__.input = self._sys_eval_input + builtin_mod.raw_input = self._sys_raw_input + builtin_mod.input = self._sys_eval_input reply_content[u'status'] = status diff --git a/IPython/lib/deepreload.py b/IPython/lib/deepreload.py index 4197166..e248f8c 100644 --- a/IPython/lib/deepreload.py +++ b/IPython/lib/deepreload.py @@ -25,7 +25,6 @@ from __future__ import print_function # the file COPYING, distributed as part of this software. #***************************************************************************** -import __builtin__ from contextlib import contextmanager import imp import sys @@ -33,16 +32,18 @@ import sys from types import ModuleType from warnings import warn -original_import = __builtin__.__import__ +from IPython.utils.py3compat import builtin_mod, builtin_mod_name + +original_import = builtin_mod.__import__ @contextmanager def replace_import_hook(new_import): - saved_import = __builtin__.__import__ - __builtin__.__import__ = new_import + saved_import = builtin_mod.__import__ + builtin_mod.__import__ = new_import try: yield finally: - __builtin__.__import__ = saved_import + builtin_mod.__import__ = saved_import def get_parent(globals, level): """ @@ -313,12 +314,12 @@ def deep_reload_hook(m): # Save the original hooks try: - original_reload = __builtin__.reload + original_reload = builtin_mod.reload except AttributeError: original_reload = imp.reload # Python 3 # Replacement for reload() -def reload(module, exclude=['sys', 'os.path', '__builtin__', '__main__']): +def reload(module, exclude=['sys', 'os.path', builtin_mod_name, '__main__']): """Recursively reload all modules used in the given module. Optionally takes a list of modules to exclude from reloading. The default exclude list contains sys, __main__, and __builtin__, to prevent, e.g., resetting @@ -335,4 +336,4 @@ def reload(module, exclude=['sys', 'os.path', '__builtin__', '__main__']): # Uncomment the following to automatically activate deep reloading whenever # this module is imported -#__builtin__.reload = reload +#builtin_mod.reload = reload diff --git a/IPython/lib/tests/test_deepreload.py b/IPython/lib/tests/test_deepreload.py index aa28dbf..9c1823a 100644 --- a/IPython/lib/tests/test_deepreload.py +++ b/IPython/lib/tests/test_deepreload.py @@ -10,6 +10,7 @@ import os import nose.tools as nt from IPython.testing import decorators as dec +from IPython.utils.py3compat import builtin_mod_name from IPython.utils.syspathcontext import prepended_to_syspath from IPython.utils.tempdir import TemporaryDirectory from IPython.lib.deepreload import reload as dreload @@ -24,7 +25,7 @@ def test_deepreload_numpy(): import numpy exclude = [ # Standard exclusions: - 'sys', 'os.path', '__builtin__', '__main__', + 'sys', 'os.path', builtin_mod_name, '__main__', # Test-related exclusions: 'unittest', 'UserDict', ] diff --git a/IPython/parallel/client/view.py b/IPython/parallel/client/view.py index aa8a007..cf28980 100644 --- a/IPython/parallel/client/view.py +++ b/IPython/parallel/client/view.py @@ -440,8 +440,8 @@ class DirectView(View): importing recarray from numpy on engine(s) """ - import __builtin__ - local_import = __builtin__.__import__ + from IPython.utils.py3compat import builtin_mod + local_import = builtin_mod.__import__ modules = set() results = [] @util.interactive @@ -463,8 +463,8 @@ class DirectView(View): locally as well. """ # don't override nested imports - save_import = __builtin__.__import__ - __builtin__.__import__ = local_import + save_import = builtin_mod.__import__ + builtin_mod.__import__ = local_import if imp.lock_held(): # this is a side-effect import, don't do it remotely, or even @@ -488,12 +488,12 @@ class DirectView(View): print("importing %s on engine(s)"%name) results.append(self.apply_async(remote_import, name, fromlist, level)) # restore override - __builtin__.__import__ = save_import + builtin_mod.__import__ = save_import return mod # override __import__ - __builtin__.__import__ = view_import + builtin_mod.__import__ = view_import try: # enter the block yield @@ -505,7 +505,7 @@ class DirectView(View): pass finally: # always restore __import__ - __builtin__.__import__ = local_import + builtin_mod.__import__ = local_import for r in results: # raise possible remote ImportErrors here diff --git a/IPython/testing/globalipapp.py b/IPython/testing/globalipapp.py index 861f575..172e53e 100644 --- a/IPython/testing/globalipapp.py +++ b/IPython/testing/globalipapp.py @@ -20,7 +20,6 @@ from __future__ import print_function #----------------------------------------------------------------------------- # stdlib -import __builtin__ as builtin_mod import os import sys @@ -30,6 +29,7 @@ from . import tools from IPython.core import page from IPython.utils import io from IPython.utils import py3compat +from IPython.utils.py3compat import builtin_mod from IPython.terminal.interactiveshell import TerminalInteractiveShell #----------------------------------------------------------------------------- diff --git a/IPython/testing/plugin/ipdoctest.py b/IPython/testing/plugin/ipdoctest.py index 3022b77..21fae2a 100644 --- a/IPython/testing/plugin/ipdoctest.py +++ b/IPython/testing/plugin/ipdoctest.py @@ -19,7 +19,6 @@ Limitations: # Module imports # From the standard library -import __builtin__ as builtin_mod import commands import doctest import inspect @@ -48,6 +47,7 @@ from nose.plugins import doctests, Plugin from nose.util import anyp, getpackage, test_address, resolve_name, tolist # Our own imports +from IPython.utils.py3compat import builtin_mod #----------------------------------------------------------------------------- # Module globals and other constants @@ -296,7 +296,7 @@ class DocTestCase(doctests.DocTestCase): # XXX - fperez: I am not sure if this is truly a bug in nose 0.11, but # it does look like one to me: its tearDown method tries to run # - # delattr(__builtin__, self._result_var) + # delattr(builtin_mod, self._result_var) # # without checking that the attribute really is there; it implicitly # assumes it should have been set via displayhook. But if the diff --git a/IPython/utils/py3compat.py b/IPython/utils/py3compat.py index a2bca3c..4cd3b39 100644 --- a/IPython/utils/py3compat.py +++ b/IPython/utils/py3compat.py @@ -1,6 +1,5 @@ # coding: utf-8 """Compatibility tricks for Python 3. Mainly to do with unicode.""" -import __builtin__ import functools import sys import re @@ -76,6 +75,7 @@ if sys.version_info[0] >= 3: input = input builtin_mod_name = "builtins" + import builtins as builtin_mod str_to_unicode = no_code unicode_to_str = no_code @@ -127,6 +127,7 @@ else: input = raw_input builtin_mod_name = "__builtin__" + import __builtin__ as builtin_mod str_to_unicode = decode unicode_to_str = encode @@ -189,7 +190,7 @@ else: # The rstrip() is necessary b/c trailing whitespace in files will # cause an IndentationError in Python 2.6 (this was fixed in 2.7, # but we still support 2.6). See issue 1027. - scripttext = __builtin__.open(fname).read().rstrip() + '\n' + scripttext = builtin_mod.open(fname).read().rstrip() + '\n' # compile converts unicode filename to str assuming # ascii. Let's do the conversion before calling compile if isinstance(fname, unicode): @@ -203,4 +204,4 @@ else: filename = fname.encode(sys.getfilesystemencoding()) else: filename = fname - __builtin__.execfile(filename, *where) + builtin_mod.execfile(filename, *where)