From 00c6bd3f4ee6bf9c5b4c294ff446d36e14e1cab2 2013-01-30 19:10:39 From: Thomas Kluyver Date: 2013-01-30 19:10:39 Subject: [PATCH] Defer import of urllib --- diff --git a/IPython/core/extensions.py b/IPython/core/extensions.py index 6ee6bcb..d61b354 100644 --- a/IPython/core/extensions.py +++ b/IPython/core/extensions.py @@ -20,8 +20,6 @@ Authors: import os from shutil import copyfile import sys -from urllib import urlretrieve -from urlparse import urlparse from IPython.core.error import UsageError from IPython.config.configurable import Configurable @@ -171,6 +169,8 @@ class ExtensionManager(Configurable): src_filename = os.path.basename(url) copy = copyfile else: + from urllib import urlretrieve # Deferred imports + from urlparse import urlparse src_filename = urlparse(url).path.split('/')[-1] copy = urlretrieve diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 5dff311..48a1287 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -28,7 +28,6 @@ import runpy import sys import tempfile import types -import urllib from io import open as io_open from IPython.config.configurable import SingletonConfigurable @@ -2989,7 +2988,8 @@ class InteractiveShell(SingletonConfigurable): return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie) except UnicodeDecodeError: if not py_only : - response = urllib.urlopen(target) + from urllib import urlopen # Deferred import + response = urlopen(target) return response.read().decode('latin1') raise ValueError(("'%s' seem to be unreadable.") % utarget) diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index b0b111f..2026ab8 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -19,7 +19,6 @@ import json import os import re import sys -from urllib2 import urlopen # Our own packages from IPython.core.error import TryNext, StdinNotImplementedError, UsageError @@ -151,6 +150,7 @@ class CodeMagics(Magics): } }).encode('utf-8') + from urllib2 import urlopen # Deferred import response = urlopen("https://api.github.com/gists", post_data) response_data = json.loads(response.read().decode('utf-8')) return response_data['html_url'] diff --git a/IPython/lib/display.py b/IPython/lib/display.py index dd2ce8c..3066c49 100644 --- a/IPython/lib/display.py +++ b/IPython/lib/display.py @@ -2,8 +2,6 @@ Authors : MinRK, gregcaporaso, dannystaple """ -import urllib - from os.path import exists, isfile, splitext, abspath, join, isdir from os import walk, sep @@ -41,7 +39,8 @@ class YouTubeVideo(object): def _repr_html_(self): """return YouTube embed iframe for this video id""" if self.params: - params = "?" + urllib.urlencode(self.params) + from urllib import urlencode # Deferred import + params = "?" + urlencode(self.params) else: params = "" return """ diff --git a/IPython/utils/openpy.py b/IPython/utils/openpy.py index b40bb99..1b3d8b5 100644 --- a/IPython/utils/openpy.py +++ b/IPython/utils/openpy.py @@ -9,7 +9,6 @@ from __future__ import absolute_import import io from io import TextIOWrapper, BytesIO import re -import urllib cookie_re = re.compile(ur"coding[:=]\s*([-\w.]+)", re.UNICODE) cookie_comment_re = re.compile(ur"^\s*#.*coding[:=]\s*([-\w.]+)", re.UNICODE) @@ -205,7 +204,8 @@ def read_py_url(url, errors='replace', skip_encoding_cookie=True): ------- A unicode string containing the contents of the file. """ - response = urllib.urlopen(url) + from urllib import urlopen # Deferred import for faster start + response = urlopen(url) buffer = io.BytesIO(response.read()) return source_to_unicode(buffer, errors, skip_encoding_cookie)