Merge ~cjwatson/launchpad:print-flush into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 1f139eb5f731ac98c06f75dd8d84ba475d3eda99
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:print-flush
Merge into: launchpad:master
Diff against target: 198 lines (+16/-34)
9 files modified
database/replication/helpers.py (+1/-2)
lib/lp/app/doc/menus.rst (+1/-2)
lib/lp/archivepublisher/tests/test_publish_ftpmaster.py (+1/-2)
lib/lp/codehosting/tests/test_rewrite.py (+4/-8)
lib/lp/services/pidfile.py (+1/-2)
lib/lp/services/profile/mem.py (+1/-2)
lib/lp/services/scripts/tests/test_cronscript_enabled.py (+2/-3)
lib/lp/services/tests/test_command_spawner.py (+3/-9)
scripts/branch-rewrite.py (+2/-4)
Reviewer Review Type Date Requested Status
Jürgen Gmach Approve
Review via email: mp+431818@code.launchpad.net

Commit message

Use print(flush=True) where appropriate

Description of the change

This was added in Python 3.3, and lets us make a few things a bit more concise and (IMO) clearer.

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/database/replication/helpers.py b/database/replication/helpers.py
2index f1f8349..5ab5b50 100644
3--- a/database/replication/helpers.py
4+++ b/database/replication/helpers.py
5@@ -227,8 +227,7 @@ def execute_slonik(script, sync=None, exit_on_fail=True, auto_preamble=True):
6 # to slonik via stdin. This way it can be examined if slonik appears
7 # to hang.
8 script_on_disk = NamedTemporaryFile(prefix="slonik", suffix=".sk")
9- print(script, file=script_on_disk)
10- script_on_disk.flush()
11+ print(script, file=script_on_disk, flush=True)
12
13 # Run slonik
14 log.debug("Executing slonik script %s" % script_on_disk.name)
15diff --git a/lib/lp/app/doc/menus.rst b/lib/lp/app/doc/menus.rst
16index 916f4ab..10f0bba 100644
17--- a/lib/lp/app/doc/menus.rst
18+++ b/lib/lp/app/doc/menus.rst
19@@ -1173,8 +1173,7 @@ NavigationMenus used in the previous TALES section.
20 ... </ul>
21 ... </div>"""
22 >>> template_file = tempfile.NamedTemporaryFile(mode="w")
23- >>> _ = template_file.write(menu_fragment)
24- >>> template_file.flush()
25+ >>> print(menu_fragment, file=template_file, flush=True)
26
27 >>> class FacetMenuView(LaunchpadView):
28 ... template = ViewPageTemplateFile(template_file.name)
29diff --git a/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py b/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py
30index 9c00c43..a5a1d0f 100644
31--- a/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py
32+++ b/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py
33@@ -88,8 +88,7 @@ def write_marker_file(path, contents, mode=None):
34 :param mode: If given, explicitly set the file to this permission mode.
35 """
36 with open(os.path.join(*path), "w") as marker:
37- marker.write(contents)
38- marker.flush()
39+ print(contents, end="", file=marker, flush=True)
40 if mode is not None:
41 os.fchmod(marker.fileno(), mode)
42
43diff --git a/lib/lp/codehosting/tests/test_rewrite.py b/lib/lp/codehosting/tests/test_rewrite.py
44index ff82882..0424f32 100644
45--- a/lib/lp/codehosting/tests/test_rewrite.py
46+++ b/lib/lp/codehosting/tests/test_rewrite.py
47@@ -321,8 +321,7 @@ class TestBranchRewriterScript(TestCaseWithFactory):
48 # For each complete line of input, the script should, without
49 # buffering, write a complete line of output.
50 for input_line in input_lines:
51- proc.stdin.write(input_line + "\n")
52- proc.stdin.flush()
53+ print(input_line, file=proc.stdin, flush=True)
54 output_lines.append(
55 nonblocking_readline(proc.stdout, 60).rstrip("\n")
56 )
57@@ -339,8 +338,7 @@ class TestBranchRewriterScript(TestCaseWithFactory):
58 "file:///var/tmp/bazaar.launchpad.test/mirrors/%s/.bzr/README"
59 % branch_id_to_path(new_branch.id)
60 )
61- proc.stdin.write(new_branch_input + "\n")
62- proc.stdin.flush()
63+ print(new_branch_input, file=proc.stdin, flush=True)
64 output_lines.append(nonblocking_readline(proc.stdout, 60).rstrip("\n"))
65
66 edited_branch_input = "/%s/.bzr/README" % edited_branch.unique_name
67@@ -348,8 +346,7 @@ class TestBranchRewriterScript(TestCaseWithFactory):
68 "file:///var/tmp/bazaar.launchpad.test/mirrors/%s/.bzr/README"
69 % branch_id_to_path(edited_branch.id)
70 )
71- proc.stdin.write(edited_branch_input + "\n")
72- proc.stdin.flush()
73+ print(edited_branch_input, file=proc.stdin, flush=True)
74 output_lines.append(nonblocking_readline(proc.stdout, 60).rstrip("\n"))
75
76 os.kill(proc.pid, signal.SIGINT)
77@@ -379,8 +376,7 @@ class TestBranchRewriterScriptHandlesDisconnects(TestCase):
78 self.addCleanup(self.rewriter_proc.terminate)
79
80 def request(self, query):
81- self.rewriter_proc.stdin.write(query + "\n")
82- self.rewriter_proc.stdin.flush()
83+ print(query, file=self.rewriter_proc.stdin, flush=True)
84
85 # 60 second timeout as we might need to wait for the script to
86 # finish starting up.
87diff --git a/lib/lp/services/pidfile.py b/lib/lp/services/pidfile.py
88index 3c9cdd2..6981f90 100644
89--- a/lib/lp/services/pidfile.py
90+++ b/lib/lp/services/pidfile.py
91@@ -45,8 +45,7 @@ def make_pidfile(service_name):
92
93 fd, tempname = tempfile.mkstemp(dir=os.path.dirname(pidfile))
94 outf = os.fdopen(fd, "w")
95- outf.write(str(os.getpid()) + "\n")
96- outf.flush()
97+ print(os.getpid(), file=outf, flush=True)
98 outf.close()
99 os.rename(tempname, pidfile)
100
101diff --git a/lib/lp/services/profile/mem.py b/lib/lp/services/profile/mem.py
102index 4bbbb75..128f9b8 100644
103--- a/lib/lp/services/profile/mem.py
104+++ b/lib/lp/services/profile/mem.py
105@@ -214,8 +214,7 @@ def logInThread(n=30):
106 def _logRefsEverySecond(log, n):
107 while True:
108 printCounts(mostRefs(n=n), file=log)
109- log.write("\n")
110- log.flush()
111+ print(file=log, flush=True)
112 time.sleep(1)
113
114
115diff --git a/lib/lp/services/scripts/tests/test_cronscript_enabled.py b/lib/lp/services/scripts/tests/test_cronscript_enabled.py
116index 9eb5f36..e958e92 100644
117--- a/lib/lp/services/scripts/tests/test_cronscript_enabled.py
118+++ b/lib/lp/services/scripts/tests/test_cronscript_enabled.py
119@@ -20,9 +20,8 @@ class TestCronscriptEnabled(TestCase):
120 self.log = BufferLogger()
121
122 def makeConfig(self, body):
123- tempfile = NamedTemporaryFile(suffix=".ini")
124- tempfile.write(body.encode("UTF-8"))
125- tempfile.flush()
126+ tempfile = NamedTemporaryFile(mode="w+", suffix=".ini")
127+ print(body, end="", file=tempfile, flush=True)
128 # Ensure a reference is kept until the test is over.
129 # tempfile will then clean itself up.
130 self.addCleanup(lambda x: None, tempfile)
131diff --git a/lib/lp/services/tests/test_command_spawner.py b/lib/lp/services/tests/test_command_spawner.py
132index be79b57..87d2904 100644
133--- a/lib/lp/services/tests/test_command_spawner.py
134+++ b/lib/lp/services/tests/test_command_spawner.py
135@@ -25,12 +25,6 @@ def make_pipe():
136 return fdopen(r, "r"), fdopen(w, "w")
137
138
139-def write_and_flush(pipe, text):
140- """Write `text` into `pipe`, and flush."""
141- pipe.write(text)
142- pipe.flush()
143-
144-
145 class FakeProcess:
146 """Fake `subprocess.Popen` result."""
147
148@@ -124,7 +118,7 @@ class TestCommandSpawner(TestCase):
149 spawner, process = self._makeSpawnerAndProcess()
150 stdout_handler = FakeMethod()
151 spawner.start("ls", stdout_handler=stdout_handler)
152- write_and_flush(process.stdout_sink, "readme.txt\n")
153+ print("readme.txt", file=process.stdout_sink, flush=True)
154 spawner.communicate()
155 self.assertEqual([("readme.txt\n",)], stdout_handler.extract_args())
156
157@@ -132,7 +126,7 @@ class TestCommandSpawner(TestCase):
158 spawner, process = self._makeSpawnerAndProcess()
159 stderr_handler = FakeMethod()
160 spawner.start("ls", stderr_handler=stderr_handler)
161- write_and_flush(process.stderr_sink, "File not found.\n")
162+ print("File not found.", file=process.stderr_sink, flush=True)
163 spawner.communicate()
164 self.assertEqual(
165 [("File not found.\n",)], stderr_handler.extract_args()
166@@ -180,7 +174,7 @@ class TestCommandSpawner(TestCase):
167 spawner.start(
168 "hello", stdout_handler=handler, completion_handler=handler
169 )
170- write_and_flush(process.stdout_sink, "Hello\n")
171+ print("Hello", file=process.stdout_sink, flush=True)
172 spawner.complete()
173 self.assertEqual([("Hello\n",), (0,)], handler.extract_args())
174
175diff --git a/scripts/branch-rewrite.py b/scripts/branch-rewrite.py
176index c752744..51753c9 100755
177--- a/scripts/branch-rewrite.py
178+++ b/scripts/branch-rewrite.py
179@@ -54,8 +54,7 @@ class BranchRewriteScript(LaunchpadScript):
180 transaction.abort()
181 # Mod-rewrite always gives us a newline terminated string.
182 if line:
183- print(rewriter.rewriteLine(line.strip()))
184- sys.stdout.flush()
185+ print(rewriter.rewriteLine(line.strip()), flush=True)
186 else:
187 # Standard input has been closed, so die.
188 return
189@@ -63,8 +62,7 @@ class BranchRewriteScript(LaunchpadScript):
190 sys.exit()
191 except Exception:
192 self.logger.exception("Exception occurred:")
193- print("NULL")
194- sys.stdout.flush()
195+ print("NULL", flush=True)
196 # The exception might have been a DisconnectionError or
197 # similar. Cleanup such as database reconnection will
198 # not happen until the transaction is rolled back.

Subscribers

People subscribed via source and target branches

to status/vote changes: