Skip to content

Commit c8eeb68

Browse files
cclaussBridgeAR
authored andcommitted
tools: fix Python 3 issues in inspector_protocol
PR-URL: #29296 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 64461ee commit c8eeb68

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

tools/compress_json.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77

88
try:
99
xrange # Python 2
10+
PY2 = True
1011
except NameError:
12+
PY2 = False
1113
xrange = range # Python 3
1214

1315

1416
if __name__ == '__main__':
15-
fp = open(sys.argv[1])
16-
obj = json.load(fp)
17-
text = json.dumps(obj, separators=(',', ':'))
17+
with open(sys.argv[1]) as fp:
18+
obj = json.load(fp)
19+
text = json.dumps(obj, separators=(',', ':')).encode('utf-8')
1820
data = zlib.compress(text, zlib.Z_BEST_COMPRESSION)
1921

2022
# To make decompression a little easier, we prepend the compressed data
@@ -24,8 +26,8 @@
2426

2527
step = 20
2628
slices = (data[i:i+step] for i in xrange(0, len(data), step))
27-
slices = [','.join(str(ord(c)) for c in s) for s in slices]
29+
slices = [','.join(str(ord(c) if PY2 else c) for c in s) for s in slices]
2830
text = ',\n'.join(slices)
2931

30-
fp = open(sys.argv[2], 'w')
31-
fp.write(text)
32+
with open(sys.argv[2], 'w') as fp:
33+
fp.write(text)

tools/inspector_protocol/convert_protocol_to_json.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ def main(argv):
2121
parser.add_argument("json_file", help="The .json output file write.")
2222
args = parser.parse_args(argv)
2323
file_name = os.path.normpath(args.pdl_file)
24-
input_file = open(file_name, "r")
25-
pdl_string = input_file.read()
24+
with open(file_name, "r") as input_file:
25+
pdl_string = input_file.read()
2626
protocol = pdl.loads(pdl_string, file_name, args.map_binary_to_string)
27-
input_file.close()
2827

29-
output_file = open(os.path.normpath(args.json_file), 'wb')
30-
json.dump(protocol, output_file, indent=4, separators=(',', ': '))
31-
output_file.close()
28+
with open(os.path.normpath(args.json_file), 'w') as output_file:
29+
json.dump(protocol, output_file, indent=4, separators=(',', ': '))
3230

3331

3432
if __name__ == '__main__':

0 commit comments

Comments
 (0)