Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
fc80874a
Commit
fc80874a
authored
Aug 15, 2015
by
Dan O'Reilly
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Start work on getting text handled properly on PY3
parent
3ff56252
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
7 deletions
+27
-7
text_format.py
python/google/protobuf/text_format.py
+27
-7
No files found.
python/google/protobuf/text_format.py
View file @
fc80874a
...
...
@@ -67,6 +67,29 @@ class Error(Exception):
class
ParseError
(
Error
):
"""Thrown in case of ASCII parsing error."""
class
TextWriter
(
object
):
def
__init__
(
self
,
as_utf8
):
self
.
_utf8
=
as_utf8
if
as_utf8
:
self
.
_writer
=
io
.
BytesIO
()
else
:
self
.
_writer
=
io
.
StringIO
()
def
write
(
self
,
val
):
if
self
.
_utf8
:
if
isinstance
(
val
,
six
.
text_type
):
val
=
val
.
encode
(
'utf-8'
)
else
:
if
isinstance
(
val
,
bytes
):
val
=
val
.
decode
(
'utf-8'
)
return
self
.
_writer
.
write
(
val
)
def
close
(
self
):
return
self
.
_writer
.
close
()
def
getvalue
(
self
):
return
self
.
_writer
.
getvalue
()
def
MessageToString
(
message
,
as_utf8
=
False
,
as_one_line
=
False
,
pointy_brackets
=
False
,
use_index_order
=
False
,
...
...
@@ -92,7 +115,7 @@ def MessageToString(message, as_utf8=False, as_one_line=False,
Returns:
A string of the text formatted protocol buffer message.
"""
out
=
io
.
BytesIO
(
)
out
=
TextWriter
(
as_utf8
)
PrintMessage
(
message
,
out
,
as_utf8
=
as_utf8
,
as_one_line
=
as_one_line
,
pointy_brackets
=
pointy_brackets
,
use_index_order
=
use_index_order
,
...
...
@@ -159,11 +182,7 @@ def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False,
# For groups, use the capitalized name.
out
.
write
(
field
.
message_type
.
name
)
else
:
if
isinstance
(
field
.
name
,
six
.
text_type
):
name
=
field
.
name
.
encode
(
'utf-8'
)
else
:
name
=
field
.
name
out
.
write
(
name
)
out
.
write
(
field
.
name
)
if
field
.
cpp_type
!=
descriptor
.
FieldDescriptor
.
CPPTYPE_MESSAGE
:
# The colon is optional in this case, but our cross-language golden files
...
...
@@ -226,7 +245,8 @@ def PrintFieldValue(field, value, out, indent=0, as_utf8=False,
out_as_utf8
=
False
else
:
out_as_utf8
=
as_utf8
out
.
write
(
text_encoding
.
CEscape
(
out_value
,
out_as_utf8
))
out_text
=
text_encoding
.
CEscape
(
out_value
,
out_as_utf8
)
out
.
write
(
out_text
)
out
.
write
(
'
\"
'
)
elif
field
.
cpp_type
==
descriptor
.
FieldDescriptor
.
CPPTYPE_BOOL
:
if
value
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment