1818
1919major , minor1 , minor2 , s , tmp = sys .version_info
2020
21+ # Handle the transition from urllib2 in Python 2 to urllib in Python 3
2122if major >= 3 :
2223 import types
2324 import urllib .request , urllib .error , urllib .parse
@@ -26,6 +27,9 @@ def urllib_quote():
2627 def addinfourl (data , headers , url , code = None ):
2728 return urllib .request .addinfourl (io .BytesIO (data ),
2829 headers , url , code )
30+ urllib_HTTPSHandler = urllib .request .HTTPSHandler
31+ urllib_build_opener = urllib .request .build_opener
32+ urllib_URLError = urllib .error .URLError
2933else :
3034 import new
3135 import urllib2
@@ -34,6 +38,9 @@ def urllib_quote():
3438 def addinfourl (data , headers , url , code = None ):
3539 return urllib2 .addinfourl (io .StringIO (data ),
3640 headers , url , code )
41+ urllib_HTTPSHandler = urllib2 .HTTPSHandler
42+ urllib_build_opener = urllib2 .build_opener
43+ urllib_URLError = urllib2 .URLError
3744
3845import matplotlib
3946
@@ -475,18 +482,18 @@ def is_scalar_or_string(val):
475482 return is_string_like (val ) or not iterable (val )
476483
477484def _get_data_server (cache_dir , baseurl ):
478- class ViewVCCachedServer (urllib2 . HTTPSHandler ):
485+ class ViewVCCachedServer (urllib_HTTPSHandler ):
479486 """
480- Urllib2 handler that takes care of caching files.
487+ Urllib handler that takes care of caching files.
481488 The file cache.pck holds the directory of files that have been cached.
482489 """
483490 def __init__ (self , cache_dir , baseurl ):
484- urllib2 . HTTPSHandler .__init__ (self )
491+ urllib_HTTPSHandler .__init__ (self )
485492 self .cache_dir = cache_dir
486493 self .baseurl = baseurl
487494 self .read_cache ()
488495 self .remove_stale_files ()
489- self .opener = urllib2 . build_opener (self )
496+ self .opener = urllib_build_opener (self )
490497
491498 def in_cache_dir (self , fn ):
492499 # make sure the datadir exists
@@ -569,7 +576,7 @@ def cache_file(self, url, data, headers):
569576 headers .get ('Last-Modified' ))
570577 self .write_cache ()
571578
572- # These urllib2 entry points are used:
579+ # These urllib entry points are used:
573580 # http_request for preprocessing requests
574581 # http_error_304 for handling 304 Not Modified responses
575582 # http_response for postprocessing requests
@@ -633,7 +640,7 @@ def get_sample_data(self, fname, asfileobj=True):
633640 path to the file as a string will be returned.
634641 """
635642 # TODO: time out if the connection takes forever
636- # (may not be possible with urllib2 only - spawn a helper process?)
643+ # (may not be possible with urllib only - spawn a helper process?)
637644
638645 quote = urllib_quote ()
639646
@@ -644,7 +651,7 @@ def get_sample_data(self, fname, asfileobj=True):
644651 % url , 'debug' )
645652 try :
646653 response = self .opener .open (url )
647- except urllib2 . URLError , e :
654+ except urllib_URLError as e :
648655 # could be a missing network connection
649656 error = str (e )
650657
0 commit comments