Description: Use python3
Origin: upstream,
 https://github.com/sunpinyin/sunpinyin/commit/f16a9b5704702a5303a4b951c66dc068c19469a0
 https://github.com/sunpinyin/sunpinyin/commit/409345202251af6dfae26705b7c4c9ff65dfbb75
 https://github.com/sunpinyin/sunpinyin/pull/97
Bug: https://github.com/sunpinyin/sunpinyin/issues/94

--- a/SConstruct
+++ b/SConstruct
@@ -227,7 +227,7 @@
 def PassVariables(envvar, env):
     for (x, y) in envvar:
         if x in os.environ:
-            print 'Warning: you\'ve set %s in the environmental variable!' % x
+            print('Warning: you\'ve set %s in the environmental variable!' % x)
             env[y] = os.environ[x]
 
 env = CreateEnvironment()
@@ -299,11 +299,11 @@
 
 def CheckPython(context):
     context.Message('Checking for Python library...')
-    ret = context.TryAction('python-config --prefix')[0]
+    ret = context.TryAction('python3-config --prefix')[0]
     context.Result(ret)
     if ret:
-        context.env.MergeFlags(['!python-config --includes',
-                                '!python-config --libs'])
+        context.env.MergeFlags(['!python3-config --includes',
+                                '!python3-config --libs'])
     return ret
 
 
@@ -429,9 +429,7 @@
     '@PREFIX@': env['PREFIX'],
     '@LIBDIR@': env['LIBDIR'],
     '@VERSION@': version,
-    '@CFLAGS@': reduce(lambda a, b: a + ' ' + b,
-                       map(lambda x: '-I$${includedir}' + x[3:],
-                           sorted(allinc()))),
+    '@CFLAGS@': ' '.join(['-I$${includedir}' + x[3:] for x in sorted(allinc())]),
 })
 
 libname_default = '%ssunpinyin%s' % (env.subst('${SHLIBPREFIX}'),
--- a/python/imdict.py
+++ b/python/imdict.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python 
+#!/usr/bin/python3
 
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 # 
--- a/python/importer/import_fcitx_userdict.py
+++ b/python/importer/import_fcitx_userdict.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import os, sys
 import codecs
--- a/python/importer/import_fit_userdict.py
+++ b/python/importer/import_fit_userdict.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import os
 import sqlite3 as sqlite
--- a/python/importer/import_google_userdict.py
+++ b/python/importer/import_google_userdict.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import os, sys
 import codecs
@@ -23,7 +23,7 @@
 
 def main ():
     if len (sys.argv) != 2:
-        print "Please specify the Google Pinyin exported user dict file!"
+        print("Please specify the Google Pinyin exported user dict file!")
         exit (1)
 
     google_user_dict = load_google_user_dict(sys.argv[1])
--- a/python/importer/import_qim_userdict.py
+++ b/python/importer/import_qim_userdict.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import os, sys
 import codecs
@@ -18,7 +18,7 @@
 
 def main ():
     if len (sys.argv) != 2:
-        print "Please specify the QIM exported user dict file!"
+        print("Please specify the QIM exported user dict file!")
         exit (1)
 
     qim_user_dict = load_qim_user_dict(sys.argv[1])
--- a/python/importer/import_qq_userdict.py
+++ b/python/importer/import_qq_userdict.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import os, sys
 import codecs
@@ -21,7 +21,7 @@
 
 def main ():
     if len (sys.argv) != 2:
-        print "Please specify the QQ PinYin exported user dict file!"
+        print("Please specify the QQ PinYin exported user dict file!")
         exit (1)
 
     qq_user_dict = load_qq_user_dict(sys.argv[1])
--- a/python/importer/import_sogou_celldict.py
+++ b/python/importer/import_sogou_celldict.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 # thanks for the reverse engineering efforts of following projects/peoples:
 # http://code.google.com/p/imewlconverter
@@ -71,7 +71,7 @@
 
 def main ():
     if len (sys.argv) != 2:
-        print "Please specify the Sogou PinYin Cell dict file!"
+        print("Please specify the Sogou PinYin Cell dict file!")
         exit (1)
 
     generator = get_word_from_sogou_cell_dict (sys.argv[1])
--- a/python/importer/import_sogou_userdict.py
+++ b/python/importer/import_sogou_userdict.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import os, sys
 import codecs
@@ -19,7 +19,7 @@
 
 def main ():
     if len (sys.argv) != 2:
-        print "Please specify the Sogou PinYin exported user dict file!"
+        print("Please specify the Sogou PinYin exported user dict file!")
         exit (1)
 
     sogou_user_dict = load_sogou_user_dict(sys.argv[1])
--- a/python/importer/import_ziguang_userdict.py
+++ b/python/importer/import_ziguang_userdict.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import os, sys
 import codecs
@@ -21,7 +21,7 @@
 
 def main ():
     if len (sys.argv) != 2:
-        print "Please specify the ZiGuang PinYin exported user dict file!"
+        print("Please specify the ZiGuang PinYin exported user dict file!")
         exit (1)
 
     ziguang_user_dict = load_ziguang_user_dict(sys.argv[1])
--- a/python/importer/importer.py
+++ b/python/importer/importer.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 import os, sys
 import struct
 import sqlite3 as sqlite
@@ -72,15 +72,15 @@
         try:
             syllables = [valid_syllables[s] for s in pystr.split("'")]
         except:
-            print "[%s] has un-recognized syllables, ignoring this record!" % pystr
+            print("[%s] has un-recognized syllables, ignoring this record!" % pystr)
             continue
 
         if len (syllables) < 2 or len (syllables) > 6:
-            print "[%s] is too long or too short for sunpinyin userdict" % utf8str
+            print("[%s] is too long or too short for sunpinyin userdict" % utf8str)
             continue
 
         if utf8str in sysdict:
-            #print "[%s] is already in sunpinyin's sysdict" % utf8str
+            #print("[%s] is already in sunpinyin's sysdict" % utf8str)
             continue
 
         record = [0]*14
@@ -102,7 +102,7 @@
                     """
             try:
                 db.execute (sqlstring, record)
-                #print "[%s] is imported into sunpinyin's userdict" % utf8str
+                #print("[%s] is imported into sunpinyin's userdict" % utf8str)
 
                 batch_count += 1
                 if batch_count == 100:
@@ -110,7 +110,7 @@
                     batch_count = 0
 
             except:
-                #print "[%s] is already in sunpinyin's userdict" % utf8str
+                #print("[%s] is already in sunpinyin's userdict" % utf8str)
                 pass
 
     db.commit()
@@ -130,7 +130,7 @@
         f    = record[8:14]
         str  = record[-1]
         syls = [initials[i[x]] + finals[f[x]] for x in range(l)]
-        print str.encode ('UTF-8'), id, "'".join(syls) 
+        print(str.encode ('UTF-8'), id, "'".join(syls))
         
 if __name__ == "__main__":
     export_sunpinyin_user_dict ()
--- a/python/mmseg.py
+++ b/python/mmseg.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 # 
@@ -45,7 +45,7 @@
 from utils import read_ch_sentences
 
 def usage():
-    print '''
+    print('''
 Usage:
 mmseg.py -d dict_file [-f (text|bin)] [-i] [-s STOK_ID] [-a AMBI_ID] corpus_file
 
@@ -65,7 +65,7 @@
     The sequence ABC will not be segmented, in binary mode, the AMBI-ID 
     is written out; in text mode, <ambi>ABC</ambi> will be output. Default 
     is 9.
-'''
+''')
 
 options={'show-id':       False, 
          'format' :       'bin', 
@@ -75,8 +75,8 @@
 def parse_options(args):
     try:
         opts, args = getopt.getopt(args, "hid:f:s:a:", ["help", "show-id", "dict=", "format=", "stok-id=", "ambi-id="])
-    except getopt.GetoptError, err:
-        print str(err) 
+    except getopt.GetoptError as err:
+        print(str(err))
         sys.exit(1)
 
     for opt,val in opts:
@@ -114,7 +114,7 @@
 
 def process_file(file, dict):
     for line in read_ch_sentences(file):
-        print >> sys.stderr, line.encode('UTF-8')
+        print(line.encode('UTF-8'), file=sys.stderr)
         length = len(line)
         i = 0
         while (i < length):
--- a/python/pinyin_data.py
+++ b/python/pinyin_data.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python 
+#!/usr/bin/python3
 
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 # 
--- a/python/pinyin_info_gen.py
+++ b/python/pinyin_info_gen.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python 
+#!/usr/bin/python3
 
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 # 
@@ -38,60 +38,60 @@
 from pinyin_data import *
 
 def fmt_str_array (name, var) :
-    print 'static const char *%s[] = { %s };' % (name, ', '.join ('"%s"' % s for s in var))
+    print('static const char *%s[] = { %s };' % (name, ', '.join ('"%s"' % s for s in var)))
 
 def fmt_array_size (name):
-    print 'static const unsigned num_%s = sizeof(%s) / sizeof(*%s);' % (name, name, name)
+    print('static const unsigned num_%s = sizeof(%s) / sizeof(*%s);' % (name, name, name))
 
 def fmt_str_pair_array (name, var) :
-    print 'static const char *%s[] = {' % name
+    print('static const char *%s[] = {' % name)
     for s1, s2 in var:
-        print '    %-7s %s' % ('"%s",' % s1, '"%s",' % s2)
-    print '};'
+        print('    %-7s %s' % ('"%s",' % s1, '"%s",' % s2))
+    print('};')
 
 def fmt_pair_array_size (name):
-    print 'static const unsigned num_%s = sizeof(%s) / sizeof(*%s) / 2;' % (name, name, name)
+    print('static const unsigned num_%s = sizeof(%s) / sizeof(*%s) / 2;' % (name, name, name))
 
 fmt_str_array ('initials', initials)
 fmt_array_size ('initials')
-print ''
+print('')
 
 fmt_str_array('finals', finals)
 fmt_array_size ('finals')
-print ''
+print('')
 
 fmt_str_array('fuzzy_finals', inner_fuzzy_finals)
 fmt_array_size ('fuzzy_finals')
-print ''
+print('')
 
 fmt_str_pair_array ('fuzzy_pairs', fuzzy_pairs)
 fmt_pair_array_size ('fuzzy_pairs')
-print ''
+print('')
 
 fmt_str_pair_array ('auto_correction_pairs', sorted(auto_correction_pairs.items()))
 fmt_pair_array_size ('auto_correction_pairs')
-print ''
+print('')
 
-print 'static const unsigned fuzzy_finals_map [] = {'
+print('static const unsigned fuzzy_finals_map [] = {')
 for s in inner_fuzzy_finals:
-    print '    %-7s %-7s %-7s /* %-4s -> %-4s len %d */' % ('0x%02x,' % finals.index(s), '0x%02x,' % valid_syllables[s[1:]], '%d,' % (len(s)-1,),  s, s[1:], len(s)-1)
-print '};\n'
+    print('    %-7s %-7s %-7s /* %-4s -> %-4s len %d */' % ('0x%02x,' % finals.index(s), '0x%02x,' % valid_syllables[s[1:]], '%d,' % (len(s)-1,),  s, s[1:], len(s)-1))
+print('};\n')
 
-print 'static const TPyTabEntry pinyin_table[] = {'
+print('static const TPyTabEntry pinyin_table[] = {')
 for syllable, hex_syllable in sorted(valid_syllables.items()):
-    print '    { %-9s %s },' % ('"%s",' % syllable, '0x%05x' % hex_syllable)
-print '};\n'
+    print('    { %-9s %s },' % ('"%s",' % syllable, '0x%05x' % hex_syllable))
+print('};\n')
 
-print 'static const unsigned fuzzy_pre_syllables [] = {'
+print('static const unsigned fuzzy_pre_syllables [] = {')
 for s in fuzzy_pre_syllables:
-    print '    %-11s %-7s %-11s /* %s */' % ('0x%05x,' % valid_syllables[s[:-1]], "'%s'," % s[-1], '0x%05x,' % valid_syllables[s], s)
-print '    0x0,'
-print '};\n'
+    print('    %-11s %-7s %-11s /* %s */' % ('0x%05x,' % valid_syllables[s[:-1]], "'%s'," % s[-1], '0x%05x,' % valid_syllables[s], s))
+print('    0x0,')
+print('};\n')
 
-print 'static const unsigned fuzzy_pro_syllables [] = {'
+print('static const unsigned fuzzy_pro_syllables [] = {')
 for s in fuzzy_pro_syllables:
-    print '    %-11s %-7s %-11s /* %s */' % ('0x%05x,' % valid_syllables[s], "'%s'," % s[0], '0x%05x,' % valid_syllables[s[1:]], s)
-print '    0x0,'
-print '};\n'
+    print('    %-11s %-7s %-11s /* %s */' % ('0x%05x,' % valid_syllables[s], "'%s'," % s[0], '0x%05x,' % valid_syllables[s[1:]], s))
+print('    0x0,')
+print('};\n')
 
 # -*- indent-tabs-mode: nil -*- vim:et:ts=4
--- a/python/pytrie.pyx
+++ b/python/pytrie.pyx
@@ -92,7 +92,7 @@
     def get_words (self):
         words = []
         cdef CPinyinTrie_TWord *p= <CPinyinTrie_TWord*> self.pnode.getWordIdPtr ()
-        for i in xrange (self.pnode.m_nWordId):
+        for i in range (self.pnode.m_nWordId):
             words.append (WordInfo(p[i].m_id, p[i].m_bSeen, p[i].m_cost, p[i].m_len, p[i].m_csLevel))
         return words 
 
--- a/python/quanpin_trie_gen.py
+++ b/python/quanpin_trie_gen.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python 
+#!/usr/bin/python3
 
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 # 
--- a/python/test.py
+++ b/python/test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 # -*- coding: UTF-8 -*-
 
 from pyslm import Slm, SlmState
@@ -10,21 +10,21 @@
         return
 
     pr, result = slm.transfer (SlmState(0,0), 58614)
-    print "pr =", pr, "\tresult = %s" % result
+    print("pr =", pr, "\tresult = %s" % result)
     
     pr, result = slm.transfer (result, 75956)
-    print "pr =", pr, "\tresult = %s" % result
+    print("pr =", pr, "\tresult = %s" % result)
     
     pr, result = slm.transfer (result, 84582)
-    print "pr =", pr, "\tresult = %s" % result
+    print("pr =", pr, "\tresult = %s" % result)
     
     his = slm.history_state_of (result)
-    print "his = %s" % his
+    print("his = %s" % his)
     
     slm.historify (result)
-    print "result = %s" % result
+    print("result = %s" % result)
 
-    print 'last_word_id =', slm.last_word_id (result)
+    print('last_word_id =', slm.last_word_id (result))
 
     slm.free ()
 
@@ -36,12 +36,12 @@
     root = trie.get_root_node ()
     node = trie.transfer (root, 0x1000)
     for w in node.get_words ():
-        print w
+        print(w)
 
-    print trie.is_valid (node, False, 0)
-    print trie[10000]
+    print(trie.is_valid (node, False, 0))
+    print(trie[10000])
 
-    print trie.get_symbol_id (u'。')
+    print(trie.get_symbol_id (u'。'))
     trie.free ()
 
 test_pyslm()
--- a/python/trie.py
+++ b/python/trie.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 # 
@@ -161,7 +161,7 @@
                 if progress_cb:
                     progress_cb ()
 
-        for i in xrange (self.chr_encoder (max(trie.root.trans))+1):
+        for i in range (self.chr_encoder (max(trie.root.trans))+1):
             if self.check[i] == -1:
                 self.check[i] = 0
 
@@ -272,7 +272,7 @@
         v, l = match_longest (datrie, s+'b')
         assert (len(s) == l and valid_syllables[s] == v)
 
-    print 'test executed successfully'
+    print('test executed successfully')
 
 if __name__ == "__main__":
     test ()
--- a/python/utils.py
+++ b/python/utils.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python 
+#!/usr/bin/python3
 # -*- coding: UTF-8 -*-
 
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
@@ -202,7 +202,7 @@
         self.__realsize += 1
 
     def __iter__(self):
-        for i in xrange(0, self.__realsize):
+        for i in range(0, self.__realsize):
             yield self.__access(i)
 
     def truncate(self, tsize):
--- a/src/SConscript
+++ b/src/SConscript
@@ -53,7 +53,7 @@
 })
 env.Command('sunpinyin-dictgen', 'sunpinyin-dictgen.mk', [
     Copy("$TARGET", "$SOURCE"),
-    Chmod("$TARGET", 0755),
+    Chmod("$TARGET", 0o755),
 ])
 
 # -*- indent-tabs-mode: nil -*- vim:et:ts=4
