diff options
author | Tres Seaver <tseaver@palladion.com> | 2015-01-13 14:21:29 -0500 |
---|---|---|
committer | Tres Seaver <tseaver@palladion.com> | 2015-01-13 14:22:05 -0500 |
commit | f336d4b7a5c1d369ed508e513d482c885705e939 (patch) | |
tree | 4d0b2d191780864b6c787780193e071f2ea7f434 /python/google/protobuf/text_encoding.py | |
parent | 052e0205a76717f39fc65e303fd2b92ab1df3028 (diff) | |
download | protobuf-f336d4b7a5c1d369ed508e513d482c885705e939.tar.gz protobuf-f336d4b7a5c1d369ed508e513d482c885705e939.tar.bz2 protobuf-f336d4b7a5c1d369ed508e513d482c885705e939.zip |
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').
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. |