Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

python: Simplify stdio flushing using print(…, flush=True).

Anders Kaseorg342834ee9c7c
diff --git a/scripts/lib/zulip_tools.py b/scripts/lib/zulip_tools.py
index 350e73bfe5b..c2fb5c1a903 100755
--- a/scripts/lib/zulip_tools.py
+++ b/scripts/lib/zulip_tools.py
@@ -198,9 +198,9 @@ def get_deployment_lock(error_rerun_script: str) -> None:
WARNING
+ "Another deployment in progress; waiting for lock... "
+ "(If no deployment is running, rmdir {})".format(LOCK_DIR)
- + ENDC
+ + ENDC,
+ flush=True,
)
- sys.stdout.flush()
time.sleep(3)
if not got_lock:
diff --git a/tools/lib/test_server.py b/tools/lib/test_server.py
index 3cd37776f10..d4470b3686a 100644
--- a/tools/lib/test_server.py
+++ b/tools/lib/test_server.py
@@ -78,18 +78,17 @@ def test_server_running(
try:
# Wait for the server to start up.
- sys.stdout.write("\nWaiting for test server (may take a while)")
+ print(end="\nWaiting for test server (may take a while)")
if not dots:
- sys.stdout.write("\n\n")
+ print("\n", flush=True)
t = time.time()
while not server_is_up(server, log_file):
if dots:
- sys.stdout.write(".")
- sys.stdout.flush()
+ print(end=".", flush=True)
time.sleep(0.4)
if time.time() - t > MAX_SERVER_WAIT:
raise Exception("Timeout waiting for server")
- sys.stdout.write("\n\n--- SERVER IS UP! ---\n\n")
+ print("\n\n--- SERVER IS UP! ---\n", flush=True)
# DO OUR ACTUAL TESTING HERE!!!
yield
diff --git a/zerver/management/commands/export.py b/zerver/management/commands/export.py
index cbcaf40f016..e065fae1a78 100644
--- a/zerver/management/commands/export.py
+++ b/zerver/management/commands/export.py
@@ -1,5 +1,4 @@
import os
-import sys
import tempfile
from argparse import ArgumentParser
from typing import Any
@@ -207,8 +206,7 @@ class Command(ZulipBaseCommand):
do_deactivate_realm(realm, acting_user=None)
def percent_callback(bytes_transferred: Any) -> None:
- sys.stdout.write(".")
- sys.stdout.flush()
+ print(end=".", flush=True)
# Allows us to trigger exports separately from command line argument parsing
export_realm_wrapper(
diff --git a/zerver/migrations/0177_user_message_add_and_index_is_private_flag.py b/zerver/migrations/0177_user_message_add_and_index_is_private_flag.py
index 2443ebd2a5b..40c68783fa9 100644
--- a/zerver/migrations/0177_user_message_add_and_index_is_private_flag.py
+++ b/zerver/migrations/0177_user_message_add_and_index_is_private_flag.py
@@ -1,7 +1,5 @@
# Generated by Django 1.11.13 on 2018-06-14 13:39
-import sys
-
import bitfield.models
from django.db import migrations
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
@@ -19,8 +17,7 @@ def reset_is_private_flag(apps: StateApps, schema_editor: DatabaseSchemaEditor)
i = 0
total = len(user_profile_ids)
- print("Setting default values for the new flag...")
- sys.stdout.flush()
+ print("Setting default values for the new flag...", flush=True)
for user_id in user_profile_ids:
while True:
# Ideally, we'd just do a single database query per user.
@@ -45,8 +42,7 @@ def reset_is_private_flag(apps: StateApps, schema_editor: DatabaseSchemaEditor)
i += 1
if i % 50 == 0 or i == total:
percent = round((i / total) * 100, 2)
- print(f"Processed {i}/{total} {percent}%")
- sys.stdout.flush()
+ print(f"Processed {i}/{total} {percent}%", flush=True)
class Migration(migrations.Migration):
diff --git a/zerver/migrations/0182_set_initial_value_is_private_flag.py b/zerver/migrations/0182_set_initial_value_is_private_flag.py
index 2e0c3d1788b..c3998f56b24 100644
--- a/zerver/migrations/0182_set_initial_value_is_private_flag.py
+++ b/zerver/migrations/0182_set_initial_value_is_private_flag.py
@@ -1,5 +1,3 @@
-import sys
-
from django.db import migrations
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
@@ -19,8 +17,7 @@ def set_initial_value_of_is_private_flag(
total = Message.objects.filter(recipient__type__in=[1, 3]).count()
processed = 0
- print("\nStart setting initial value for is_private flag...")
- sys.stdout.flush()
+ print("\nStart setting initial value for is_private flag...", flush=True)
while True:
range_end = i + 10000
# Can't use [Recipient.PERSONAL, Recipient.HUDDLE] in migration files
@@ -41,8 +38,7 @@ def set_initial_value_of_is_private_flag(
percent = round((processed / total) * 100, 2)
else:
percent = 100.00
- print(f"Processed {processed}/{total} {percent}%")
- sys.stdout.flush()
+ print(f"Processed {processed}/{total} {percent}%", flush=True)
class Migration(migrations.Migration):
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index f3dc48535e4..8b71884956c 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -5,7 +5,6 @@
# and zerver/lib/data_types.py systems for validating the schemas of
# events; it also uses the OpenAPI tools to validate our documentation.
import copy
-import sys
import time
from io import StringIO
from typing import Any, Callable, Dict, List, Optional, Set
@@ -389,10 +388,10 @@ class BaseAction(ZulipTestCase):
we apply events after fetching data. If you
do not know how to debug it, you can ask for
help on chat.
- """
+ """,
+ flush=True,
)
- sys.stdout.flush()
raise AssertionError("Mismatching states")