THRIFT-3612 Add Python C extension for compact protocol
Client: Python
Patch: Nobuaki Sukegawa

This closes #844
diff --git a/lib/py/src/protocol/TBinaryProtocol.py b/lib/py/src/protocol/TBinaryProtocol.py
index 7fce12f..f6be772 100644
--- a/lib/py/src/protocol/TBinaryProtocol.py
+++ b/lib/py/src/protocol/TBinaryProtocol.py
@@ -258,6 +258,7 @@
     We inherit from TBinaryProtocol so that the normal TBinaryProtocol
     encoding can happen if the fastbinary module doesn't work for some
     reason.  (TODO(dreiss): Make this happen sanely in more cases.)
+    To disable this behavior, pass fallback=False constructor argument.
 
     In order to take advantage of the C module, just use
     TBinaryProtocolAccelerated instead of TBinaryProtocol.
@@ -270,16 +271,31 @@
     """
     pass
 
+    def __init__(self, *args, **kwargs):
+        fallback = kwargs.pop('fallback', True)
+        super(TBinaryProtocolAccelerated, self).__init__(*args, **kwargs)
+        try:
+            from thrift.protocol import fastbinary
+        except ImportError:
+            if not fallback:
+                raise
+        else:
+            self._fast_decode = fastbinary.decode_binary
+            self._fast_encode = fastbinary.encode_binary
+
 
 class TBinaryProtocolAcceleratedFactory(object):
     def __init__(self,
                  string_length_limit=None,
-                 container_length_limit=None):
+                 container_length_limit=None,
+                 fallback=True):
         self.string_length_limit = string_length_limit
         self.container_length_limit = container_length_limit
+        self._fallback = fallback
 
     def getProtocol(self, trans):
         return TBinaryProtocolAccelerated(
             trans,
             string_length_limit=self.string_length_limit,
-            container_length_limit=self.container_length_limit)
+            container_length_limit=self.container_length_limit,
+            fallback=self._fallback)