self._finished = True
def __enter__(self) -> "Copy":
- assert not self._finished
+ if self._finished:
+ raise TypeError("copy blocks can be used only once")
return self
def __exit__(
self._finished = True
async def __aenter__(self) -> "AsyncCopy":
- assert not self._finished
+ if self._finished:
+ raise TypeError("copy blocks can be used only once")
return self
async def __aexit__(
def _enter_commands(self) -> List[str]:
if not self._yolo:
- raise TypeError("transaction blocks cannot be use more than once")
+ raise TypeError("transaction blocks can be used only once")
else:
self._yolo = False
list(copy)
+def test_cant_reenter(conn):
+ cur = conn.cursor()
+ with cur.copy("copy (select 1) to stdout") as copy:
+ list(copy)
+
+ with pytest.raises(TypeError):
+ with copy:
+ list(copy)
+
+
def ensure_table(cur, tabledef, name="copy_in"):
cur.execute(f"drop table if exists {name}")
cur.execute(f"create table {name} ({tabledef})")
pass
+async def test_cant_reenter(aconn):
+ cur = await aconn.cursor()
+ async with cur.copy("copy (select 1) to stdout") as copy:
+ async for record in copy:
+ pass
+
+ with pytest.raises(TypeError):
+ async with copy:
+ async for record in copy:
+ pass
+
+
async def ensure_table(cur, tabledef, name="copy_in"):
await cur.execute(f"drop table if exists {name}")
await cur.execute(f"create table {name} ({tabledef})")