diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2015-08-25 20:24:43 -0700 |
---|---|---|
committer | Feng Xiao <xfxyjwf@gmail.com> | 2015-08-25 20:24:43 -0700 |
commit | b192ba87f72b5e3e8add09bbcbb7623831e3079b (patch) | |
tree | affb3beff3c33050e7f93647ddab8606899ff03d /python/google/protobuf/text_encoding.py | |
parent | 5da0b46811a103bd1a953f496d4bcd5ff45d3736 (diff) | |
parent | cf94f7b74495d08b833056016e045a0ae3fb64fa (diff) | |
download | protobuf-javalite-b192ba87f72b5e3e8add09bbcbb7623831e3079b.tar.gz |
Merge remote-tracking branch 'origin/master' into beta-1
Diffstat (limited to 'python/google/protobuf/text_encoding.py')
-rw-r--r-- | python/google/protobuf/text_encoding.py | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/python/google/protobuf/text_encoding.py b/python/google/protobuf/text_encoding.py index 2d86a67c..a0728e3c 100644 --- a/python/google/protobuf/text_encoding.py +++ b/python/google/protobuf/text_encoding.py @@ -27,16 +27,13 @@ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (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. -# """Encoding related utilities.""" - import re -import sys ##PY25 + +import six # Lookup table for utf8 -_cescape_utf8_to_str = [chr(i) for i in xrange(0, 256)] +_cescape_utf8_to_str = [chr(i) for i in range(0, 256)] _cescape_utf8_to_str[9] = r'\t' # optional escape _cescape_utf8_to_str[10] = r'\n' # optional escape _cescape_utf8_to_str[13] = r'\r' # optional escape @@ -46,9 +43,9 @@ _cescape_utf8_to_str[34] = r'\"' # necessary escape _cescape_utf8_to_str[92] = r'\\' # necessary escape # Lookup table for non-utf8, with necessary escapes at (o >= 127 or o < 32) -_cescape_byte_to_str = ([r'\%03o' % i for i in xrange(0, 32)] + - [chr(i) for i in xrange(32, 127)] + - [r'\%03o' % i for i in xrange(127, 256)]) +_cescape_byte_to_str = ([r'\%03o' % i for i in range(0, 32)] + + [chr(i) for i in range(32, 127)] + + [r'\%03o' % i for i in range(127, 256)]) _cescape_byte_to_str[9] = r'\t' # optional escape _cescape_byte_to_str[10] = r'\n' # optional escape _cescape_byte_to_str[13] = r'\r' # optional escape @@ -75,7 +72,7 @@ def CEscape(text, as_utf8): """ # PY3 hack: make Ord work for str and bytes: # //platforms/networking/data uses unicode here, hence basestring. - Ord = ord if isinstance(text, basestring) else lambda x: x + Ord = ord if isinstance(text, six.string_types) else lambda x: x if as_utf8: return ''.join(_cescape_utf8_to_str[Ord(c)] for c in text) return ''.join(_cescape_byte_to_str[Ord(c)] for c in text) @@ -100,8 +97,7 @@ def CUnescape(text): # allow single-digit hex escapes (like '\xf'). result = _CUNESCAPE_HEX.sub(ReplaceHex, text) - if sys.version_info[0] < 3: ##PY25 -##!PY25 if str is bytes: # PY2 + if str is bytes: # PY2 return result.decode('string_escape') result = ''.join(_cescape_highbit_to_str[ord(c)] for c in result) return (result.encode('ascii') # Make it bytes to allow decode. |