Merge lp:~pconnell/entrails/2and3 into lp:entrails

Proposed by Phil Connell
Status: Merged
Approved by: Martin Morrison
Approved revision: 21
Merged at revision: 20
Proposed branch: lp:~pconnell/entrails/2and3
Merge into: lp:entrails
Diff against target: 191 lines (+41/-35)
3 files modified
src/entrails/_output.py (+19/-17)
src/entrails/_trace.py (+5/-3)
src/trace.py (+17/-15)
To merge this branch: bzr merge lp:~pconnell/entrails/2and3
Reviewer Review Type Date Requested Status
Martin Morrison Approve
Review via email: mp+225819@code.launchpad.net

Commit message

Updates to run on both Python 2 and 3.

Description of the change

Updates to run on both Python 2 and 3.

Approach is very simple: ran 2to3 over the code and added a few extra __future__ imports to make Python 2 work, i.e. most of the changes are auto-generated.

Background/Motivation:
  - Anything using twisted is stuck on Python 2, can't drop support yet.
  - Future tools (e.g. sextant) and most development without dependencies on twisted is all Python 3.
  - So, need to support both for forseeable future. Eventual goal is to drop Python 2 support.

To post a comment you must log in.
Revision history for this message
Martin Morrison (isoschiz) wrote :

Didn't really review code; approach sounds reasonable. Sextant sounds exciting. ;-)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/entrails/_output.py'
2--- src/entrails/_output.py 2014-07-07 08:59:06 +0000
3+++ src/entrails/_output.py 2014-07-07 12:55:36 +0000
4@@ -6,6 +6,8 @@
5 # Copyright 2012, 2014, Ensoft Ltd.
6 # -----------------------------------------------------------------------------
7
8+from __future__ import absolute_import, print_function
9+
10 """Output generators."""
11
12 __all__ = (
13@@ -138,36 +140,36 @@
14 Displays all output data with little formatting.
15 """
16 def __init__(self):
17- print "Entrails debug output started on {0}".format(
18- datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
19+ print("Entrails debug output started on {0}".format(
20+ datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
21
22 def close(self):
23- print "Entrails debug output ended on {0}".format(
24- datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
25+ print("Entrails debug output ended on {0}".format(
26+ datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
27
28 def enter(self, o):
29- print "FUNCTION CALL: " + o.call_string()
30- for k, v in o.iteritems():
31- print "\t{0}:{1}".format(k, str(v))
32+ print("FUNCTION CALL: " + o.call_string())
33+ for k, v in o.items():
34+ print("\t{0}:{1}".format(k, str(v)))
35 return None
36
37 def exit(self, o):
38- print "FUNCTION EXIT"
39- for k, v in o.iteritems():
40- print "\t{0}:{1}".format(k, str(v))
41+ print("FUNCTION EXIT")
42+ for k, v in o.items():
43+ print("\t{0}:{1}".format(k, str(v)))
44 return None
45
46 def exception(self, o):
47- print "EXCEPTION on line {0} in {1}".format(o["f_lineno"],
48- o["co_filename"])
49- for k, v in o.iteritems():
50- print k + ": " + str(v)
51+ print("EXCEPTION on line {0} in {1}".format(o["f_lineno"],
52+ o["co_filename"]))
53+ for k, v in o.items():
54+ print(k + ": " + str(v))
55 return None
56
57 def logging(self, o, event_type_string):
58- print "ERROR TRACING with event: {0} on line {1} in {2}".format(event_type_string,
59+ print("ERROR TRACING with event: {0} on line {1} in {2}".format(event_type_string,
60 o["f_lineno"],
61- o["co_filename"])
62+ o["co_filename"]))
63 return None
64
65
66@@ -215,7 +217,7 @@
67 def write_out(self, codeframe, event_type):
68 event = ET.SubElement(self.root, "event")
69 event.attrib = {"timestamp" : "{0:f}".format(codeframe["timestamp"])}
70- for k, v in codeframe.iteritems():
71+ for k, v in codeframe.items():
72 if k != "timestamp":
73 ET.SubElement(event, k).text = str(v)
74 ET.SubElement(event, "type").text = event_type
75
76=== modified file 'src/entrails/_trace.py'
77--- src/entrails/_trace.py 2014-07-07 08:59:06 +0000
78+++ src/entrails/_trace.py 2014-07-07 12:55:36 +0000
79@@ -6,6 +6,8 @@
80 # Copyright 2012, 2014, Ensoft Ltd.
81 # -----------------------------------------------------------------------------
82
83+from __future__ import absolute_import, print_function
84+
85 """Execution tracer."""
86
87 __all__ = (
88@@ -38,7 +40,7 @@
89 """
90 # We rely on CPython putting arguments at start of f_locals (in reverse
91 # order for some reason...). This is undocumented, but consistent.
92- parameters = [(k, v) for k, v in self["f_locals"].iteritems()
93+ parameters = [(k, v) for k, v in self["f_locals"].items()
94 if k in self["co_varnames"][0:self["co_argcount"]]]
95 parameters.reverse()
96 return collections.OrderedDict(parameters)
97@@ -52,7 +54,7 @@
98 """
99 # Possibly chop off first self argument
100 ignorer = 0 if not self.is_method() else 1
101- return self.parameters().values()[ignorer:]
102+ return list(self.parameters().values())[ignorer:]
103
104 def string_arguments(self):
105 """
106@@ -151,7 +153,7 @@
107 try:
108 # This is confusing. We go into the previous (parent) frame and
109 # find the local variable name in that pointing to a our class.
110- for k, v in self['f_back'].f_locals.items():
111+ for k, v in list(self['f_back'].f_locals.items()):
112 if v == self['f_locals']['self']:
113 return k
114 except:
115
116=== modified file 'src/trace.py'
117--- src/trace.py 2014-07-07 08:59:06 +0000
118+++ src/trace.py 2014-07-07 12:55:36 +0000
119@@ -5,6 +5,8 @@
120 # Created by Ben Eills
121 # ----------------------------------------------------------------------------
122
123+from __future__ import absolute_import, print_function
124+
125 """
126 Convienience script to add/remove EnTrails tracing from a Python program.
127
128@@ -45,21 +47,21 @@
129 first=True
130 for line in fileinput.input(filename, inplace=1, backup=".backup"):
131 if first and line.startswith("#!/"):
132- print line,
133+ print(line, end=' ')
134 continue
135 if first:
136- print MARKER
137- print "import entrails"
138+ print(MARKER)
139+ print("import entrails")
140 if include_std_lib:
141- print "_entrails_t = entrails.Entrails(\"%s\", filters=())" % label
142+ print("_entrails_t = entrails.Entrails(\"%s\", filters=())" % label)
143 else:
144- print "_entrails_t = entrails.Entrails(\"%s\")" % label
145- print output_code_string
146- print "_entrails_t.add_output(_entrails_o)"
147- print "_entrails_t.start_trace()"
148- print ENDMARKER
149+ print("_entrails_t = entrails.Entrails(\"%s\")" % label)
150+ print(output_code_string)
151+ print("_entrails_t.add_output(_entrails_o)")
152+ print("_entrails_t.start_trace()")
153+ print(ENDMARKER)
154 first = False
155- print line,
156+ print(line, end=' ')
157 fileinput.close()
158
159
160@@ -116,12 +118,12 @@
161
162 if args.action == "add":
163 if not already_modified(args.sourcefile):
164- print "Adding entrails code to " + args.sourcefile
165- print "Backing up file to " + args.sourcefile + ".backup"
166+ print("Adding entrails code to " + args.sourcefile)
167+ print("Backing up file to " + args.sourcefile + ".backup")
168 insert_header(args.sourcefile, output_code_string, label, args.include_std_lib)
169 insert_footer(args.sourcefile)
170 else:
171- print "File already has entrails code. Run with 'remove' option to remove code."
172+ print("File already has entrails code. Run with 'remove' option to remove code.")
173
174 elif args.action == "remove":
175 extra=False
176@@ -129,13 +131,13 @@
177 if line.startswith(MARKER):
178 extra = True
179 if not extra:
180- print line,
181+ print(line, end=' ')
182 if line.startswith(ENDMARKER):
183 extra = False
184 fileinput.close()
185
186 else:
187- print "Invalid action: " + args.action[0]
188+ print("Invalid action: " + args.action[0])
189 parser.print_help()
190
191 if __name__ == '__main__':

Subscribers

People subscribed via source and target branches