Skip to content

Commit d935e95

Browse files
author
Thomas Preud'homme
committed
[LNT] Python 3 support: use Python 3 module names
Summary: Adapt imports to use Python 3 module names by installing them as aliases. Due to the clear separation between text and binary data in Python 3, this also requires changing all I/O in profilev2impl to be binary I/O with encoding/decoding for text and some adaptation to the integer to ULEB conversion code. Installing aliases was produced by running futurize's stage2 libfuturize.fixes.fix_future_standard_library and further edited by hand to adapt that I/O code. Reviewers: cmatthews, hubert.reinterpretcast, kristof.beyls, leandron, PrzemekWirkus Reviewed By: PrzemekWirkus Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D67823
1 parent a99f861 commit d935e95

File tree

11 files changed

+49
-32
lines changed

11 files changed

+49
-32
lines changed

lnt/lnttool/admin.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#!/usr/bin/env python
2+
from future import standard_library
3+
standard_library.install_aliases()
24
import click
35
from .common import submit_options
46

57

68
def _load_dependencies():
7-
global yaml, sys, requests, json, os, httplib
9+
global yaml, sys, requests, json, os, http
810
import yaml
911
import sys
1012
import requests
1113
import json
1214
import os
13-
import httplib
15+
import http.client
1416

1517

1618
def _error(msg):
@@ -101,7 +103,7 @@ def _check_response(response):
101103
return
102104

103105
sys.stderr.write("%d: %s\n" %
104-
(status_code, httplib.responses.get(status_code, '')))
106+
(status_code, http.client.responses.get(status_code, '')))
105107
sys.stderr.write("\n%s\n" % response.text)
106108
sys.exit(1)
107109

lnt/lnttool/viewcomparison.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def action_view_comparison(report_a, report_b, hostname, port, dry_run,
6060
import os
6161
import shutil
6262
import tempfile
63-
import thread
63+
import _thread
6464

6565
init_logger(logging.ERROR)
6666

@@ -98,7 +98,7 @@ def action_view_comparison(report_a, report_b, hostname, port, dry_run,
9898
logger.info("opening comparison view: %s" % (comparison_url,))
9999

100100
if not dry_run:
101-
thread.start_new_thread(_start_browser, (comparison_url, True))
101+
_thread.start_new_thread(_start_browser, (comparison_url, True))
102102

103103
# Run the webserver.
104104
app = lnt.server.ui.app.App.create_with_instance(instance)

lnt/server/ui/app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import print_function
2-
import StringIO
2+
from future import standard_library
3+
standard_library.install_aliases()
4+
import io
35
import logging
46
import logging.handlers
57
import sys
@@ -97,7 +99,7 @@ def get_db(self):
9799
# FIXME: Conditionalize on an is_production variable.
98100
show_sql = bool(self.args.get('db_log') or self.form.get('db_log'))
99101
if show_sql:
100-
g.db_log = StringIO.StringIO()
102+
g.db_log = io.StringIO()
101103
logger = logging.getLogger("sqlalchemy")
102104
logger.addHandler(logging.StreamHandler(g.db_log))
103105
return self.db

lnt/server/ui/filters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from future import standard_library
22
standard_library.install_aliases()
3-
import StringIO
3+
import io
44
import datetime
55
import pprint
66
import urllib.parse
@@ -26,7 +26,7 @@ def filter_aspctcell(value, class_=None, style=None, attributes=None, *args,
2626

2727

2828
def filter_pprint(value):
29-
stream = StringIO.StringIO()
29+
stream = io.StringIO()
3030
pprint.pprint(value, stream)
3131
return stream.getvalue()
3232

lnt/server/ui/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from future import standard_library
2+
standard_library.install_aliases()
13
import datetime
24
import json
35
import os
46
import re
57
import time
68
from collections import namedtuple, defaultdict
7-
from urlparse import urlparse, urljoin
9+
from urllib.parse import urlparse, urljoin
810

911
import flask
1012
import sqlalchemy.sql

lnt/testing/profile/profilev1impl.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from future import standard_library
2+
standard_library.install_aliases()
13
from lnt.testing.profile.profile import ProfileImpl
2-
import cPickle
4+
import pickle
35
import zlib
46

57

@@ -48,11 +50,11 @@ def checkFile(fn):
4850
@staticmethod
4951
def deserialize(fobj):
5052
o = zlib.decompress(fobj.read())
51-
data = cPickle.loads(o)
53+
data = pickle.loads(o)
5254
return ProfileV1(data)
5355

5456
def serialize(self, fname=None):
55-
obj = cPickle.dumps(self.data)
57+
obj = pickle.dumps(self.data)
5658
compressed_obj = zlib.compress(obj)
5759

5860
if fname is None:

lnt/testing/profile/profilev2impl.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import absolute_import
22
from builtins import range
3+
from future import standard_library
4+
standard_library.install_aliases()
35
from .profile import ProfileImpl
4-
import StringIO
56
import bz2
67
import copy
78
import io
@@ -92,7 +93,7 @@ def readNum(fobj):
9293
n = 0
9394
shift = 0
9495
while True:
95-
b = ord(fobj.read(1))
96+
b = bytearray(fobj.read(1))[0]
9697
n |= (b & 0x7F) << shift
9798
shift += 7
9899
if (b & 0x80) == 0:
@@ -108,7 +109,7 @@ def writeNum(fobj, n):
108109
n >>= 7
109110
if n != 0:
110111
b |= 0x80
111-
fobj.write(chr(b))
112+
fobj.write(bytearray([b]))
112113

113114
if n == 0:
114115
break
@@ -118,15 +119,15 @@ def readString(fobj):
118119
"""
119120
Read a string from a stream.
120121
"""
121-
return fobj.readline()[:-1]
122+
return fobj.readline()[:-1].decode()
122123

123124

124125
def writeString(fobj, s):
125126
"""
126127
Write a string to a stream.
127128
"""
128-
fobj.write(str(s))
129-
fobj.write('\n')
129+
fobj.write(s.encode())
130+
fobj.write(u'\n'.encode())
130131

131132

132133
def readFloat(fobj):
@@ -183,7 +184,7 @@ def copy(self):
183184
class CompressedSection(Section):
184185
def read(self, fobj):
185186
fobj.seek(self.offset + self.start)
186-
_io = StringIO.StringIO(bz2.decompress(fobj.read(self.size)))
187+
_io = io.BytesIO(bz2.decompress(fobj.read(self.size)))
187188
return self.deserialize(_io)
188189

189190
def write(self, fobj):
@@ -214,12 +215,12 @@ def read(self, fobj):
214215
raise NotImplementedError()
215216

216217
else:
217-
_io = StringIO.StringIO(bz2.decompress(fobj.read(self.size)))
218+
_io = io.BytesIO(bz2.decompress(fobj.read(self.size)))
218219
self.size = len(_io.getvalue())
219220
return self.deserialize(_io)
220221

221222
def write(self, fobj):
222-
_io = StringIO.StringIO()
223+
_io = io.BytesIO()
223224
if self.pool_fname:
224225
raise NotImplementedError()
225226

@@ -334,7 +335,7 @@ def setOffsetFor(self, fname, value):
334335

335336
def extractForFunction(self, fname, counters):
336337
offset = self.function_offsets[fname]
337-
_io = StringIO.StringIO(self.data)
338+
_io = io.BytesIO(self.data)
338339
_io.seek(offset)
339340
counters.sort()
340341
while True:
@@ -384,7 +385,7 @@ def setOffsetFor(self, fname, value):
384385

385386
def extractForFunction(self, fname):
386387
offset = self.function_offsets[fname]
387-
_io = StringIO.StringIO(self.data)
388+
_io = io.BytesIO(self.data)
388389
_io.seek(offset)
389390
last_address = 0
390391
while True:
@@ -436,7 +437,7 @@ def setOffsetFor(self, fname, value):
436437
def extractForFunction(self, fname):
437438
offset = self.function_offsets[fname]
438439

439-
_io = StringIO.StringIO(self.data)
440+
_io = io.BytesIO(self.data)
440441
_io.seek(offset)
441442
while True:
442443
n = readNum(_io)
@@ -455,7 +456,7 @@ def __init__(self):
455456
# Populate data with a single character initially so that zero is
456457
# never a valid string pool index. LineText relies upon this to use
457458
# zero as a sentinel.
458-
self.data = StringIO.StringIO('\n')
459+
self.data = io.BytesIO(u'\n'.encode())
459460
self.pool_read = False
460461

461462
def serialize(self, fobj):
@@ -464,7 +465,7 @@ def serialize(self, fobj):
464465

465466
def deserialize(self, fobj):
466467
# FIXME: Make this lazy!
467-
self.data = StringIO.StringIO(fobj.read(self.size))
468+
self.data = io.BytesIO(fobj.read(self.size))
468469

469470
def upgrade(self, impl):
470471
pass
@@ -591,7 +592,7 @@ def deserialize(fobj):
591592
def serialize(self, fname=None):
592593
# If we're not writing to a file, emulate a file object instead.
593594
if fname is None:
594-
fobj = StringIO.StringIO()
595+
fobj = io.BytesIO()
595596
else:
596597
fobj = open(fname, 'wb')
597598

@@ -613,7 +614,7 @@ def serialize(self, fname=None):
613614

614615
# We need to write all sections first, so we know their offset
615616
# before we write the header.
616-
tmpio = StringIO.StringIO()
617+
tmpio = io.BytesIO()
617618
offsets = {}
618619
sizes = {}
619620
for section in sections:

lnt/testing/util/rcs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from future import standard_library
2+
standard_library.install_aliases()
13
import re
24
import os
35
from lnt.util import logger

lnt/tests/compile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Single file compile-time performance testing"""
22
from __future__ import print_function
3+
from future import standard_library
4+
standard_library.install_aliases()
35
import errno
46
import hashlib
57
import json

lnt/util/wsgi_restart.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# This code lifted from the mod_wsgi docs.
22
from __future__ import print_function
3+
from future import standard_library
4+
standard_library.install_aliases()
35
import os
46
import sys
57
import signal
68
import threading
79
import atexit
8-
import Queue
10+
import queue
911

1012
_interval = 1.0
1113
_times = {}
1214
_files = []
1315

1416
_running = False
15-
_queue = Queue.Queue()
17+
_queue = queue.Queue()
1618
_lock = threading.Lock()
1719

1820

0 commit comments

Comments
 (0)