]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(copy): avoid sending copy data if an error is set
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 1 Sep 2025 16:06:43 +0000 (18:06 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 16 Sep 2025 00:13:26 +0000 (02:13 +0200)
It would result in a partial message. The server seems to do the right
thing anyway, but let's not waste its precious time.

psycopg/psycopg/_copy.py
psycopg/psycopg/_copy_async.py

index c2271638af35857d56ffa9a118cac91dbe964ddb..336ceb707d6f5cceaa103d7b571e539a49580ff2 100644 (file)
@@ -135,8 +135,9 @@ class Copy(BaseCopy["Connection[Any]"]):
         using the `Copy` object outside a block.
         """
         if self._direction == COPY_IN:
-            if data := self.formatter.end():
-                self._write(data)
+            if not exc:
+                if data := self.formatter.end():
+                    self._write(data)
             self.writer.finish(exc)
             self._finished = True
         else:
index 012ae143304171150369b03463138f8ff60d88b0..c009f116d4b521a8055aff85fc82df4cb92864f1 100644 (file)
@@ -132,8 +132,9 @@ class AsyncCopy(BaseCopy["AsyncConnection[Any]"]):
         using the `Copy` object outside a block.
         """
         if self._direction == COPY_IN:
-            if data := self.formatter.end():
-                await self._write(data)
+            if not exc:
+                if data := self.formatter.end():
+                    await self._write(data)
             await self.writer.finish(exc)
             self._finished = True
         else: