From: Daniele Varrazzo Date: Sun, 15 May 2022 15:54:14 +0000 (+0200) Subject: refactor: while 1 -> while True X-Git-Tag: 3.1~90 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0494b998a9381a8b3cc60d58d5ff2e16bfe8cef4;p=thirdparty%2Fpsycopg.git refactor: while 1 -> while True Brain legacy of ancient Python versions where the latter required a global lookup. --- diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index e8d06de72..bf0d71aff 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -898,7 +898,7 @@ class Connection(BaseConnection[Row]): """ Yield `Notify` objects as soon as they are received from the database. """ - while 1: + while True: with self.lock: ns = self.wait(notifies(self.pgconn)) enc = pgconn_encoding(self.pgconn) diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index 3411dc6de..5741597db 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -296,7 +296,7 @@ class AsyncConnection(BaseConnection[Row]): yield tx async def notifies(self) -> AsyncGenerator[Notify, None]: - while 1: + while True: async with self.lock: ns = await self.wait(notifies(self.pgconn)) enc = pgconn_encoding(self.pgconn) diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 557556037..3b4d01325 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -837,7 +837,7 @@ class Cursor(BaseCursor["Connection[Any]", Row]): def load(pos: int) -> Optional[Row]: return self._tx.load_row(pos, self._make_row) - while 1: + while True: row = load(self._pos) if row is None: break diff --git a/psycopg/psycopg/cursor_async.py b/psycopg/psycopg/cursor_async.py index 142845abc..7f7f9443b 100644 --- a/psycopg/psycopg/cursor_async.py +++ b/psycopg/psycopg/cursor_async.py @@ -179,7 +179,7 @@ class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]): def load(pos: int) -> Optional[Row]: return self._tx.load_row(pos, self._make_row) - while 1: + while True: row = load(self._pos) if row is None: break diff --git a/psycopg/psycopg/generators.py b/psycopg/psycopg/generators.py index 86b5a0bba..6c27e5cb8 100644 --- a/psycopg/psycopg/generators.py +++ b/psycopg/psycopg/generators.py @@ -36,7 +36,7 @@ def connect(conninfo: str) -> PQGenConn[PGconn]: """ conn = pq.PGconn.connect_start(conninfo.encode()) - while 1: + while True: if conn.status == ConnStatus.BAD: encoding = conninfo_encoding(conninfo) raise e.OperationalError( @@ -91,7 +91,7 @@ def send(pgconn: PGconn) -> PQGen[None]: After this generator has finished you may want to cycle using `fetch()` to retrieve the results available. """ - while 1: + while True: f = pgconn.flush() if f == 0: break @@ -114,7 +114,7 @@ def fetch_many(pgconn: PGconn) -> PQGen[List[PGresult]]: or error). """ results: List[PGresult] = [] - while 1: + while True: res = yield from fetch(pgconn) if not res: break @@ -219,7 +219,7 @@ def notifies(pgconn: PGconn) -> PQGen[List[pq.PGnotify]]: pgconn.consume_input() ns = [] - while 1: + while True: n = pgconn.notifies() if n: ns.append(n) @@ -230,7 +230,7 @@ def notifies(pgconn: PGconn) -> PQGen[List[pq.PGnotify]]: def copy_from(pgconn: PGconn) -> PQGen[Union[memoryview, PGresult]]: - while 1: + while True: nbytes, data = pgconn.get_copy_data(1) if nbytes != 0: break @@ -273,7 +273,7 @@ def copy_end(pgconn: PGconn, error: Optional[bytes]) -> PQGen[PGresult]: yield Wait.W # Repeat until it the message is flushed to the server - while 1: + while True: yield Wait.W f = pgconn.flush() if f == 0: diff --git a/psycopg/psycopg/types/array.py b/psycopg/psycopg/types/array.py index d8d7f8756..ef9429b15 100644 --- a/psycopg/psycopg/types/array.py +++ b/psycopg/psycopg/types/array.py @@ -381,7 +381,7 @@ class ArrayBinaryLoader(BaseArrayLoader): dims = [_unpack_dim(data, i)[0] for i in list(range(12, p, 8))] def consume(p: int) -> Iterator[Any]: - while 1: + while True: size = unpack_len(data, p)[0] p += 4 if size != -1: diff --git a/psycopg/psycopg/waiting.py b/psycopg/psycopg/waiting.py index 807de6343..25e828fc4 100644 --- a/psycopg/psycopg/waiting.py +++ b/psycopg/psycopg/waiting.py @@ -50,7 +50,7 @@ def wait_selector(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) try: s = next(gen) with DefaultSelector() as sel: - while 1: + while True: sel.register(fileno, s) rlist = None while not rlist: @@ -85,7 +85,7 @@ def wait_conn(gen: PQGenConn[RV], timeout: Optional[float] = None) -> RV: if not timeout: timeout = None with DefaultSelector() as sel: - while 1: + while True: sel.register(fileno, s) rlist = sel.select(timeout=timeout) sel.unregister(fileno) @@ -124,7 +124,7 @@ async def wait_async(gen: PQGen[RV], fileno: int) -> RV: try: s = next(gen) - while 1: + while True: reader = s & Wait.R writer = s & Wait.W if not reader and not writer: @@ -178,7 +178,7 @@ async def wait_conn_async(gen: PQGenConn[RV], timeout: Optional[float] = None) - fileno, s = next(gen) if not timeout: timeout = None - while 1: + while True: reader = s & Wait.R writer = s & Wait.W if not reader and not writer: @@ -226,7 +226,7 @@ def wait_epoll(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) -> with select.epoll() as epoll: evmask = poll_evmasks[s] epoll.register(fileno, evmask) - while 1: + while True: fileevs = None while not fileevs: fileevs = epoll.poll(timeout) diff --git a/psycopg_c/psycopg_c/_psycopg/generators.pyx b/psycopg_c/psycopg_c/_psycopg/generators.pyx index 4afb53508..85c3a0b00 100644 --- a/psycopg_c/psycopg_c/_psycopg/generators.pyx +++ b/psycopg_c/psycopg_c/_psycopg/generators.pyx @@ -31,7 +31,7 @@ def connect(conninfo: str) -> PQGenConn[abc.PGconn]: cdef int conn_status = libpq.PQstatus(pgconn_ptr) cdef int poll_status - while 1: + while True: if conn_status == libpq.CONNECTION_BAD: encoding = conninfo_encoding(conninfo) raise e.OperationalError( @@ -92,7 +92,7 @@ def send(pq.PGconn pgconn) -> PQGen[None]: cdef int status cdef int cires - while 1: + while True: if libpq.PQflush(pgconn_ptr) == 0: break @@ -122,7 +122,7 @@ def fetch_many(pq.PGconn pgconn) -> PQGen[List[PGresult]]: cdef pq.PGresult result cdef libpq.PGresult *pgres - while 1: + while True: result = yield from fetch(pgconn) if result is None: break @@ -161,7 +161,7 @@ def fetch(pq.PGconn pgconn) -> PQGen[Optional[PGresult]]: if libpq.PQisBusy(pgconn_ptr): yield WAIT_R - while 1: + while True: with nogil: cires = libpq.PQconsumeInput(pgconn_ptr) if cires == 1: @@ -176,7 +176,7 @@ def fetch(pq.PGconn pgconn) -> PQGen[Optional[PGresult]]: # Consume notifies if notify_handler is not None: - while 1: + while True: pynotify = pgconn.notifies() if pynotify is None: break @@ -184,7 +184,7 @@ def fetch(pq.PGconn pgconn) -> PQGen[Optional[PGresult]]: notify_handler, pynotify, NULL ) else: - while 1: + while True: notify = libpq.PQnotifies(pgconn_ptr) if notify is NULL: break diff --git a/psycopg_c/psycopg_c/pq/conninfo.pyx b/psycopg_c/psycopg_c/pq/conninfo.pyx index b0d2cf5ad..3443de1ba 100644 --- a/psycopg_c/psycopg_c/pq/conninfo.pyx +++ b/psycopg_c/psycopg_c/pq/conninfo.pyx @@ -41,7 +41,7 @@ cdef _options_from_array(libpq.PQconninfoOption *opts): rv = [] cdef int i = 0 cdef libpq.PQconninfoOption* opt - while 1: + while True: opt = opts + i if opt.keyword is NULL: break diff --git a/tests/fix_faker.py b/tests/fix_faker.py index 86c3c0ee8..d1603219c 100644 --- a/tests/fix_faker.py +++ b/tests/fix_faker.py @@ -488,7 +488,7 @@ class Faker: # don't make empty lists because they regularly fail cast length = randrange(1, self.list_max_length) spec = spec[1] - while 1: + while True: rv = [self.make(spec) for i in range(length)] # TODO multirange lists fail binary dump if the last element is diff --git a/tests/pq/test_async.py b/tests/pq/test_async.py index 0d0e4aa72..fee58f075 100644 --- a/tests/pq/test_async.py +++ b/tests/pq/test_async.py @@ -20,7 +20,7 @@ def test_send_query(pgconn): # send loop waited_on_send = 0 - while 1: + while True: f = pgconn.flush() if f == 0: break @@ -40,7 +40,7 @@ def test_send_query(pgconn): # read loop results = [] - while 1: + while True: pgconn.consume_input() if pgconn.is_busy(): select([pgconn.socket], [], []) diff --git a/tests/pq/test_pgconn.py b/tests/pq/test_pgconn.py index dfd0a6a80..4545ed59a 100644 --- a/tests/pq/test_pgconn.py +++ b/tests/pq/test_pgconn.py @@ -32,7 +32,7 @@ def test_connectdb_badtype(baddsn): def test_connect_async(dsn): conn = pq.PGconn.connect_start(dsn.encode()) conn.nonblocking = 1 - while 1: + while True: assert conn.status != pq.ConnStatus.BAD rv = conn.connect_poll() if rv == pq.PollingStatus.OK: @@ -56,7 +56,7 @@ def test_connect_async_bad(dsn): parsed_dsn[b"dbname"] = b"psycopg_test_not_for_real" dsn = b" ".join(b"%s='%s'" % item for item in parsed_dsn.items()) conn = pq.PGconn.connect_start(dsn) - while 1: + while True: assert conn.status != pq.ConnStatus.BAD, conn.error_message rv = conn.connect_poll() if rv == pq.PollingStatus.FAILED: @@ -149,7 +149,7 @@ def test_reset_async(pgconn): pgconn.exec_(b"select pg_terminate_backend(pg_backend_pid())") assert pgconn.status == pq.ConnStatus.BAD pgconn.reset_start() - while 1: + while True: rv = pgconn.reset_poll() if rv == pq.PollingStatus.READING: select([pgconn.socket], [], []) diff --git a/tests/test_client_cursor.py b/tests/test_client_cursor.py index 6c94b5eef..dca40221d 100644 --- a/tests/test_client_cursor.py +++ b/tests/test_client_cursor.py @@ -764,12 +764,12 @@ def test_leak(dsn, faker, fetch, row_factory): cur.execute(faker.select_stmt) if fetch == "one": - while 1: + while True: tmp = cur.fetchone() if tmp is None: break elif fetch == "many": - while 1: + while True: tmp = cur.fetchmany(3) if not tmp: break diff --git a/tests/test_client_cursor_async.py b/tests/test_client_cursor_async.py index 80f14e615..a4730886f 100644 --- a/tests/test_client_cursor_async.py +++ b/tests/test_client_cursor_async.py @@ -637,12 +637,12 @@ async def test_leak(dsn, faker, fetch, row_factory): await cur.execute(faker.select_stmt) if fetch == "one": - while 1: + while True: tmp = await cur.fetchone() if tmp is None: break elif fetch == "many": - while 1: + while True: tmp = await cur.fetchmany(3) if not tmp: break diff --git a/tests/test_copy.py b/tests/test_copy.py index 15187f102..bda6f1109 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -152,7 +152,7 @@ def test_copy_out_allchars(conn, format): ) with cur.copy(query) as copy: copy.set_types(["text"]) - while 1: + while True: row = copy.read_row() if not row: break @@ -167,7 +167,7 @@ def test_read_row_notypes(conn, format): cur = conn.cursor() with cur.copy(f"copy ({sample_values}) to stdout (format {format.name})") as copy: rows = [] - while 1: + while True: row = copy.read_row() if not row: break @@ -648,14 +648,14 @@ def test_copy_to_leaks(dsn, faker, fmt, set_types, method): copy.set_types(faker.types_names) if method == "read": - while 1: + while True: tmp = copy.read() if not tmp: break elif method == "iter": list(copy) elif method == "row": - while 1: + while True: tmp = copy.read_row() # type: ignore[assignment] if tmp is None: break @@ -807,7 +807,7 @@ class DataGenerator: def sha(self, f): m = hashlib.sha256() - while 1: + while True: block = f.read() if not block: break diff --git a/tests/test_copy_async.py b/tests/test_copy_async.py index c5df84f03..b759b0554 100644 --- a/tests/test_copy_async.py +++ b/tests/test_copy_async.py @@ -134,7 +134,7 @@ async def test_copy_out_allchars(aconn, format): ) async with cur.copy(query) as copy: copy.set_types(["text"]) - while 1: + while True: row = await copy.read_row() if not row: break @@ -151,7 +151,7 @@ async def test_read_row_notypes(aconn, format): f"copy ({sample_values}) to stdout (format {format.name})" ) as copy: rows = [] - while 1: + while True: row = await copy.read_row() if not row: break @@ -648,14 +648,14 @@ async def test_copy_to_leaks(dsn, faker, fmt, set_types, method): copy.set_types(faker.types_names) if method == "read": - while 1: + while True: tmp = await copy.read() if not tmp: break elif method == "iter": await alist(copy) elif method == "row": - while 1: + while True: tmp = await copy.read_row() # type: ignore[assignment] if tmp is None: break @@ -796,7 +796,7 @@ class DataGenerator: def sha(self, f): m = hashlib.sha256() - while 1: + while True: block = f.read() if not block: break diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 3c7e4e443..534f6066e 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -814,12 +814,12 @@ def test_leak(dsn, faker, fmt, fmt_out, fetch, row_factory): cur.execute(faker.select_stmt) if fetch == "one": - while 1: + while True: tmp = cur.fetchone() if tmp is None: break elif fetch == "many": - while 1: + while True: tmp = cur.fetchmany(3) if not tmp: break diff --git a/tests/test_cursor_async.py b/tests/test_cursor_async.py index 39d8e8327..3af0e5775 100644 --- a/tests/test_cursor_async.py +++ b/tests/test_cursor_async.py @@ -684,12 +684,12 @@ async def test_leak(dsn, faker, fmt, fmt_out, fetch, row_factory): await cur.execute(faker.select_stmt) if fetch == "one": - while 1: + while True: tmp = await cur.fetchone() if tmp is None: break elif fetch == "many": - while 1: + while True: tmp = await cur.fetchmany(3) if not tmp: break diff --git a/tests/test_server_cursor.py b/tests/test_server_cursor.py index e1a9da446..fd47b684d 100644 --- a/tests/test_server_cursor.py +++ b/tests/test_server_cursor.py @@ -339,7 +339,7 @@ def test_row_factory(conn): cur.execute("select generate_series(1, 3) as x") recs = cur.fetchall() cur.scroll(0, "absolute") - while 1: + while True: rec = cur.fetchone() if not rec: break diff --git a/tests/test_server_cursor_async.py b/tests/test_server_cursor_async.py index 8ccf68651..396e48149 100644 --- a/tests/test_server_cursor_async.py +++ b/tests/test_server_cursor_async.py @@ -349,7 +349,7 @@ async def test_row_factory(aconn): await cur.execute("select generate_series(1, 3) as x") recs = await cur.fetchall() await cur.scroll(0, "absolute") - while 1: + while True: rec = await cur.fetchone() if not rec: break