##// 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
@@ -169,11 +169,13 b' class ExtensionManager(Configurable):'
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
@@ -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,6 +183,9 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 try:
187 from urllib.parse import urlencode # Py 3
188 except ImportError:
186 from urllib import urlencode
189 from urllib import urlencode
187 params = "?" + urlencode(self.params)
190 params = "?" + urlencode(self.params)
188 else:
191 else:
@@ -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