From f336d4b7a5c1d369ed508e513d482c885705e939 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 13 Jan 2015 14:21:29 -0500 Subject: Prepare for Python2-Python3 straddle. - Remove PY25 cruft. - Selectively apply cleanups from 'python-modernize': - New exception syntax. - Use 'six' to handle module renames. - Use 'six' to handle text / binary stuff. This PR covers most of the work from #66 which falls inside `python` (rather than the Python code generation stuff in 'src'). --- python/google/protobuf/internal/encoder.py | 50 ++++++++++-------------------- 1 file changed, 17 insertions(+), 33 deletions(-) (limited to 'python/google/protobuf/internal/encoder.py') diff --git a/python/google/protobuf/internal/encoder.py b/python/google/protobuf/internal/encoder.py index 38a5138a..21ed2ed7 100755 --- a/python/google/protobuf/internal/encoder.py +++ b/python/google/protobuf/internal/encoder.py @@ -28,8 +28,6 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#PY25 compatible for GAE. -# # Copyright 2009 Google Inc. All Rights Reserved. """Code for encoding protocol message primitives. @@ -71,8 +69,9 @@ sizer rather than when calling them. In particular: __author__ = 'kenton@google.com (Kenton Varda)' import struct -import sys ##PY25 -_PY2 = sys.version_info[0] < 3 ##PY25 + +import six + from google.protobuf.internal import wire_format @@ -346,16 +345,14 @@ def MessageSetItemSizer(field_number): def _VarintEncoder(): """Return an encoder for a basic varint value (does not include tag).""" - local_chr = _PY2 and chr or (lambda x: bytes((x,))) ##PY25 -##!PY25 local_chr = chr if bytes is str else lambda x: bytes((x,)) def EncodeVarint(write, value): bits = value & 0x7f value >>= 7 while value: - write(local_chr(0x80|bits)) + write(six.int2byte(0x80|bits)) bits = value & 0x7f value >>= 7 - return write(local_chr(bits)) + return write(six.int2byte(bits)) return EncodeVarint @@ -364,18 +361,16 @@ def _SignedVarintEncoder(): """Return an encoder for a basic signed varint value (does not include tag).""" - local_chr = _PY2 and chr or (lambda x: bytes((x,))) ##PY25 -##!PY25 local_chr = chr if bytes is str else lambda x: bytes((x,)) def EncodeSignedVarint(write, value): if value < 0: value += (1 << 64) bits = value & 0x7f value >>= 7 while value: - write(local_chr(0x80|bits)) + write(six.int2byte(0x80|bits)) bits = value & 0x7f value >>= 7 - return write(local_chr(bits)) + return write(six.int2byte(bits)) return EncodeSignedVarint @@ -390,8 +385,7 @@ def _VarintBytes(value): pieces = [] _EncodeVarint(pieces.append, value) - return "".encode("latin1").join(pieces) ##PY25 -##!PY25 return b"".join(pieces) + return b"".join(pieces) def TagBytes(field_number, wire_type): @@ -529,33 +523,26 @@ def _FloatingPointEncoder(wire_type, format): format: The format string to pass to struct.pack(). """ - b = _PY2 and (lambda x:x) or (lambda x:x.encode('latin1')) ##PY25 value_size = struct.calcsize(format) if value_size == 4: def EncodeNonFiniteOrRaise(write, value): # Remember that the serialized form uses little-endian byte order. if value == _POS_INF: - write(b('\x00\x00\x80\x7F')) ##PY25 -##!PY25 write(b'\x00\x00\x80\x7F') + write(b'\x00\x00\x80\x7F') elif value == _NEG_INF: - write(b('\x00\x00\x80\xFF')) ##PY25 -##!PY25 write(b'\x00\x00\x80\xFF') + write(b'\x00\x00\x80\xFF') elif value != value: # NaN - write(b('\x00\x00\xC0\x7F')) ##PY25 -##!PY25 write(b'\x00\x00\xC0\x7F') + write(b'\x00\x00\xC0\x7F') else: raise elif value_size == 8: def EncodeNonFiniteOrRaise(write, value): if value == _POS_INF: - write(b('\x00\x00\x00\x00\x00\x00\xF0\x7F')) ##PY25 -##!PY25 write(b'\x00\x00\x00\x00\x00\x00\xF0\x7F') + write(b'\x00\x00\x00\x00\x00\x00\xF0\x7F') elif value == _NEG_INF: - write(b('\x00\x00\x00\x00\x00\x00\xF0\xFF')) ##PY25 -##!PY25 write(b'\x00\x00\x00\x00\x00\x00\xF0\xFF') + write(b'\x00\x00\x00\x00\x00\x00\xF0\xFF') elif value != value: # NaN - write(b('\x00\x00\x00\x00\x00\x00\xF8\x7F')) ##PY25 -##!PY25 write(b'\x00\x00\x00\x00\x00\x00\xF8\x7F') + write(b'\x00\x00\x00\x00\x00\x00\xF8\x7F') else: raise else: @@ -631,10 +618,8 @@ DoubleEncoder = _FloatingPointEncoder(wire_format.WIRETYPE_FIXED64, '