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