THRIFT-3613 Port Python C extension to Python 3
Client: Python
Patch: Nobuaki Sukegawa

This closes #845
diff --git a/test/py/FastbinaryTest.py b/test/py/FastbinaryTest.py
index d688cd5..db3ef8b 100755
--- a/test/py/FastbinaryTest.py
+++ b/test/py/FastbinaryTest.py
@@ -122,7 +122,7 @@
     def _check_write(self, o):
         trans_fast = TTransport.TMemoryBuffer()
         trans_slow = TTransport.TMemoryBuffer()
-        prot_fast = self._fast(trans_fast)
+        prot_fast = self._fast(trans_fast, fallback=False)
         prot_slow = self._slow(trans_slow)
 
         o.write(prot_fast)
@@ -140,7 +140,7 @@
         slow_version_binary = prot.trans.getvalue()
 
         prot = self._fast(
-            TTransport.TMemoryBuffer(slow_version_binary))
+            TTransport.TMemoryBuffer(slow_version_binary), fallback=False)
         c = o.__class__()
         c.read(prot)
         if c != o:
@@ -152,7 +152,7 @@
 
         prot = self._fast(
             TTransport.TBufferedTransport(
-                TTransport.TMemoryBuffer(slow_version_binary)))
+                TTransport.TMemoryBuffer(slow_version_binary)), fallback=False)
         c = o.__class__()
         c.read(prot)
         if c != o:
@@ -187,7 +187,7 @@
         o = Backwards(**{"first_tag2": 4, "second_tag1": 2})
         trans_fast = TTransport.TMemoryBuffer()
         trans_slow = TTransport.TMemoryBuffer()
-        prot_fast = self._fast(trans_fast)
+        prot_fast = self._fast(trans_fast, fallback=False)
         prot_slow = self._slow(trans_slow)
 
         o.write(prot_fast)
@@ -196,7 +196,7 @@
         MINE = trans_fast.getvalue()
         assert id(ORIG) != id(MINE)
 
-        prot = self._fast(TTransport.TMemoryBuffer())
+        prot = self._fast(TTransport.TMemoryBuffer(), fallback=False)
         o.write(prot)
         prot = self._slow(
             TTransport.TMemoryBuffer(prot.trans.getvalue()))
@@ -218,12 +218,12 @@
 from __main__ import hm, rs, TDevNullTransport
 from thrift.protocol.{0} import {0}{1}
 trans = TDevNullTransport()
-prot = {0}{1}(trans)
+prot = {0}{1}(trans{2})
 """
 
-    setup_fast = setup.format(protocol, 'Accelerated')
+    setup_fast = setup.format(protocol, 'Accelerated', ', fallback=False')
     if not skip_slow:
-        setup_slow = setup.format(protocol, '')
+        setup_slow = setup.format(protocol, '', '')
 
     print("Starting Benchmarks")
 
diff --git a/test/py/RunClientServer.py b/test/py/RunClientServer.py
index 150f2be..d4a9cb2 100755
--- a/test/py/RunClientServer.py
+++ b/test/py/RunClientServer.py
@@ -22,7 +22,6 @@
 from __future__ import division
 from __future__ import print_function
 import copy
-import glob
 import os
 import signal
 import socket
@@ -31,10 +30,9 @@
 import time
 from optparse import OptionParser
 
+from util import local_libpath
+
 SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
-ROOT_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR))
-DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*')
-DEFAULT_LIBDIR_PY3 = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib')
 
 SCRIPTS = [
     'FastbinaryTest.py',
@@ -244,13 +242,6 @@
         return test_count
 
 
-def default_libdir():
-    if sys.version_info[0] == 2:
-        return glob.glob(DEFAULT_LIBDIR_GLOB)[0]
-    else:
-        return DEFAULT_LIBDIR_PY3
-
-
 def main():
     parser = OptionParser()
     parser.add_option('--all', action="store_true", dest='all')
@@ -265,7 +256,7 @@
     parser.add_option('-q', '--quiet', action="store_const",
                       dest="verbose", const=0,
                       help="minimal output")
-    parser.add_option('-L', '--libdir', dest="libdir", default=default_libdir(),
+    parser.add_option('-L', '--libdir', dest="libdir", default=local_libpath(),
                       help="directory path that contains Thrift Python library")
     parser.add_option('--gen-base', dest="gen_base", default=SCRIPT_DIR,
                       help="directory path that contains Thrift Python library")
diff --git a/test/py/SerializationTest.py b/test/py/SerializationTest.py
index d6308f0..f4f3a4f 100755
--- a/test/py/SerializationTest.py
+++ b/test/py/SerializationTest.py
@@ -292,7 +292,7 @@
 
 
 class AcceleratedBinaryTest(AbstractTest):
-    protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
+    protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False)
 
 
 class CompactProtocolTest(AbstractTest):
@@ -300,7 +300,7 @@
 
 
 class AcceleratedCompactTest(AbstractTest):
-    protocol_factory = TCompactProtocol.TCompactProtocolAcceleratedFactory()
+    protocol_factory = TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False)
 
 
 class JSONProtocolTest(AbstractTest):
diff --git a/test/py/TestClient.py b/test/py/TestClient.py
index e83a880..5de9fa3 100755
--- a/test/py/TestClient.py
+++ b/test/py/TestClient.py
@@ -26,9 +26,9 @@
 import unittest
 from optparse import OptionParser
 
+from util import local_libpath
+
 SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
-ROOT_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR))
-DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*')
 
 
 class AbstractTest(unittest.TestCase):
@@ -268,12 +268,12 @@
 
 class AcceleratedBinaryTest(AbstractTest):
     def get_protocol(self, transport):
-        return TBinaryProtocol.TBinaryProtocolAcceleratedFactory().getProtocol(transport)
+        return TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport)
 
 
 class AcceleratedCompactTest(AbstractTest):
     def get_protocol(self, transport):
-        return TCompactProtocol.TCompactProtocolAcceleratedFactory().getProtocol(transport)
+        return TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport)
 
 
 def suite():
@@ -333,10 +333,7 @@
 
     if options.genpydir:
         sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir))
-    if options.libpydir:
-        sys.path.insert(0, glob.glob(options.libpydir)[0])
-    else:
-        sys.path.insert(0, glob.glob(DEFAULT_LIBDIR_GLOB)[0])
+    sys.path.insert(0, local_libpath())
 
     from ThriftTest import ThriftTest
     from ThriftTest.ttypes import Xtruct, Xtruct2, Numberz, Xception, Xception2
diff --git a/test/py/TestEof.py b/test/py/TestEof.py
index 8901613..cda1050 100755
--- a/test/py/TestEof.py
+++ b/test/py/TestEof.py
@@ -107,8 +107,8 @@
 
     def testBinaryProtocolAcceleratedBinaryEof(self):
         """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream"""
-        self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
-        self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
+        self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False))
+        self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False))
 
     def testCompactProtocolEof(self):
         """Test that TCompactProtocol throws an EOFError when it reaches the end of the stream"""
@@ -117,8 +117,8 @@
 
     def testCompactProtocolAcceleratedCompactEof(self):
         """Test that TCompactProtocolAccelerated throws an EOFError when it reaches the end of the stream"""
-        self.eofTestHelper(TCompactProtocol.TCompactProtocolAcceleratedFactory())
-        self.eofTestHelperStress(TCompactProtocol.TCompactProtocolAcceleratedFactory())
+        self.eofTestHelper(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False))
+        self.eofTestHelperStress(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False))
 
 
 def suite():
diff --git a/test/py/TestFrozen.py b/test/py/TestFrozen.py
index f7c629b..e568e8c 100755
--- a/test/py/TestFrozen.py
+++ b/test/py/TestFrozen.py
@@ -102,12 +102,12 @@
 
 class TestFrozenAcceleratedBinary(TestFrozenBase):
     def protocol(self, trans):
-        return TBinaryProtocol.TBinaryProtocolAcceleratedFactory().getProtocol(trans)
+        return TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(trans)
 
 
 class TestFrozenAcceleratedCompact(TestFrozenBase):
     def protocol(self, trans):
-        return TCompactProtocol.TCompactProtocolAcceleratedFactory().getProtocol(trans)
+        return TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(trans)
 
 
 def suite():
diff --git a/test/py/TestServer.py b/test/py/TestServer.py
index 8819821..070560c 100755
--- a/test/py/TestServer.py
+++ b/test/py/TestServer.py
@@ -19,16 +19,15 @@
 # under the License.
 #
 from __future__ import division
-import glob
 import logging
 import os
 import sys
 import time
 from optparse import OptionParser
 
+from util import local_libpath
+
 SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
-ROOT_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR))
-DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*')
 
 
 class TestHandler(object):
@@ -300,10 +299,7 @@
     logging.basicConfig(level=options.verbose)
 
     sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir))
-    if options.libpydir:
-        sys.path.insert(0, glob.glob(options.libpydir)[0])
-    else:
-        sys.path.insert(0, glob.glob(DEFAULT_LIBDIR_GLOB)[0])
+    sys.path.insert(0, local_libpath())
 
     from ThriftTest import ThriftTest
     from ThriftTest.ttypes import Xtruct, Xception, Xception2, Insanity
diff --git a/test/py/util.py b/test/py/util.py
new file mode 100644
index 0000000..c2b3f5c
--- /dev/null
+++ b/test/py/util.py
@@ -0,0 +1,32 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import glob
+import os
+import sys
+
+_SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
+_ROOT_DIR = os.path.dirname(os.path.dirname(_SCRIPT_DIR))
+
+
+def local_libpath():
+    globdir = os.path.join(_ROOT_DIR, 'lib', 'py', 'build', 'lib.*')
+    for libpath in glob.glob(globdir):
+        if libpath.endswith('-%d.%d' % (sys.version_info[0], sys.version_info[1])):
+            return libpath