launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #29337
[Merge] ~cjwatson/launchpad:print-flush into launchpad:master
Colin Watson has proposed merging ~cjwatson/launchpad:print-flush into launchpad:master.
Commit message:
Use print(flush=True) where appropriate
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/431818
This was added in Python 3.3, and lets us make a few things a bit more concise and (IMO) clearer.
--
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:print-flush into launchpad:master.
diff --git a/database/replication/helpers.py b/database/replication/helpers.py
index f1f8349..5ab5b50 100644
--- a/database/replication/helpers.py
+++ b/database/replication/helpers.py
@@ -227,8 +227,7 @@ def execute_slonik(script, sync=None, exit_on_fail=True, auto_preamble=True):
# to slonik via stdin. This way it can be examined if slonik appears
# to hang.
script_on_disk = NamedTemporaryFile(prefix="slonik", suffix=".sk")
- print(script, file=script_on_disk)
- script_on_disk.flush()
+ print(script, file=script_on_disk, flush=True)
# Run slonik
log.debug("Executing slonik script %s" % script_on_disk.name)
diff --git a/lib/lp/app/doc/menus.rst b/lib/lp/app/doc/menus.rst
index 916f4ab..10f0bba 100644
--- a/lib/lp/app/doc/menus.rst
+++ b/lib/lp/app/doc/menus.rst
@@ -1173,8 +1173,7 @@ NavigationMenus used in the previous TALES section.
... </ul>
... </div>"""
>>> template_file = tempfile.NamedTemporaryFile(mode="w")
- >>> _ = template_file.write(menu_fragment)
- >>> template_file.flush()
+ >>> print(menu_fragment, file=template_file, flush=True)
>>> class FacetMenuView(LaunchpadView):
... template = ViewPageTemplateFile(template_file.name)
diff --git a/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py b/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py
index 9c00c43..a5a1d0f 100644
--- a/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py
+++ b/lib/lp/archivepublisher/tests/test_publish_ftpmaster.py
@@ -88,8 +88,7 @@ def write_marker_file(path, contents, mode=None):
:param mode: If given, explicitly set the file to this permission mode.
"""
with open(os.path.join(*path), "w") as marker:
- marker.write(contents)
- marker.flush()
+ print(contents, end="", file=marker, flush=True)
if mode is not None:
os.fchmod(marker.fileno(), mode)
diff --git a/lib/lp/codehosting/tests/test_rewrite.py b/lib/lp/codehosting/tests/test_rewrite.py
index ff82882..0424f32 100644
--- a/lib/lp/codehosting/tests/test_rewrite.py
+++ b/lib/lp/codehosting/tests/test_rewrite.py
@@ -321,8 +321,7 @@ class TestBranchRewriterScript(TestCaseWithFactory):
# For each complete line of input, the script should, without
# buffering, write a complete line of output.
for input_line in input_lines:
- proc.stdin.write(input_line + "\n")
- proc.stdin.flush()
+ print(input_line, file=proc.stdin, flush=True)
output_lines.append(
nonblocking_readline(proc.stdout, 60).rstrip("\n")
)
@@ -339,8 +338,7 @@ class TestBranchRewriterScript(TestCaseWithFactory):
"file:///var/tmp/bazaar.launchpad.test/mirrors/%s/.bzr/README"
% branch_id_to_path(new_branch.id)
)
- proc.stdin.write(new_branch_input + "\n")
- proc.stdin.flush()
+ print(new_branch_input, file=proc.stdin, flush=True)
output_lines.append(nonblocking_readline(proc.stdout, 60).rstrip("\n"))
edited_branch_input = "/%s/.bzr/README" % edited_branch.unique_name
@@ -348,8 +346,7 @@ class TestBranchRewriterScript(TestCaseWithFactory):
"file:///var/tmp/bazaar.launchpad.test/mirrors/%s/.bzr/README"
% branch_id_to_path(edited_branch.id)
)
- proc.stdin.write(edited_branch_input + "\n")
- proc.stdin.flush()
+ print(edited_branch_input, file=proc.stdin, flush=True)
output_lines.append(nonblocking_readline(proc.stdout, 60).rstrip("\n"))
os.kill(proc.pid, signal.SIGINT)
@@ -379,8 +376,7 @@ class TestBranchRewriterScriptHandlesDisconnects(TestCase):
self.addCleanup(self.rewriter_proc.terminate)
def request(self, query):
- self.rewriter_proc.stdin.write(query + "\n")
- self.rewriter_proc.stdin.flush()
+ print(query, file=self.rewriter_proc.stdin, flush=True)
# 60 second timeout as we might need to wait for the script to
# finish starting up.
diff --git a/lib/lp/services/pidfile.py b/lib/lp/services/pidfile.py
index 3c9cdd2..6981f90 100644
--- a/lib/lp/services/pidfile.py
+++ b/lib/lp/services/pidfile.py
@@ -45,8 +45,7 @@ def make_pidfile(service_name):
fd, tempname = tempfile.mkstemp(dir=os.path.dirname(pidfile))
outf = os.fdopen(fd, "w")
- outf.write(str(os.getpid()) + "\n")
- outf.flush()
+ print(os.getpid(), file=outf, flush=True)
outf.close()
os.rename(tempname, pidfile)
diff --git a/lib/lp/services/profile/mem.py b/lib/lp/services/profile/mem.py
index 4bbbb75..128f9b8 100644
--- a/lib/lp/services/profile/mem.py
+++ b/lib/lp/services/profile/mem.py
@@ -214,8 +214,7 @@ def logInThread(n=30):
def _logRefsEverySecond(log, n):
while True:
printCounts(mostRefs(n=n), file=log)
- log.write("\n")
- log.flush()
+ print(file=log, flush=True)
time.sleep(1)
diff --git a/lib/lp/services/scripts/tests/test_cronscript_enabled.py b/lib/lp/services/scripts/tests/test_cronscript_enabled.py
index 9eb5f36..e958e92 100644
--- a/lib/lp/services/scripts/tests/test_cronscript_enabled.py
+++ b/lib/lp/services/scripts/tests/test_cronscript_enabled.py
@@ -20,9 +20,8 @@ class TestCronscriptEnabled(TestCase):
self.log = BufferLogger()
def makeConfig(self, body):
- tempfile = NamedTemporaryFile(suffix=".ini")
- tempfile.write(body.encode("UTF-8"))
- tempfile.flush()
+ tempfile = NamedTemporaryFile(mode="w+", suffix=".ini")
+ print(body, end="", file=tempfile, flush=True)
# Ensure a reference is kept until the test is over.
# tempfile will then clean itself up.
self.addCleanup(lambda x: None, tempfile)
diff --git a/lib/lp/services/tests/test_command_spawner.py b/lib/lp/services/tests/test_command_spawner.py
index be79b57..87d2904 100644
--- a/lib/lp/services/tests/test_command_spawner.py
+++ b/lib/lp/services/tests/test_command_spawner.py
@@ -25,12 +25,6 @@ def make_pipe():
return fdopen(r, "r"), fdopen(w, "w")
-def write_and_flush(pipe, text):
- """Write `text` into `pipe`, and flush."""
- pipe.write(text)
- pipe.flush()
-
-
class FakeProcess:
"""Fake `subprocess.Popen` result."""
@@ -124,7 +118,7 @@ class TestCommandSpawner(TestCase):
spawner, process = self._makeSpawnerAndProcess()
stdout_handler = FakeMethod()
spawner.start("ls", stdout_handler=stdout_handler)
- write_and_flush(process.stdout_sink, "readme.txt\n")
+ print("readme.txt", file=process.stdout_sink, flush=True)
spawner.communicate()
self.assertEqual([("readme.txt\n",)], stdout_handler.extract_args())
@@ -132,7 +126,7 @@ class TestCommandSpawner(TestCase):
spawner, process = self._makeSpawnerAndProcess()
stderr_handler = FakeMethod()
spawner.start("ls", stderr_handler=stderr_handler)
- write_and_flush(process.stderr_sink, "File not found.\n")
+ print("File not found.", file=process.stderr_sink, flush=True)
spawner.communicate()
self.assertEqual(
[("File not found.\n",)], stderr_handler.extract_args()
@@ -180,7 +174,7 @@ class TestCommandSpawner(TestCase):
spawner.start(
"hello", stdout_handler=handler, completion_handler=handler
)
- write_and_flush(process.stdout_sink, "Hello\n")
+ print("Hello", file=process.stdout_sink, flush=True)
spawner.complete()
self.assertEqual([("Hello\n",), (0,)], handler.extract_args())
diff --git a/scripts/branch-rewrite.py b/scripts/branch-rewrite.py
index c752744..51753c9 100755
--- a/scripts/branch-rewrite.py
+++ b/scripts/branch-rewrite.py
@@ -54,8 +54,7 @@ class BranchRewriteScript(LaunchpadScript):
transaction.abort()
# Mod-rewrite always gives us a newline terminated string.
if line:
- print(rewriter.rewriteLine(line.strip()))
- sys.stdout.flush()
+ print(rewriter.rewriteLine(line.strip()), flush=True)
else:
# Standard input has been closed, so die.
return
@@ -63,8 +62,7 @@ class BranchRewriteScript(LaunchpadScript):
sys.exit()
except Exception:
self.logger.exception("Exception occurred:")
- print("NULL")
- sys.stdout.flush()
+ print("NULL", flush=True)
# The exception might have been a DisconnectionError or
# similar. Cleanup such as database reconnection will
# not happen until the transaction is rolled back.