Support both Python2 and Python3 in the exported-sql-viewer.py script. The use of 'from __future__' implies the minimum supported Python2 version is now v2.6
Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all
Unified · Split
| @@ -88,11 +88,20 @@ · 87 unmodified lines | ||
| 88 | 88 | # 7fab593ea956 48 89 15 3b 13 22 00 movq %rdx, 0x22133b(%rip) |
| 89 | 89 | # 8107675243232 2 ls 22011 22011 hardware interrupt No 7fab593ea956 _dl_start+0x26 (ld-2.19.so) -> ffffffff86a012e0 page_fault ([kernel]) |
| 90 | 90 | |
| 91 | +from __future__ import print_function | |
| 92 | + | |
| 91 | 93 | import sys |
| 92 | 94 | import weakref |
| 93 | 95 | import threading |
| 94 | 96 | import string |
| 95 | −import cPickle | |
| 97 | +try: | |
| 98 | + # Python2 | |
| 99 | + import cPickle as pickle | |
| 100 | + # size of pickled integer big enough for record size | |
| 101 | + glb_nsz = 8 | |
| 102 | +except ImportError: | |
| 103 | + import pickle | |
| 104 | + glb_nsz = 16 | |
| 96 | 105 | import re |
| 97 | 106 | import os |
| 98 | 107 | from PySide.QtCore import * |
| @@ -102,6 +111,15 @@ from decimal import * · 3 unmodified lines | ||
| 102 | 111 | from ctypes import * |
| 103 | 112 | from multiprocessing import Process, Array, Value, Event |
| 104 | 113 | |
| 114 | +# xrange is range in Python3 | |
| 115 | +try: | |
| 116 | + xrange | |
| 117 | +except NameError: | |
| 118 | + xrange = range | |
| 119 | + | |
| 120 | +def printerr(*args, **keyword_args): | |
| 121 | + print(*args, file=sys.stderr, **keyword_args) | |
| 122 | + | |
| 105 | 123 | # Data formatting helpers |
| 106 | 124 | |
| 107 | 125 | def tohex(ip): |
| @@ -1004,10 +1022,6 @@ class ChildDataItemFinder(): · 896 unmodified lines | ||
| 1004 | 1022 | |
| 1005 | 1023 | glb_chunk_sz = 10000 |
| 1006 | 1024 | |
| 1007 | −# size of pickled integer big enough for record size | |
| 1008 | − | |
| 1009 | −glb_nsz = 8 | |
| 1010 | − | |
| 1011 | 1025 | # Background process for SQL data fetcher |
| 1012 | 1026 | |
| 1013 | 1027 | class SQLFetcherProcess(): |
| @@ -1066,7 +1080,7 @@ class SQLFetcherProcess(): · 52 unmodified lines | ||
| 1066 | 1080 | return True |
| 1067 | 1081 | if space >= glb_nsz: |
| 1068 | 1082 | # Use 0 (or space < glb_nsz) to mean there is no more at the top of the buffer |
| 1069 | − nd = cPickle.dumps(0, cPickle.HIGHEST_PROTOCOL) | |
| 1083 | + nd = pickle.dumps(0, pickle.HIGHEST_PROTOCOL) | |
| 1070 | 1084 | self.buffer[self.local_head : self.local_head + len(nd)] = nd |
| 1071 | 1085 | self.local_head = 0 |
| 1072 | 1086 | if self.local_tail - self.local_head > sz: |
| @@ -1084,9 +1098,9 @@ class SQLFetcherProcess(): · 11 unmodified lines | ||
| 1084 | 1098 | self.wait_event.wait() |
| 1085 | 1099 | |
| 1086 | 1100 | def AddToBuffer(self, obj): |
| 1087 | − d = cPickle.dumps(obj, cPickle.HIGHEST_PROTOCOL) | |
| 1101 | + d = pickle.dumps(obj, pickle.HIGHEST_PROTOCOL) | |
| 1088 | 1102 | n = len(d) |
| 1089 | − nd = cPickle.dumps(n, cPickle.HIGHEST_PROTOCOL) | |
| 1103 | + nd = pickle.dumps(n, pickle.HIGHEST_PROTOCOL) | |
| 1090 | 1104 | sz = n + glb_nsz |
| 1091 | 1105 | self.WaitForSpace(sz) |
| 1092 | 1106 | pos = self.local_head |
| @@ -1198,12 +1212,12 @@ class SQLFetcher(QObject): · 105 unmodified lines | ||
| 1198 | 1212 | pos = self.local_tail |
| 1199 | 1213 | if len(self.buffer) - pos < glb_nsz: |
| 1200 | 1214 | pos = 0 |
| 1201 | − n = cPickle.loads(self.buffer[pos : pos + glb_nsz]) | |
| 1215 | + n = pickle.loads(self.buffer[pos : pos + glb_nsz]) | |
| 1202 | 1216 | if n == 0: |
| 1203 | 1217 | pos = 0 |
| 1204 | − n = cPickle.loads(self.buffer[0 : glb_nsz]) | |
| 1218 | + n = pickle.loads(self.buffer[0 : glb_nsz]) | |
| 1205 | 1219 | pos += glb_nsz |
| 1206 | − obj = cPickle.loads(self.buffer[pos : pos + n]) | |
| 1220 | + obj = pickle.loads(self.buffer[pos : pos + n]) | |
| 1207 | 1221 | self.local_tail = pos + n |
| 1208 | 1222 | return obj |
| 1209 | 1223 | |
| @@ -2973,7 +2987,7 @@ class DBRef(): · 1763 unmodified lines | ||
| 2973 | 2987 | |
| 2974 | 2988 | def Main(): |
| 2975 | 2989 | if (len(sys.argv) < 2): |
| 2976 | − print >> sys.stderr, "Usage is: exported-sql-viewer.py {<database name> | --help-only}" | |
| 2990 | + printerr("Usage is: exported-sql-viewer.py {<database name> | --help-only}"); | |
| 2977 | 2991 | raise Exception("Too few arguments") |
| 2978 | 2992 | |
| 2979 | 2993 | dbname = sys.argv[1] |
| @@ -2986,8 +3000,8 @@ def Main(): · 6 unmodified lines | ||
| 2986 | 3000 | |
| 2987 | 3001 | is_sqlite3 = False |
| 2988 | 3002 | try: |
| 2989 | − f = open(dbname) | |
| 2990 | − if f.read(15) == "SQLite format 3": | |
| 3003 | + f = open(dbname, "rb") | |
| 3004 | + if f.read(15) == b'SQLite format 3': | |
| 2991 | 3005 | is_sqlite3 = True |
| 2992 | 3006 | f.close() |
| 2993 | 3007 | except: |