[python-debian/master 26/36] Fix up debian.changelog for string handling changes in Python 3.
Colin Watson
cjwatson at canonical.com
Mon Oct 8 07:48:28 UTC 2012
---
lib/debian/changelog.py | 43 +++++++++++++++++++++++++++++++----------
tests/test_changelog.py | 48 +++++++++++++++++++++++++++++-----------------
2 files changed, 62 insertions(+), 29 deletions(-)
diff --git a/lib/debian/changelog.py b/lib/debian/changelog.py
index df0d3df..e99c06a 100644
--- a/lib/debian/changelog.py
+++ b/lib/debian/changelog.py
@@ -30,6 +30,9 @@ import pwd
import re
import socket
import warnings
+import sys
+
+import six
from debian import debian_support
@@ -137,7 +140,7 @@ class ChangeBlock(object):
changes.append(change)
self._changes = changes
- def __unicode__(self):
+ def _format(self):
# TODO(jsw): Switch to StringIO or a list to join at the end.
block = ""
if self.package is None:
@@ -170,8 +173,16 @@ class ChangeBlock(object):
block += line + "\n"
return block
- def __str__(self):
- return unicode(self).encode(self._encoding)
+ if sys.version >= '3':
+ __str__ = _format
+
+ def __bytes__(self):
+ return str(self).encode(self._encoding)
+ else:
+ __unicode__ = _format
+
+ def __str__(self):
+ return unicode(self).encode(self._encoding)
topline = re.compile(r'^(\w%(name_chars)s*) \(([^\(\) \t]+)\)'
'((\s+%(name_chars)s+)+)\;'
@@ -268,7 +279,9 @@ class Changelog(object):
state = first_heading
old_state = None
- if isinstance(file, basestring):
+ if isinstance(file, bytes):
+ file = file.decode(encoding)
+ if isinstance(file, six.string_types):
# Make sure the changelog file is not empty.
if len(file.strip()) == 0:
self._parse_error('Empty changelog file.', strict)
@@ -276,7 +289,7 @@ class Changelog(object):
file = file.splitlines()
for line in file:
- if not isinstance(line, unicode):
+ if not isinstance(line, six.text_type):
line = line.decode(encoding)
# Support both lists of lines without the trailing newline and
# those with trailing newlines (e.g. when given a file object
@@ -468,15 +481,23 @@ class Changelog(object):
def _raw_versions(self):
return [block._raw_version for block in self._blocks]
- def __unicode__(self):
+ def _format(self):
pieces = []
- pieces.append(u'\n'.join(self.initial_blank_lines))
+ pieces.append(six.u('\n').join(self.initial_blank_lines))
for block in self._blocks:
- pieces.append(unicode(block))
- return u''.join(pieces)
+ pieces.append(six.text_type(block))
+ return six.u('').join(pieces)
- def __str__(self):
- return unicode(self).encode(self._encoding)
+ if sys.version >= '3':
+ __str__ = _format
+
+ def __bytes__(self):
+ return str(self).encode(self._encoding)
+ else:
+ __unicode__ = _format
+
+ def __str__(self):
+ return unicode(self).encode(self._encoding)
def __iter__(self):
return iter(self._blocks)
diff --git a/tests/test_changelog.py b/tests/test_changelog.py
index 2ba0676..2b2ba78 100755
--- a/tests/test_changelog.py
+++ b/tests/test_changelog.py
@@ -29,10 +29,21 @@ from __future__ import absolute_import
import sys
import unittest
+import six
+
sys.path.insert(0, '../lib/')
from debian import changelog
+
+def open_utf8(filename, mode='r'):
+ """Open a UTF-8 text file in text mode."""
+ if sys.version < '3':
+ return open(filename, mode=mode)
+ else:
+ return open(filename, mode=mode, encoding='UTF-8')
+
+
class ChangelogTests(unittest.TestCase):
def test_create_changelog(self):
@@ -186,45 +197,46 @@ class ChangelogTests(unittest.TestCase):
self.assertEqual(str(c), cl_data)
def test_utf8_encoded_file_input(self):
- f = open('test_changelog_unicode')
+ f = open_utf8('test_changelog_unicode')
c = changelog.Changelog(f)
f.close()
- u = unicode(c)
- expected_u = u"""haskell-src-exts (1.8.2-3) unstable; urgency=low
+ u = six.text_type(c)
+ expected_u = six.u("""haskell-src-exts (1.8.2-3) unstable; urgency=low
* control: Use versioned Replaces: and Conflicts:
- -- Marco Túlio Gontijo e Silva <marcot at debian.org> Wed, 05 May 2010 18:01:53 -0300
+ -- Marco T\xfalio Gontijo e Silva <marcot at debian.org> Wed, 05 May 2010 18:01:53 -0300
haskell-src-exts (1.8.2-2) unstable; urgency=low
* debian/control: Rename -doc package.
- -- Marco Túlio Gontijo e Silva <marcot at debian.org> Tue, 16 Mar 2010 10:59:48 -0300
-"""
+ -- Marco T\xfalio Gontijo e Silva <marcot at debian.org> Tue, 16 Mar 2010 10:59:48 -0300
+""")
self.assertEqual(u, expected_u)
- self.assertEqual(str(c), u.encode('utf-8'))
+ self.assertEqual(bytes(c), u.encode('utf-8'))
def test_unicode_object_input(self):
- f = open('test_changelog_unicode')
- c_str = f.read()
+ f = open('test_changelog_unicode', 'rb')
+ c_bytes = f.read()
f.close()
- c_unicode = c_str.decode('utf-8')
+ c_unicode = c_bytes.decode('utf-8')
c = changelog.Changelog(c_unicode)
- self.assertEqual(unicode(c), c_unicode)
- self.assertEqual(str(c), c_str)
+ self.assertEqual(six.text_type(c), c_unicode)
+ self.assertEqual(bytes(c), c_bytes)
def test_non_utf8_encoding(self):
- f = open('test_changelog_unicode')
- c_str = f.read()
+ f = open('test_changelog_unicode', 'rb')
+ c_bytes = f.read()
f.close()
- c_unicode = c_str.decode('utf-8')
+ c_unicode = c_bytes.decode('utf-8')
c_latin1_str = c_unicode.encode('latin1')
c = changelog.Changelog(c_latin1_str, encoding='latin1')
- self.assertEqual(unicode(c), c_unicode)
- self.assertEqual(str(c), c_latin1_str)
+ self.assertEqual(six.text_type(c), c_unicode)
+ self.assertEqual(bytes(c), c_latin1_str)
for block in c:
- self.assertEqual(str(block), unicode(block).encode('latin1'))
+ self.assertEqual(bytes(block),
+ six.text_type(block).encode('latin1'))
def test_block_iterator(self):
f = open('test_changelog')
--
1.7.2.5
More information about the pkg-python-debian-commits
mailing list