diff --git a/IPython/core/debugger.py b/IPython/core/debugger.py index 9472ec3..587c2c7 100644 --- a/IPython/core/debugger.py +++ b/IPython/core/debugger.py @@ -146,7 +146,7 @@ class Tracer(object): # at least raise that limit to 80 chars, which should be enough for # most interactive uses. try: - from repr import aRepr + from reprlib import aRepr aRepr.maxstring = 80 except: # This is only a user-facing convenience, so any error we encounter @@ -331,7 +331,7 @@ class Pdb(OldPdb): # vds: << def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3): - import repr + import reprlib ret = [] @@ -349,7 +349,7 @@ class Pdb(OldPdb): if '__return__' in frame.f_locals: rv = frame.f_locals['__return__'] #return_value += '->' - return_value += repr.repr(rv) + '\n' + return_value += reprlib.repr(rv) + '\n' ret.append(return_value) #s = filename + '(' + `lineno` + ')' @@ -364,7 +364,7 @@ class Pdb(OldPdb): call = '' if func != '?': if '__args__' in frame.f_locals: - args = repr.repr(frame.f_locals['__args__']) + args = reprlib.repr(frame.f_locals['__args__']) else: args = '()' call = tpl_call % (func, args) diff --git a/IPython/core/extensions.py b/IPython/core/extensions.py index ff4da75..b14851c 100644 --- a/IPython/core/extensions.py +++ b/IPython/core/extensions.py @@ -170,7 +170,10 @@ class ExtensionManager(Configurable): copy = copyfile else: from urllib import urlretrieve # Deferred imports - from urlparse import urlparse + try: + from urllib.parse import urlparse # Py3 + except ImportError: + from urlparse import urlparse src_filename = urlparse(url).path.split('/')[-1] copy = urlretrieve diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py index 6a2b43d..49edc10 100644 --- a/IPython/core/formatters.py +++ b/IPython/core/formatters.py @@ -28,7 +28,7 @@ import abc import sys import warnings # We must use StringIO, as cStringIO doesn't handle unicode properly. -from StringIO import StringIO +from io import StringIO # Our own imports from IPython.config.configurable import Configurable diff --git a/IPython/core/inputtransformer.py b/IPython/core/inputtransformer.py index 7582d65..56fbb5d 100644 --- a/IPython/core/inputtransformer.py +++ b/IPython/core/inputtransformer.py @@ -1,7 +1,7 @@ import abc import functools import re -from StringIO import StringIO +from io import StringIO from IPython.core.splitinput import LineInfo from IPython.utils import tokenize2 diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index e8ba0b6..bd6b048 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -20,7 +20,7 @@ import bdb import os import sys import time -from StringIO import StringIO +from io import StringIO # cProfile was added in Python2.5 try: diff --git a/IPython/core/tests/test_debugger.py b/IPython/core/tests/test_debugger.py index 4f69112..2d36a22 100644 --- a/IPython/core/tests/test_debugger.py +++ b/IPython/core/tests/test_debugger.py @@ -58,7 +58,7 @@ class PdbTestInput(object): #----------------------------------------------------------------------------- def test_longer_repr(): - from repr import repr as trepr + from reprlib import repr as trepr a = '1234567890'* 7 ar = "'1234567890123456789012345678901234567890123456789012345678901234567890'" diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index 5446e1a..3b7acfd 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -28,7 +28,7 @@ import sys import tempfile import unittest from os.path import join -from StringIO import StringIO +from io import StringIO # third-party import nose.tools as nt @@ -155,7 +155,7 @@ class InteractiveShellTestCase(unittest.TestCase): " list.__init__(self,x)")) ip.run_cell("w=Mylist([1,2,3])") - from cPickle import dumps + from pickle import dumps # We need to swap in our main module - this is only necessary # inside the test framework, because IPython puts the interactive module diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index c00fec1..ae0cf0e 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -12,7 +12,7 @@ from __future__ import absolute_import import io import os import sys -from StringIO import StringIO +from io import StringIO from unittest import TestCase try: diff --git a/IPython/core/tests/test_magic_terminal.py b/IPython/core/tests/test_magic_terminal.py index 963f04e..464324d 100644 --- a/IPython/core/tests/test_magic_terminal.py +++ b/IPython/core/tests/test_magic_terminal.py @@ -9,7 +9,7 @@ from __future__ import absolute_import #----------------------------------------------------------------------------- import sys -from StringIO import StringIO +from io import StringIO from unittest import TestCase import nose.tools as nt diff --git a/IPython/extensions/tests/test_autoreload.py b/IPython/extensions/tests/test_autoreload.py index 5f71559..aac6ee9 100644 --- a/IPython/extensions/tests/test_autoreload.py +++ b/IPython/extensions/tests/test_autoreload.py @@ -18,7 +18,7 @@ import tempfile import shutil import random import time -from StringIO import StringIO +from io import StringIO import nose.tools as nt import IPython.testing.tools as tt diff --git a/IPython/extensions/tests/test_rmagic.py b/IPython/extensions/tests/test_rmagic.py index a93b6a7..55817b2 100644 --- a/IPython/extensions/tests/test_rmagic.py +++ b/IPython/extensions/tests/test_rmagic.py @@ -1,4 +1,4 @@ -from StringIO import StringIO +from io import StringIO import numpy as np from IPython.testing.decorators import skip_without diff --git a/IPython/html/base/zmqhandlers.py b/IPython/html/base/zmqhandlers.py index a552ba9..0d4c95a 100644 --- a/IPython/html/base/zmqhandlers.py +++ b/IPython/html/base/zmqhandlers.py @@ -16,7 +16,10 @@ Authors: # Imports #----------------------------------------------------------------------------- -import Cookie +try: + from http.cookies import SimpleCookie # Py 3 +except ImportError: + from Cookie import SimpleCookie # Py 2 import logging from tornado import web from tornado import websocket @@ -102,7 +105,7 @@ class AuthenticatedZMQStreamHandler(ZMQStreamHandler, IPythonHandler): logging.error("First ws message didn't have the form 'identity:[cookie]' - %r", msg) try: - self.request._cookies = Cookie.SimpleCookie(msg) + self.request._cookies = SimpleCookie(msg) except: self.log.warn("couldn't parse cookie string: %s",msg, exc_info=True) diff --git a/IPython/kernel/blocking/channels.py b/IPython/kernel/blocking/channels.py index 6eaceda..e525019 100644 --- a/IPython/kernel/blocking/channels.py +++ b/IPython/kernel/blocking/channels.py @@ -13,7 +13,10 @@ Useful for test suites and blocking terminal interfaces. # Imports #----------------------------------------------------------------------------- -import Queue +try: + from queue import Queue, Empty # Py 3 +except ImportError: + from Queue import Queue, Empty # Py 2 from IPython.kernel.channels import IOPubChannel, HBChannel, \ ShellChannel, StdInChannel @@ -27,7 +30,7 @@ class BlockingChannelMixin(object): def __init__(self, *args, **kwds): super(BlockingChannelMixin, self).__init__(*args, **kwds) - self._in_queue = Queue.Queue() + self._in_queue = Queue() def call_handlers(self, msg): self._in_queue.put(msg) @@ -46,7 +49,7 @@ class BlockingChannelMixin(object): while True: try: msgs.append(self.get_msg(block=False)) - except Queue.Empty: + except Empty: break return msgs diff --git a/IPython/kernel/inprocess/socket.py b/IPython/kernel/inprocess/socket.py index 464b327..6196829 100644 --- a/IPython/kernel/inprocess/socket.py +++ b/IPython/kernel/inprocess/socket.py @@ -13,7 +13,10 @@ # Standard library imports. import abc -import Queue +try: + from queue import Queue # Py 3 +except ImportError: + from Queue import Queue # Py 2 # System library imports. import zmq @@ -45,7 +48,7 @@ SocketABC.register(zmq.Socket) class DummySocket(HasTraits): """ A dummy socket implementing (part of) the zmq.Socket interface. """ - queue = Instance(Queue.Queue, ()) + queue = Instance(Queue, ()) message_sent = Int(0) # Should be an Event #------------------------------------------------------------------------- diff --git a/IPython/kernel/inprocess/tests/test_kernel.py b/IPython/kernel/inprocess/tests/test_kernel.py index 091886a..cdcdbf9 100644 --- a/IPython/kernel/inprocess/tests/test_kernel.py +++ b/IPython/kernel/inprocess/tests/test_kernel.py @@ -11,7 +11,7 @@ from __future__ import print_function # Standard library imports -from StringIO import StringIO +from io import StringIO import sys import unittest diff --git a/IPython/kernel/tests/test_message_spec.py b/IPython/kernel/tests/test_message_spec.py index 6a0503a..9cf6325 100644 --- a/IPython/kernel/tests/test_message_spec.py +++ b/IPython/kernel/tests/test_message_spec.py @@ -9,7 +9,10 @@ import re from subprocess import PIPE -from Queue import Empty +try: + from queue import Empty # Py 3 +except ImportError: + from Queue import Empty # Py 2 import nose.tools as nt diff --git a/IPython/kernel/tests/utils.py b/IPython/kernel/tests/utils.py index affda47..1d0b9ab 100644 --- a/IPython/kernel/tests/utils.py +++ b/IPython/kernel/tests/utils.py @@ -15,7 +15,10 @@ import atexit from contextlib import contextmanager from subprocess import PIPE, STDOUT -from Queue import Empty +try: + from queue import Empty # Py 3 +except ImportError: + from Queue import Empty # Py 2 import nose import nose.tools as nt diff --git a/IPython/kernel/zmq/eventloops.py b/IPython/kernel/zmq/eventloops.py index 70da2ab..0335075 100644 --- a/IPython/kernel/zmq/eventloops.py +++ b/IPython/kernel/zmq/eventloops.py @@ -92,14 +92,17 @@ def loop_wx(kernel): def loop_tk(kernel): """Start a kernel with the Tk event loop.""" - import Tkinter + try: + from tkinter import Tk # Py 3 + except ImportError: + from Tkinter import Tk # Py 2 doi = kernel.do_one_iteration # Tk uses milliseconds poll_interval = int(1000*kernel._poll_interval) # For Tkinter, we create a Tk object and call its withdraw method. class Timer(object): def __init__(self, func): - self.app = Tkinter.Tk() + self.app = Tk() self.app.withdraw() self.func = func diff --git a/IPython/kernel/zmq/parentpoller.py b/IPython/kernel/zmq/parentpoller.py index 7a6e31d..7e38c34 100644 --- a/IPython/kernel/zmq/parentpoller.py +++ b/IPython/kernel/zmq/parentpoller.py @@ -6,7 +6,10 @@ except: import os import platform import time -from thread import interrupt_main +try: + from _thread import interrupt_main # Py 3 +except ImportError: + from thread import interrupt_main # Py 2 from threading import Thread from IPython.utils.warn import warn diff --git a/IPython/lib/clipboard.py b/IPython/lib/clipboard.py index cb3dffd..4dc6e45 100644 --- a/IPython/lib/clipboard.py +++ b/IPython/lib/clipboard.py @@ -41,11 +41,14 @@ def tkinter_clipboard_get(): implementation that uses that toolkit. """ try: - import Tkinter + from tkinter import Tk # Py 3 except ImportError: - raise TryNext("Getting text from the clipboard on this platform " - "requires Tkinter.") - root = Tkinter.Tk() + try: + from Tkinter import Tk # Py 2 + except ImportError: + raise TryNext("Getting text from the clipboard on this platform " + "requires Tkinter.") + root = Tk() root.withdraw() text = root.clipboard_get() root.destroy() diff --git a/IPython/lib/inputhook.py b/IPython/lib/inputhook.py index 5ae158c..bdb7be3 100644 --- a/IPython/lib/inputhook.py +++ b/IPython/lib/inputhook.py @@ -320,8 +320,8 @@ class InputHookManager(object): """ self._current_gui = GUI_TK if app is None: - import Tkinter - app = Tkinter.Tk() + import tkinter + app = tkinter.Tk() app.withdraw() self._apps[GUI_TK] = app return app diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index e4bb727..0e574bc 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -109,7 +109,7 @@ import sys import types import re import datetime -from StringIO import StringIO +from io import StringIO from collections import deque diff --git a/IPython/lib/tests/test_irunner.py b/IPython/lib/tests/test_irunner.py index d3d0e88..527d5fb 100644 --- a/IPython/lib/tests/test_irunner.py +++ b/IPython/lib/tests/test_irunner.py @@ -8,7 +8,7 @@ from __future__ import print_function VERBOSE = True # stdlib imports -import StringIO +import io import sys import unittest @@ -20,7 +20,7 @@ from IPython.utils.py3compat import doctest_refactor_print class RunnerTestCase(unittest.TestCase): def setUp(self): - self.out = StringIO.StringIO() + self.out = io.StringIO() #self.out = sys.stdout def _test_runner(self,runner,source,output): diff --git a/IPython/lib/tests/test_irunner_pylab_magic.py b/IPython/lib/tests/test_irunner_pylab_magic.py index dc7740d..31d4f02 100644 --- a/IPython/lib/tests/test_irunner_pylab_magic.py +++ b/IPython/lib/tests/test_irunner_pylab_magic.py @@ -7,7 +7,7 @@ from __future__ import print_function VERBOSE = True # stdlib imports -import StringIO +import io import sys import unittest import re @@ -28,7 +28,7 @@ def pylab_not_importable(): class RunnerTestCase(unittest.TestCase): def setUp(self): - self.out = StringIO.StringIO() + self.out = io.StringIO() #self.out = sys.stdout def _test_runner(self,runner,source,output): diff --git a/IPython/nbconvert/writers/tests/test_debug.py b/IPython/nbconvert/writers/tests/test_debug.py index a58552a..08ab03e 100644 --- a/IPython/nbconvert/writers/tests/test_debug.py +++ b/IPython/nbconvert/writers/tests/test_debug.py @@ -15,7 +15,7 @@ Module with tests for debug #----------------------------------------------------------------------------- import sys -from StringIO import StringIO +from io import StringIO from ...tests.base import TestsBase from ..debug import DebugWriter diff --git a/IPython/nbconvert/writers/tests/test_files.py b/IPython/nbconvert/writers/tests/test_files.py index ba3e7f9..02296b8 100644 --- a/IPython/nbconvert/writers/tests/test_files.py +++ b/IPython/nbconvert/writers/tests/test_files.py @@ -16,7 +16,7 @@ Module with tests for files import sys import os -from StringIO import StringIO +from io import StringIO from ...tests.base import TestsBase from ..files import FilesWriter diff --git a/IPython/nbconvert/writers/tests/test_stdout.py b/IPython/nbconvert/writers/tests/test_stdout.py index 648cf02..4d97f9e 100644 --- a/IPython/nbconvert/writers/tests/test_stdout.py +++ b/IPython/nbconvert/writers/tests/test_stdout.py @@ -15,7 +15,7 @@ Module with tests for stdout #----------------------------------------------------------------------------- import sys -from StringIO import StringIO +from io import StringIO from ...tests.base import TestsBase from ..stdout import StdoutWriter diff --git a/IPython/parallel/controller/sqlitedb.py b/IPython/parallel/controller/sqlitedb.py index cf3c3e7..336e9bf 100644 --- a/IPython/parallel/controller/sqlitedb.py +++ b/IPython/parallel/controller/sqlitedb.py @@ -13,7 +13,10 @@ Authors: import json import os -import cPickle as pickle +try: + import cPickle as pickle +except ImportError: + import pickle from datetime import datetime try: diff --git a/IPython/parallel/tests/clienttest.py b/IPython/parallel/tests/clienttest.py index 6b65894..5e897c5 100644 --- a/IPython/parallel/tests/clienttest.py +++ b/IPython/parallel/tests/clienttest.py @@ -16,7 +16,7 @@ from __future__ import print_function import sys import tempfile import time -from StringIO import StringIO +from io import StringIO from nose import SkipTest diff --git a/IPython/sphinxext/ipython_directive.py b/IPython/sphinxext/ipython_directive.py index 9942e5e..df0342f 100644 --- a/IPython/sphinxext/ipython_directive.py +++ b/IPython/sphinxext/ipython_directive.py @@ -58,7 +58,7 @@ from __future__ import print_function #----------------------------------------------------------------------------- # Stdlib -import cStringIO +import io import os import re import sys @@ -193,7 +193,7 @@ class EmbeddedSphinxShell(object): def __init__(self): - self.cout = cStringIO.StringIO() + self.cout = io.StringIO() # Create config object for IPython diff --git a/IPython/terminal/console/completer.py b/IPython/terminal/console/completer.py index baa6cf7..a435146 100644 --- a/IPython/terminal/console/completer.py +++ b/IPython/terminal/console/completer.py @@ -1,6 +1,9 @@ # -*- coding: utf-8 -*- import readline -from Queue import Empty +try: + from queue import Empty # Py 3 +except ImportError: + from Queue import Empty # Py 2 from IPython.config import Configurable from IPython.utils.traitlets import Float diff --git a/IPython/terminal/console/interactiveshell.py b/IPython/terminal/console/interactiveshell.py index 9d4c499..9c32317 100644 --- a/IPython/terminal/console/interactiveshell.py +++ b/IPython/terminal/console/interactiveshell.py @@ -23,7 +23,10 @@ import subprocess from io import BytesIO import base64 -from Queue import Empty +try: + from queue import Empty # Py 3 +except ImportError: + from Queue import Empty # Py 2 from IPython.core import page from IPython.utils.warn import warn, error diff --git a/IPython/testing/plugin/ipdoctest.py b/IPython/testing/plugin/ipdoctest.py index 21fae2a..8bcba9b 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 commands import doctest import inspect import logging @@ -30,7 +29,7 @@ import traceback import unittest from inspect import getmodule -from StringIO import StringIO +from io import StringIO # We are overriding the default doctest runner, so we need to import a few # things from doctest directly diff --git a/IPython/utils/PyColorize.py b/IPython/utils/PyColorize.py index 2fd8898..074b95d 100644 --- a/IPython/utils/PyColorize.py +++ b/IPython/utils/PyColorize.py @@ -38,7 +38,7 @@ _scheme_default = 'Linux' # Imports -import StringIO +import io import keyword import os import sys @@ -143,13 +143,13 @@ class Parser: string_output = 0 if out == 'str' or self.out == 'str' or \ - isinstance(self.out,StringIO.StringIO): + isinstance(self.out,io.StringIO): # XXX - I don't really like this state handling logic, but at this # point I don't want to make major changes, so adding the # isinstance() check is the simplest I can do to ensure correct # behavior. out_old = self.out - self.out = StringIO.StringIO() + self.out = io.StringIO() string_output = 1 elif out is not None: self.out = out @@ -183,7 +183,7 @@ class Parser: # parse the source and write it self.pos = 0 - text = StringIO.StringIO(self.raw) + text = io.StringIO(self.raw) error = False try: diff --git a/IPython/utils/capture.py b/IPython/utils/capture.py index a663f3e..e7b96ac 100644 --- a/IPython/utils/capture.py +++ b/IPython/utils/capture.py @@ -16,7 +16,7 @@ from __future__ import print_function #----------------------------------------------------------------------------- import sys -from StringIO import StringIO +from io import StringIO #----------------------------------------------------------------------------- # Classes and functions diff --git a/IPython/utils/codeutil.py b/IPython/utils/codeutil.py index b8968bf..a51af71 100644 --- a/IPython/utils/codeutil.py +++ b/IPython/utils/codeutil.py @@ -24,7 +24,11 @@ __docformat__ = "restructuredtext en" #------------------------------------------------------------------------------- import sys -import types, copy_reg +import types +try: + import copyreg # Py 3 +except ImportError: + import copy_reg as copyreg # Py 2 def code_ctor(*args): return types.CodeType(*args) @@ -40,4 +44,4 @@ def reduce_code(co): args.insert(1, co.co_kwonlyargcount) return code_ctor, tuple(args) -copy_reg.pickle(types.CodeType, reduce_code) \ No newline at end of file +copyreg.pickle(types.CodeType, reduce_code) \ No newline at end of file diff --git a/IPython/utils/io.py b/IPython/utils/io.py index 95b9b0e..60ed1c2 100644 --- a/IPython/utils/io.py +++ b/IPython/utils/io.py @@ -17,7 +17,6 @@ from __future__ import print_function import os import sys import tempfile -from StringIO import StringIO from .capture import CapturedIO, capture_output from .py3compat import string_types diff --git a/IPython/utils/path.py b/IPython/utils/path.py index d490b94..21b5dec 100644 --- a/IPython/utils/path.py +++ b/IPython/utils/path.py @@ -206,7 +206,10 @@ def get_home_dir(require_writable=False): if not _writable_dir(homedir) and os.name == 'nt': # expanduser failed, use the registry to get the 'My Documents' folder. try: - import _winreg as wreg + try: + import winreg as wreg # Py 3 + except ImportError: + import _winreg as wreg # Py 2 key = wreg.OpenKey( wreg.HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" diff --git a/IPython/utils/pickleshare.py b/IPython/utils/pickleshare.py index 9c890da..3113b0d 100755 --- a/IPython/utils/pickleshare.py +++ b/IPython/utils/pickleshare.py @@ -37,7 +37,10 @@ from __future__ import print_function from IPython.external.path import path as Path import os,stat,time import collections -import cPickle as pickle +try: + import cPickle as pickle +except ImportError: + import pickle import glob def gethashfile(key): diff --git a/IPython/utils/tests/test_io.py b/IPython/utils/tests/test_io.py index 2ece068..c3a099c 100644 --- a/IPython/utils/tests/test_io.py +++ b/IPython/utils/tests/test_io.py @@ -15,7 +15,7 @@ from __future__ import print_function import sys -from StringIO import StringIO +from io import StringIO from subprocess import Popen, PIPE import unittest diff --git a/IPython/utils/tests/test_path.py b/IPython/utils/tests/test_path.py index dc3a82d..01d20dc 100644 --- a/IPython/utils/tests/test_path.py +++ b/IPython/utils/tests/test_path.py @@ -37,15 +37,21 @@ from IPython.utils.tempdir import TemporaryDirectory # Platform-dependent imports try: - import _winreg as wreg + import winreg as wreg # Py 3 except ImportError: - #Fake _winreg module on none windows platforms - import types - wr_name = "winreg" if py3compat.PY3 else "_winreg" - sys.modules[wr_name] = types.ModuleType(wr_name) - import _winreg as wreg - #Add entries that needs to be stubbed by the testing code - (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) + try: + import _winreg as wreg # Py 2 + except ImportError: + #Fake _winreg module on none windows platforms + import types + wr_name = "winreg" if py3compat.PY3 else "_winreg" + sys.modules[wr_name] = types.ModuleType(wr_name) + try: + import winreg as wreg + except ImportError: + import _winreg as wreg + #Add entries that needs to be stubbed by the testing code + (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) try: reload