Skip to content

Commit

Permalink
More python 2 support cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ymattw committed Jun 23, 2024
1 parent 4fe5f67 commit d9bf050
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import with_statement
import sys
from setuptools import setup
from ydiff import PKG_INFO

Expand Down
34 changes: 16 additions & 18 deletions tests/test_ydiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
class DecodeTest(unittest.TestCase):

def test_normal(self):
utext = 'hello'.encode('utf-8')
self.assertEqual('hello', ydiff.decode(utext))
octets = b'\xe4\xbd\xa0\xe5\xa5\xbd'
want = '你好'
self.assertEqual(ydiff.decode(octets), want)

def test_latin_1(self):
text = '\x80\x02q\x01(U'
if sys.version_info[0] == 2:
decoded_text = text.decode('latin-1')
else:
decoded_text = text
self.assertEqual(decoded_text, ydiff.decode(text))
octets = b'\x80\x02q\x01(U'
want = '\x80\x02q\x01(U'
self.assertEqual(ydiff.decode(octets), want)


class HunkTest(unittest.TestCase):
Expand Down Expand Up @@ -456,7 +454,7 @@ def test_is_new_neg(self):
class DiffParserTest(unittest.TestCase):

def test_parse_invalid_hunk_meta(self):
patch = """\
patch = b"""\
spam
--- a
+++ b
Expand All @@ -469,7 +467,7 @@ def test_parse_invalid_hunk_meta(self):
self.assertRaises(RuntimeError, list, parser.parse())

def test_parse_dangling_header(self):
patch = """\
patch = b"""\
--- a
+++ b
@@ -1,2 +1,2 @@
Expand All @@ -491,7 +489,7 @@ def test_parse_dangling_header(self):
self.assertEqual(len(out[1]._hunks), 0)

def test_parse_missing_new_path(self):
patch = """\
patch = b"""\
--- a
+++ b
@@ -1,2 +1,2 @@
Expand All @@ -506,7 +504,7 @@ def test_parse_missing_new_path(self):
self.assertRaises(AssertionError, list, parser.parse())

def test_parse_missing_hunk_meta(self):
patch = """\
patch = b"""\
--- a
+++ b
@@ -1,2 +1,2 @@
Expand All @@ -528,7 +526,7 @@ def test_parse_missing_hunk_meta(self):
self.assertEqual(len(out[1]._hunks), 0)

def test_parse_missing_hunk_list(self):
patch = """\
patch = b"""\
--- a
+++ b
@@ -1,2 +1,2 @@
Expand All @@ -545,7 +543,7 @@ def test_parse_missing_hunk_list(self):
self.assertRaises(AssertionError, list, parser.parse())

def test_parse_only_in_dir(self):
patch = """\
patch = b"""\
--- a
+++ b
@@ -1,2 +1,2 @@
Expand All @@ -572,7 +570,7 @@ def test_parse_only_in_dir(self):
self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)

def test_parse_only_in_dir_at_last(self):
patch = """\
patch = b"""\
--- a
+++ b
@@ -1,2 +1,2 @@
Expand All @@ -591,7 +589,7 @@ def test_parse_only_in_dir_at_last(self):
self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])

def test_parse_binary_differ_diff_ru(self):
patch = """\
patch = b"""\
--- a
+++ b
@@ -1,2 +1,2 @@
Expand Down Expand Up @@ -621,7 +619,7 @@ def test_parse_binary_differ_diff_ru(self):
self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)

def test_parse_binary_differ_git(self):
patch = """\
patch = b"""\
diff --git a/foo b/foo
index 529d8a3..ad71911 100755
--- a/foo
Expand Down Expand Up @@ -657,7 +655,7 @@ def test_parse_binary_differ_git(self):
self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)

def test_parse_svn_prop(self):
patch = """\
patch = b"""\
--- a
+++ b
Added: svn:executable
Expand Down
17 changes: 7 additions & 10 deletions ydiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,15 @@ def is_binary_differ(self, line):
class DiffParser(object):

def __init__(self, stream):
self._stream = stream
self._stream = stream # bytes

def parse(self):
"""parse all diff lines, construct a list of UnifiedDiff objects"""
diff = UnifiedDiff([], None, None, [])
headers = []

for line in self._stream:
line = decode(line)
for octets in self._stream:
line = decode(octets)

if diff.is_old_path(line):
# This is a new diff when current hunk is not yet genreated or
Expand Down Expand Up @@ -588,7 +588,7 @@ def _markup_mix(self, line, base_color):


def markup_to_pager(stream, opts):
"""Pipe unified diff stream to pager (less)."""
"""Pipe unified diff stream (in bytes) to pager (less)."""
pager_cmd = [opts.pager]
pager_opts = opts.pager_options.split(' ') if opts.pager_options else []

Expand Down Expand Up @@ -623,14 +623,11 @@ def check_command_status(arguments):
return False


def decode(line):
"""Decode UTF-8 if necessary."""
if isinstance(line, str):
return line

def decode(octets):
"""Decode bytes (read from file)."""
for encoding in ['utf-8', 'latin1']:
try:
return line.decode(encoding)
return octets.decode(encoding)
except UnicodeDecodeError:
pass

Expand Down

0 comments on commit d9bf050

Please sign in to comment.