File: 0005-Migrate-to-python3.patch

package info (click to toggle)
libkkc 0.3.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,908 kB
  • sloc: ansic: 7,099; makefile: 917; cpp: 435; python: 231; sh: 124
file content (177 lines) | stat: -rw-r--r-- 6,858 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
From: Boyuan Yang <byang@debian.org>
Date: Wed, 13 Nov 2019 19:55:00 -0500
Subject: Migrate to python3

---
 configure.ac                                  |  2 +-
 data/templates/libkkc-data/configure.ac.in    |  2 +-
 data/templates/libkkc-data/tools/genfilter.py | 20 ++++++++++----------
 data/templates/libkkc-data/tools/sortlm.py    | 25 +++++++++++--------------
 4 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/configure.ac b/configure.ac
index e044965..b781490 100644
--- a/configure.ac
+++ b/configure.ac
@@ -47,7 +47,7 @@ AC_PROG_INSTALL
 IT_PROG_INTLTOOL
 
 dnl Check python for tests
-AM_PATH_PYTHON
+AM_PATH_PYTHON([3])
 
 # define PACKAGE_VERSION_* variables
 AM_DISABLE_STATIC
diff --git a/data/templates/libkkc-data/configure.ac.in b/data/templates/libkkc-data/configure.ac.in
index c8f5f44..f14046f 100644
--- a/data/templates/libkkc-data/configure.ac.in
+++ b/data/templates/libkkc-data/configure.ac.in
@@ -28,7 +28,7 @@ dnl Check for programs
 AC_PROG_INSTALL
 
 dnl Check python
-AM_PATH_PYTHON
+AM_PATH_PYTHON([3])
 
 AC_CONFIG_FILES([Makefile
 data/Makefile
diff --git a/data/templates/libkkc-data/tools/genfilter.py b/data/templates/libkkc-data/tools/genfilter.py
index 5ffab32..3a06d9d 100644
--- a/data/templates/libkkc-data/tools/genfilter.py
+++ b/data/templates/libkkc-data/tools/genfilter.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 # Copyright (C) 2011-2014 Daiki Ueno <ueno@gnu.org>
 # Copyright (C) 2011-2014 Red Hat, Inc.
@@ -84,24 +84,24 @@ class FilterGenerator(object):
 
     def generate(self):
         size = os.fstat(self.infile.fileno()).st_size
-        n = size / self.record_size
+        n = size // self.record_size
         m = int(math.ceil(-n*math.log10(ERROR_RATE) /
                           math.pow(math.log10(2), 2)))
-        m = (m/8 + 1)*8
+        m = (m//8 + 1)*8
         inmem = mmap.mmap(self.infile.fileno(),
                           size,
                           access=mmap.ACCESS_READ)
-        outmem = bytearray(m/8)
-        for i in xrange(0, n):
+        outmem = bytearray(m//8)
+        for i in range(0, n):
             offset = i*self.record_size
             b0, b1 = struct.unpack("=LL", inmem[offset:offset+8])
-            for k in xrange(0, 4):
+            for k in range(0, 4):
                 h = murmur_hash3_32(b0, b1, k)
                 h = int(h * (m / float(0xFFFFFFFF)))
-                outmem[h/8] |= (1 << (h%8))
+                outmem[h//8] |= (1 << (h%8))
         inmem.close()
-        # Convert bytearray to str, for Python 2.6 compatibility.
-        self.outfile.write(str(outmem))
+        # Convert bytearray to bytes, for Python 3 compatibility.
+        self.outfile.write(bytes(outmem))
 
 if __name__ == '__main__':
     import sys
@@ -110,7 +110,7 @@ if __name__ == '__main__':
     parser = argparse.ArgumentParser(description='filter')
     parser.add_argument('infile', type=argparse.FileType('r'),
                         help='input file')
-    parser.add_argument('outfile', type=argparse.FileType('w'),
+    parser.add_argument('outfile', type=argparse.FileType('wb'),
                         help='output file')
     parser.add_argument('record_size', type=int,
                         help='record size')
diff --git a/data/templates/libkkc-data/tools/sortlm.py b/data/templates/libkkc-data/tools/sortlm.py
index a0dd8fe..fe80ddc 100644
--- a/data/templates/libkkc-data/tools/sortlm.py
+++ b/data/templates/libkkc-data/tools/sortlm.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 # Copyright (C) 2011-2014 Daiki Ueno <ueno@gnu.org>
 # Copyright (C) 2011-2014 Red Hat, Inc.
@@ -40,10 +40,10 @@ class SortedGenerator(object):
         self.__min_cost = 0.0
 
     def read(self):
-        print "reading N-grams"
+        print("reading N-grams")
         self.__read_tries()
         self.__read_ngrams()
-        print "min cost = %lf" % self.__min_cost
+        print("min cost = %lf" % self.__min_cost)
 
     def __read_tries(self):
         while True:
@@ -58,7 +58,7 @@ class SortedGenerator(object):
             line = self.__infile.readline()
             if line == "":
                 break
-            line = line.strip()
+            line = line.strip('\n')
             if line == "":
                 break
             match = self.__ngram_line_regex.match(line)
@@ -89,7 +89,7 @@ class SortedGenerator(object):
                 line = self.__infile.readline()
                 if line == "":
                     break
-                line = line.strip()
+                line = line.strip('\n')
                 if line == "":
                     break
                 match = self.__ngram_line_regex.match(line)
@@ -125,14 +125,11 @@ class SortedGenerator(object):
         def quantize(cost, min_cost):
             return max(0, min(65535, int(cost * 65535 / min_cost)))
 
-        def cmp_header(a, b):
-            return cmp(a[0], b[0])
-
-        print "writing 1-gram file"
+        print("writing 1-gram file")
         unigram_offsets = {}
         unigram_file = open("%s.1gram" % self.__output_prefix, "wb")
         offset = 0
-        for ids, value in sorted(self.__ngram_entries[0].iteritems()):
+        for ids, value in sorted(self.__ngram_entries[0].items()):
             unigram_offsets[ids[0]] = offset
             s = struct.pack("=HHH",
                             quantize(value[0], self.__min_cost),
@@ -143,13 +140,13 @@ class SortedGenerator(object):
             offset += 1
         unigram_file.close()
 
-        print "writing 2-gram file"
+        print("writing 2-gram file")
         bigram_offsets = {}
         bigram_file = open("%s.2gram" % self.__output_prefix, "wb")
         keys = self.__ngram_entries[1].keys()
         items = [(struct.pack("=LL", ids[1], unigram_offsets[ids[0]]), ids) for ids in keys]
         offset = 0
-        for header, ids in sorted(items, cmp=cmp_header):
+        for header, ids in sorted(items, key=lambda x: x[0]):
             value = self.__ngram_entries[1][ids]
             bigram_offsets[ids] = offset
             s = struct.pack("=HH",
@@ -160,11 +157,11 @@ class SortedGenerator(object):
         bigram_file.close()
 
         if len(self.__ngram_entries[2]) > 0:
-            print "writing 3-gram file"
+            print("writing 3-gram file")
             trigram_file = open("%s.3gram" % self.__output_prefix, "wb")
             keys = self.__ngram_entries[2].keys()
             items = [(struct.pack("=LL", ids[2], bigram_offsets[(ids[0], ids[1])]), ids) for ids in keys]
-            for header, ids in sorted(items, cmp=cmp_header):
+            for header, ids in sorted(items, key=lambda x: x[0]):
                 value = self.__ngram_entries[2][ids]
                 s = struct.pack("=H",
                                 quantize(value[0], self.__min_cost))