##// END OF EJS Templates
Fix Python 3 handling of urllib...
Sean Vig -
Show More
@@ -312,8 +312,11 b' class DisplayObject(object):'
312 312 self.data = f.read()
313 313 elif self.url is not None:
314 314 try:
315 import urllib2
316 response = urllib2.urlopen(self.url)
315 try:
316 from urllib.request import urlopen # Py3
317 except ImportError:
318 from urllib2 import urlopen
319 response = urlopen(self.url)
317 320 self.data = response.read()
318 321 # extract encoding from header, if there is one:
319 322 encoding = None
@@ -151,37 +151,39 b' class ExtensionManager(Configurable):'
151 151 if hasattr(mod, 'unload_ipython_extension'):
152 152 mod.unload_ipython_extension(self.shell)
153 153 return True
154
154
155 155 def install_extension(self, url, filename=None):
156 156 """Download and install an IPython extension.
157
157
158 158 If filename is given, the file will be so named (inside the extension
159 159 directory). Otherwise, the name from the URL will be used. The file must
160 160 have a .py or .zip extension; otherwise, a ValueError will be raised.
161
161
162 162 Returns the full path to the installed file.
163 163 """
164 164 # Ensure the extension directory exists
165 165 if not os.path.isdir(self.ipython_extension_dir):
166 166 os.makedirs(self.ipython_extension_dir, mode = 0o777)
167
167
168 168 if os.path.isfile(url):
169 169 src_filename = os.path.basename(url)
170 170 copy = copyfile
171 171 else:
172 from urllib import urlretrieve # Deferred imports
172 # Deferred imports
173 173 try:
174 174 from urllib.parse import urlparse # Py3
175 from urllib.request import urlretrieve
175 176 except ImportError:
176 177 from urlparse import urlparse
178 from urllib import urlretrieve
177 179 src_filename = urlparse(url).path.split('/')[-1]
178 180 copy = urlretrieve
179
181
180 182 if filename is None:
181 183 filename = src_filename
182 184 if os.path.splitext(filename)[1] not in ('.py', '.zip'):
183 185 raise ValueError("The file must have a .py or .zip extension", filename)
184
186
185 187 filename = os.path.join(self.ipython_extension_dir, filename)
186 188 copy(url, filename)
187 189 return filename
@@ -3088,7 +3088,11 b' class InteractiveShell(SingletonConfigurable):'
3088 3088 return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie)
3089 3089 except UnicodeDecodeError:
3090 3090 if not py_only :
3091 from urllib import urlopen # Deferred import
3091 # Deferred import
3092 try:
3093 from urllib.request import urlopen # Py3
3094 except ImportError:
3095 from urllib import urlopen
3092 3096 response = urlopen(target)
3093 3097 return response.read().decode('latin1')
3094 3098 raise ValueError(("'%s' seem to be unreadable.") % utarget)
@@ -235,7 +235,11 b' class CodeMagics(Magics):'
235 235 print(e.args[0])
236 236 return
237 237
238 from urllib2 import urlopen # Deferred import
238 # Deferred import
239 try:
240 from urllib.request import urlopen # Py 3
241 except ImportError:
242 from urllib2 import urlopen
239 243 import json
240 244 post_data = json.dumps({
241 245 "description": opts.get('d', "Pasted from IPython"),
@@ -58,11 +58,15 b' import os'
58 58 import shutil
59 59 import sys
60 60 import tarfile
61 import urllib2
62 61 import zipfile
63 62
64 63 from IPython.utils.path import get_ipython_dir
65 64
65 try:
66 from urllib.request import urlopen # Py 3
67 except ImportError:
68 from urllib2 import urlopen
69
66 70 #-----------------------------------------------------------------------------
67 71 #
68 72 #-----------------------------------------------------------------------------
@@ -167,7 +171,7 b" def install_mathjax(tag='v2.2', dest=default_dest, replace=False, file=None, ext"
167 171 # download mathjax
168 172 mathjax_url = "https://github.com/mathjax/MathJax/archive/%s.tar.gz" %tag
169 173 print("Downloading mathjax source from %s" % mathjax_url)
170 response = urllib2.urlopen(mathjax_url)
174 response = urlopen(mathjax_url)
171 175 file = response.fp
172 176
173 177 print("Extracting to %s" % dest)
@@ -183,7 +183,10 b' class IFrame(object):'
183 183 def _repr_html_(self):
184 184 """return the embed iframe"""
185 185 if self.params:
186 from urllib import urlencode
186 try:
187 from urllib.parse import urlencode # Py 3
188 except ImportError:
189 from urllib import urlencode
187 190 params = "?" + urlencode(self.params)
188 191 else:
189 192 params = ""
@@ -207,7 +207,11 b" def read_py_url(url, errors='replace', skip_encoding_cookie=True):"
207 207 -------
208 208 A unicode string containing the contents of the file.
209 209 """
210 from urllib import urlopen # Deferred import for faster start
210 # Deferred import for faster start
211 try:
212 from urllib.request import urlopen # Py 3
213 except ImportError:
214 from urllib import urlopen
211 215 response = urlopen(url)
212 216 buffer = io.BytesIO(response.read())
213 217 return source_to_unicode(buffer, errors, skip_encoding_cookie)
General Comments 0
You need to be logged in to leave comments. Login now