summaryrefslogtreecommitdiff
path: root/apt/package.py
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2014-01-06 13:42:46 +0100
committerJulian Andres Klode <jak@debian.org>2014-01-06 13:47:31 +0100
commit86b1fa71cf952c5f4aae1cabef332ba0d4f5885c (patch)
tree1ef5ca37678cd559f50d4235a05c9fecc14ab6ad /apt/package.py
parent8b8a55f10ca47fe297fbb9b16a8e100658c60df3 (diff)
downloadpython-apt-86b1fa71cf952c5f4aae1cabef332ba0d4f5885c.tar.gz
Use a single code base for Python 2 and 3
This is much better than running 2to3 during the build, as it gives us more control over the Python 3 code.
Diffstat (limited to 'apt/package.py')
-rw-r--r--apt/package.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/apt/package.py b/apt/package.py
index f7f07680..34c1b91e 100644
--- a/apt/package.py
+++ b/apt/package.py
@@ -21,13 +21,20 @@
"""Functionality related to packages."""
from __future__ import print_function
-import httplib
+
import os
import sys
import re
import socket
import subprocess
-import urllib2
+
+try:
+ from http.client import BadStatusLine
+ from urllib.error import HTTPError
+ from urllib.request import urlopen
+except ImportError:
+ from httplib import BadStatusLine
+ from urllib2 import HTTPError, urlopen
from collections import Mapping, Sequence
@@ -38,6 +45,9 @@ from apt_pkg import gettext as _
__all__ = ('BaseDependency', 'Dependency', 'Origin', 'Package', 'Record',
'Version', 'VersionList')
+if sys.version_info.major >= 3:
+ unicode = str
+
def _file_is_same(path, size, md5):
"""Return ``True`` if the file is the same."""
@@ -325,14 +335,14 @@ class Version(object):
try:
if not isinstance(dsc, unicode):
# Only convert where needed (i.e. Python 2.X)
- dsc = unicode(dsc, "utf-8")
+ dsc = dsc.decode("utf-8")
except UnicodeDecodeError as err:
return _("Invalid unicode in description for '%s' (%s). "
"Please report.") % (self.package.name, err)
lines = iter(dsc.split("\n"))
# Skip the first line, since its a duplication of the summary
- lines.next()
+ next(lines)
for raw_line in lines:
if raw_line.strip() == ".":
# The line is just line break
@@ -517,7 +527,7 @@ class Version(object):
.. versionadded:: 0.7.10
"""
try:
- return iter(self._uris()).next()
+ return next(iter(self._uris()))
except StopIteration:
return None
@@ -970,7 +980,7 @@ class Package(object):
if cancel_lock and cancel_lock.isSet():
return u""
# FIXME: python3.2: Should be closed manually
- changelog_file = urllib2.urlopen(uri)
+ changelog_file = urlopen(uri)
# do only get the lines that are new
changelog = u""
regexp = "^%s \((.*)\)(.*)$" % (re.escape(src_pkg))
@@ -1011,14 +1021,14 @@ class Package(object):
changelog = changelog.decode("utf-8")
self._changelog = changelog
- except urllib2.HTTPError:
+ except HTTPError:
res = _("The list of changes is not available yet.\n\n"
"Please use http://launchpad.net/ubuntu/+source/%s/"
"%s/+changelog\n"
"until the changes become available or try again "
"later.") % (src_pkg, src_ver)
return res if isinstance(res, unicode) else res.decode("utf-8")
- except (IOError, httplib.BadStatusLine):
+ except (IOError, BadStatusLine):
res = _("Failed to download the list of changes. \nPlease "
"check your Internet connection.")
return res if isinstance(res, unicode) else res.decode("utf-8")