Browse Source

More fix after merge pull request from @myint to support python3 (issue #2)

Matthew Wang 12 years ago
parent
commit
1ea76e6853
2 changed files with 11 additions and 4 deletions
  1. 3
    2
      Makefile
  2. 8
    2
      src/cdiff.py

+ 3
- 2
Makefile View File

@@ -7,8 +7,6 @@ TESTS = git svn crlf
7 7
 dogfood:
8 8
 	git diff | src/cdiff.py
9 9
 	git diff | src/cdiff.py -s
10
-	git diff | src/cdiff.py -s -w 60
11
-	git diff | src/cdiff.py -s -w 90
12 10
 
13 11
 test: $(TESTS)
14 12
 
@@ -16,5 +14,8 @@ $(TESTS):
16 14
 	src/cdiff.py tests/$@.diff
17 15
 	src/cdiff.py tests/$@.diff -s
18 16
 	src/cdiff.py tests/$@.diff | diff -u tests/$@.diff -
17
+	python3 src/cdiff.py tests/$@.diff
18
+	python3 src/cdiff.py tests/$@.diff -s
19
+	python3 src/cdiff.py tests/$@.diff | diff -u tests/$@.diff -
19 20
 
20 21
 # vim:set noet ts=8 sw=8:

+ 8
- 2
src/cdiff.py View File

@@ -13,6 +13,7 @@ import sys
13 13
 if sys.hexversion < 0x02050000:
14 14
     sys.stderr.write("ERROR: requires python >= 2.5.0\n")
15 15
     sys.exit(1)
16
+_is_py3 = sys.hexversion >= 0x03000000
16 17
 
17 18
 import os
18 19
 import re
@@ -160,7 +161,7 @@ class Diff(object):
160 161
                     out.append(patt.sub(r'\1', markup))
161 162
                     markup = patt.sub(r'\3', markup)
162 163
                 else:
163
-                    # FIXME: utf-8 char broken here
164
+                    # FIXME: utf-8 wchar broken here
164 165
                     out.append(markup[0])
165 166
                     markup = markup[1:]
166 167
                     count += 1
@@ -468,7 +469,11 @@ if __name__ == '__main__':
468 469
     opts, args = parser.parse_args()
469 470
 
470 471
     if len(args) >= 1:
471
-        diff_hdl = open(args[0], 'r')
472
+        if _is_py3:
473
+            # Python3 needs the newline='' to keep '\r' (DOS format)
474
+            diff_hdl = open(args[0], mode='rt', newline='')
475
+        else:
476
+            diff_hdl = open(args[0], mode='rt')
472 477
     elif sys.stdin.isatty():
473 478
         sys.stderr.write('Try --help option for usage\n')
474 479
         sys.exit(1)
@@ -477,6 +482,7 @@ if __name__ == '__main__':
477 482
 
478 483
     # FIXME: can't use generator for now due to current implementation in parser
479 484
     stream = diff_hdl.readlines()
485
+
480 486
     # Don't let empty diff pass thru
481 487
     if not stream:
482 488
         sys.exit(0)