Python 3.9 is now generally available.

Module: google.appengine.datastore.sortable_pb_encoder

An Encoder class for protocol buffers (PB) that preserves sorting characteristics.

This is used by datastore_sqlite_stub and datastore_types to match the ordering semantics of the production datastore. Broadly, there are four changes from regular PB encoding:

  • Strings are escaped and null terminated instead of length-prefixed. The escaping replaces \x00 with \x01\x01 and \x01 with \x01\x02, thus preserving the ordering of the original string.
  • Variable length integers are encoded using a variable length encoding that preserves order. The first byte stores the absolute value if it's between -119 to 119, otherwise it stores the number of bytes that follow.
  • Numbers are stored big endian instead of little endian.
  • Negative doubles are entirely negated, while positive doubles have their sign bit flipped.


Due to the way nested Protocol Buffers are encoded, this encoder will NOT preserve sorting characteristics for embedded protocol buffers!


class Decoder: Decoder that decodes a bytes buffer into a protocol buffer message.

class Encoder: Encodes protocol buffers in a form that sorts nicely.


EncodeDouble(...): Encode a double into a sortable byte buffer.


GetFieldNumber(...): Get the field number from the tuple returned by Message.ListFields.