33from the Python Cookbook -- hence the name cbook
44"""
55from __future__ import print_function
6+
67import re , os , errno , sys , io , traceback , locale , threading , types
78import time , datetime
89import warnings
1415import random
1516import urllib2
1617from functools import reduce
17- if sys .version_info [0 ] >= 3 :
18+
19+ major , minor1 , minor2 , s , tmp = sys .version_info
20+
21+ if major >= 3 :
1822 import types
23+ import urllib .request
24+ def addinfourl (data , headers , url , code = None ):
25+ return urllib .request .addinfourl (io .BytesIO (data ),
26+ headers , url , code )
1927else :
2028 import new
29+ import urllib2
30+ def addinfourl (data , headers , url , code = None ):
31+ return urllib2 .addinfourl (io .StringIO (data ),
32+ headers , url , code )
2133
2234import matplotlib
2335
24- major , minor1 , minor2 , s , tmp = sys .version_info
25-
2636
2737# On some systems, locale.getpreferredencoding returns None,
2838# which can break unicode; and the sage project reports that
@@ -275,7 +285,7 @@ def process(self, s, *args, **kwargs):
275285 callbacks on *s* will be called with *\*args* and *\*\*kwargs*
276286 """
277287 self ._check_signal (s )
278- for cid , proxy in self .callbacks [s ].iteritems ():
288+ for cid , proxy in self .callbacks [s ].items ():
279289 # Clean out dead references
280290 if proxy .inst is not None and proxy .inst () is None :
281291 del self .callbacks [s ][cid ]
@@ -571,7 +581,8 @@ def https_request(self, req):
571581 if url in self .cache :
572582 _ , etag , lastmod = self .cache [url ]
573583 req .add_header ("If-None-Match" , etag )
574- req .add_header ("If-Modified-Since" , lastmod )
584+ if lastmod :
585+ req .add_header ("If-Modified-Since" , lastmod )
575586 return req
576587
577588 def https_error_304 (self , req , fp , code , msg , hdrs ):
@@ -584,7 +595,7 @@ def https_error_304(self, req, fp, code, msg, hdrs):
584595 'ViewVCCachedServer: reading data file from cache file "%s"' % fn ,
585596 'debug' )
586597 file = open (fn , 'rb' )
587- handle = urllib2 . addinfourl (file , hdrs , url )
598+ handle = addinfourl (file , hdrs , url )
588599 handle .code = 304
589600 return handle
590601
@@ -599,9 +610,7 @@ def https_response(self, req, response):
599610 else :
600611 data = response .read ()
601612 self .cache_file (req .get_full_url (), data , response .headers )
602- result = urllib2 .addinfourl (io .StringIO (data ),
603- response .headers ,
604- req .get_full_url ())
613+ result = addinfourl (data , response .headers , req .get_full_url ())
605614 result .code = response .code
606615 result .msg = response .msg
607616 return result
@@ -620,10 +629,7 @@ def get_sample_data(self, fname, asfileobj=True):
620629
621630 # quote is not in python2.4, so check for it and get it from
622631 # urllib if it is not available
623- quote = getattr (urllib2 , 'quote' , None )
624- if quote is None :
625- import urllib
626- quote = urllib .quote
632+ quote = urllib2 .quote
627633
628634 # retrieve the URL for the side effect of refreshing the cache
629635 url = self .baseurl + quote (fname )
0 commit comments