8000 Add try-finally block to ensure proper cursor closure · pangpang20/gaussdb-python@e99262b · GitHub
[go: up one dir, main page]

Skip to content

Commit e99262b

Browse files
committed
Add try-finally block to ensure proper cursor closure
1 parent eece3d4 commit e99262b

File tree

2 files changed

+89
-76
lines changed

2 files changed

+89
-76
lines changed

tests/test_copy.py

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -717,45 +717,51 @@ def test_copy_to_leaks(conn_cls, dsn, faker, fmt, set_types, method, gc):
717717

718718
def work():
719719
with conn_cls.connect(dsn) as conn:
720-
with conn.cursor(binary=(fmt == pq.Format.BINARY)) as cur:
721-
cur.execute(faker.drop_stmt)
722-
cur.execute(faker.create_stmt)
723-
conn.commit()
724-
with faker.find_insert_problem(conn):
725-
cur.executemany(faker.insert_stmt, faker.records)
726-
727-
stmt = sql.SQL(
728-
"copy (select {} from {} order by id) to stdout (format {})"
729-
).format(
730-
sql.SQL(", ").join(faker.fields_names),
731-
faker.table_name,
732-
sql.SQL(fmt.name),
733-
)
734-
735-
with cur.copy(stmt) as copy:
720+
try:
721+
with conn.cursor(binary=fmt) as cur:
736722
try:
737-
if set_types:
738-
copy.set_types(faker.types_names)
739-
740-
if method == "read":
741-
while True:
742-
tmp = copy.read()
743-
if not tmp:
744-
break
745-
elif method == "iter":
746-
list(copy)
747-
elif method == "row":
748-
while True:
749-
tmp = copy.read_row()
750-
if tmp is None:
751-
break
752-
elif method == "rows":
753-
list(copy.rows())
754-
except psycopg.OperationalError as e:
755-
if "no COPY in progress" in str(e):
756-
pytest.skip("COPY not started; skipping test iteration")
757-
else:
758-
raise
723+
cur.execute(faker.drop_stmt)
724+
cur.execute(faker.create_stmt)
725+
conn.commit()
726+
with faker.find_insert_problem(conn):
727+
cur.executemany(faker.insert_stmt, faker.records)
728+
729+
stmt = sql.SQL(
730+
"copy (select {} from {} order by id) to stdout (format {})"
731+
).format(
732+
sql.SQL(", ").join(faker.fields_names),
733+
faker.table_name,
734+
sql.SQL(fmt.name),
735+
)
736+
737+
with cur.copy(stmt) as copy:
738+
try:
739+
if set_types and fmt == pq.Format.BINARY:
740+
copy.set_types(faker.types_names)
741+
742+
if method == "read":
743+
while True:
744+
tmp = copy.read()
745+
if not tmp:
746+
break
747+
elif method == "iter":
748+
list(copy)
749+
elif method == "row":
750+
while True:
751+
tmp = copy.read_row()
752+
if tmp is None:
753+
break
754+
elif method == "rows":
755+
list(copy.rows())
756+
except psycopg.OperationalError as e:
757+
if "no COPY in progress" in str(e):
758+
pytest.skip("COPY not started; skipping test")
759+
else:
760+
raise
761+
finally:
762+
cur.close()
763+
finally:
764+
conn.close()
759765

760766
gc.collect()
761767
n = []

tests/test_copy_async.py

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -734,45 +734,52 @@ async def test_copy_to_leaks(aconn_cls, dsn, faker, fmt, set_types, method, gc):
734734

735735
async def work():
736736
async with await aconn_cls.connect(dsn) as conn:
737-
async with conn.cursor(binary=fmt) as cur:
738-
await cur.execute(faker.drop_stmt)
739-
await cur.execute(faker.create_stmt)
740-
await conn.commit()
741-
async with faker.find_insert_problem_async(conn):
742-
await cur.executemany(faker.insert_stmt, faker.records)
743-
744-
stmt = sql.SQL(
745-
"copy (select {} from {} order by id) to stdout (format {})"
746-
).format(
747-
sql.SQL(", ").join(faker.fields_names),
748-
faker.table_name,
749-
sql.SQL(fmt.name),
750-
)
751-
752-
async with cur.copy(stmt) as copy:
737+
try:
738+
async with conn.cursor(binary=fmt) as cur:
753739
try:
754-
if set_types:
755-
copy.set_types(faker.types_names)
756-
757-
if method == "read":
758-
while True:
759-
tmp = await copy.read()
760-
if not tmp:
761-
break
762-
elif method == "iter":
763-
await alist(copy)
764-
elif method == "row":
765-
while True:
766-
tmp = await copy.read_row()
767-
if tmp is None:
768-
break
769-
elif method == "rows":
770-
await alist(copy.rows())
771-
except psycopg.OperationalError as e:
772-
if "no COPY in progress" in str(e):
773-
pytest.skip("COPY not started; skipping test iteration")
774-
else:
775-
raise
740+
await cur.execute(faker.drop_stmt)
741+
await cur.execute(faker.create_stmt)
742+
await conn.commit()
743+
async with faker.find_insert_problem_async(conn):
744+
await cur.executemany(faker.insert_stmt, faker.records)
745+
746+
stmt = sql.SQL(
747+
"copy (select {} from {} order by id) to stdout (format {})"
748+
).format(
749+
sql.SQL(", ").join(faker.fields_names),
750+
faker.table_name,
751+
sql.SQL(fmt.name),
752+
)
753+
754+
async with cur.copy(stmt) as copy:
755+
try:
756+
if set_types and fmt == pq.Format.BINARY:
757+
copy.set_types(faker.types_names)
758+
759+
if method == "read":
760+
while True:
761+
tmp = await copy.read()
762+
if not tmp:
763+
break
764+
elif method == "iter":
765+
await alist(copy)
766+
elif method == "row":
767+
while True:
768+
tmp = await copy.read_row()
769+
if tmp is None:
770+
break
771+
elif method == "rows":
772+
await alist(copy.rows())
773+
except psycopg.OperationalError as e:
774+
if "no COPY in progress" in str(e):
775+
pytest.skip("COPY not started; skipping test")
776+
else:
777+
raise
778+
finally:
779+
await cur.close()
780+
781+
finally:
782+
await conn.close()
776783

777784
gc.collect()
778785
n = []

0 commit comments

Comments
 (0)
0