Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Python3 comatibility

  • Loading branch information...
commit 5fce3218b30023499b8d83c5d15e48000f480788 1 parent 53d8ad2
@tonyseek tonyseek authored committed
View
2  pythonz/commands/__init__.py
@@ -23,5 +23,5 @@ def run(self, args):
def load_commands():
modules = ['pythonz.commands.%s' % os.path.splitext(file)[0] for file in os.listdir(os.path.dirname(__file__)) if os.path.splitext(file)[1] == '.py' and file != os.path.basename(__file__)]
- map(__import__, modules)
+ list(map(__import__, modules))
View
32 pythonz/downloader.py
@@ -1,11 +1,17 @@
-import urllib
-import urllib2
import sys
+from pythonz.util import PY3K
from pythonz.exceptions import DownloadError
+if PY3K:
+ from urllib.request import Request, urlopen, urlretrieve
+else:
+ from urllib import urlretrieve
+ from urllib2 import urlopen, Request
+
+
class ProgressBar(object):
def __init__(self, out=sys.stdout):
self._term_width = 79
@@ -13,10 +19,14 @@ def __init__(self, out=sys.stdout):
def update_line(self, current):
num_bar = int(current / 100.0 * (self._term_width - 5))
- bars = u'#' * num_bar
- spaces = u' ' * (self._term_width - 5 - num_bar)
- percentage = u'%3d' % int(current) + u'%\r'
- return bars + spaces + u' ' + percentage
+ bars = '#' * num_bar
+ spaces = ' ' * (self._term_width - 5 - num_bar)
+ percentage = '%3d' % int(current) + '%\r'
+ result = bars + spaces + ' ' + percentage
+ if not PY3K:
+ # Python 2.x
+ return result.decode("utf-8")
+ return result
def reporthook(self, blocknum, bs, size):
current = (blocknum * bs * 100) / size
@@ -30,7 +40,7 @@ def finish(self):
self._out.flush()
-class HEADRequest(urllib2.Request):
+class HEADRequest(Request):
def get_method(self):
return "HEAD"
@@ -40,7 +50,7 @@ class Downloader(object):
@classmethod
def read(cls, url):
try:
- r = urllib.urlopen(url)
+ r = urlopen(url)
except IOError:
raise DownloadError('Failed to fetch %s' % url)
else:
@@ -50,7 +60,7 @@ def read(cls, url):
def read_head_info(cls, url):
try:
req = HEADRequest(url)
- res = urllib2.urlopen(req)
+ res = urlopen(req)
except IOError:
raise DownloadError('Failed to fetch %s' % url)
else:
@@ -62,10 +72,8 @@ def read_head_info(cls, url):
def fetch(cls, url, filename):
b = ProgressBar()
try:
- urllib.urlretrieve(url, filename, b.reporthook)
+ urlretrieve(url, filename, b.reporthook)
sys.stdout.write('\n')
except IOError:
sys.stdout.write('\n')
raise DownloadError('Failed to fetch %s from %s' % (filename, url))
-
-
View
2  pythonz/installer/pythoninstaller.py
@@ -131,7 +131,7 @@ def _apply_patches(self):
s = Subprocess(log=self.logfile, cwd=self.build_dir, verbose=self.options.verbose)
for patch in self.patches:
if type(patch) is dict:
- for (ed, source) in patch.items():
+ for ed, source in patch.items():
s.shell('ed - %s < %s' % (source, ed))
else:
s.shell("patch -p0 < %s" % patch)
View
34 pythonz/util.py
@@ -7,12 +7,16 @@
import posixpath
import tarfile
import platform
-import urllib
import subprocess
import shlex
import select
-from urllib2 import urlparse
+PY3K = sys.version_info >= (3,)
+if not PY3K:
+ from urllib import quote as urlquote, unquote as urlunquote
+ from urllib2 import urlparse
+else:
+ from urllib.parse import urlparse, quote as urlquote, unquote as urlunquote
from pythonz.define import PATH_PYTHONS
from pythonz.exceptions import ShellCommandException
@@ -97,7 +101,7 @@ def symlink(src, dst):
def unlink(path):
try:
os.unlink(path)
- except OSError, e:
+ except OSError as e:
if e.errno != errno.ENOENT:
raise
@@ -201,7 +205,7 @@ def is_installed(pkg):
def path_to_fileurl(path):
path = os.path.normcase(os.path.abspath(path))
- url = urllib.quote(path)
+ url = urlquote(path)
url = url.replace(os.path.sep, '/')
url = url.lstrip('/')
return 'file:///' + url
@@ -209,25 +213,24 @@ def path_to_fileurl(path):
def fileurl_to_path(url):
assert url.startswith('file:'), ('Illegal scheme:%s' % url)
url = '/' + url[len('file:'):].lstrip('/')
- return urllib.unquote(url)
+ return urlunquote(url)
def to_str(val):
- try:
- # python3
- if type(val) is bytes:
- return val.decode()
- except Exception:
- if type(val) is unicode:
+ if not PY3K:
+ # python2
+ if isinstance(val, unicode):
return val.encode("utf-8")
+ return val
+ if isinstance(val, bytes):
+ return val.decode("utf-8")
return val
def is_str(val):
- try:
+ if not PY3K:
# python2
return isinstance(val, basestring)
- except NameError:
- # python3
- return isinstance(val, str)
+ # python3
+ return isinstance(val, str)
def is_sequence(val):
if is_str(val):
@@ -396,4 +399,3 @@ def _parse_version_parts(s):
def __repr__(self):
return self._version
-
View
1  setup.py
@@ -30,5 +30,6 @@ def find_packages(toplevel):
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.1',
'Programming Language :: Python :: 3.2',
+ 'Programming Language :: Python :: 3.3',
])
Please sign in to comment.
Something went wrong with that request. Please try again.