Skip to content

Commit c235ab3

Browse files
author
James William Pye
committed
Get rid of the NoReceivers exception.
CopyFail should be good enough.
1 parent 83d39d1 commit c235ab3

2 files changed

Lines changed: 18 additions & 20 deletions

File tree

postgresql/copyman.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from .protocol.element3 import CopyData, CopyDone, Complete, cat_messages
1717
from .protocol.xact3 import Complete as xactComplete
1818

19-
#: 100KB buffer for COPY messages by default.
20-
default_buffer_size = 1024 * 100
19+
#: 10KB buffer for COPY messages by default.
20+
default_buffer_size = 1024 * 10
2121

2222
class Fault(Exception):
2323
"""
@@ -33,28 +33,26 @@ def __init__(self, manager, faults):
3333
self.faults = faults
3434

3535
def __str__(self):
36-
return "{0} faults occurred".format(len(self.receivers))
36+
return "{0} faults occurred".format(len(self.faults))
3737

3838
class CopyFail(Exception):
3939
"""
40-
Exception thrown by the CopyManager when the COPY failed.
40+
Exception thrown by the CopyManager when the COPY operation failed.
41+
42+
The 'manager' attribute the CopyManager that raised the CopyFail.
43+
44+
The 'reason' attribute is a string indicating why it failed.
45+
46+
The 'faults' attribute is a mapping of receivers to exceptions that were
47+
raised on exit.
4148
"""
42-
def __init__(self, manager, reason, faults = None):
49+
def __init__(self, manager, reason = None, faults = None):
4350
self.manager = manager
4451
self.reason = reason
4552
self.faults = faults or {}
4653

4754
def __str__(self):
48-
return self.reason
49-
50-
class NoReceivers(CopyFail):
51-
"""
52-
Exception thrown by the CopyManager when the COPY failed due to all the
53-
receivers faulting out.
54-
"""
55-
reason = 'no receivers remained after fault'
56-
def __init__(self, manager):
57-
self.manager = manager
55+
return self.reason or 'copy '
5856

5957
# The identifier for PQv3 copy data.
6058
PROTOCOL_PQv3 = "PQv3"
@@ -708,9 +706,9 @@ def __exit__(self, typ, val, tb):
708706

709707
# No receivers? It wasn't a success.
710708
if not self.receivers:
711-
if typ is NoReceivers:
709+
if typ is CopyFail:
712710
raise
713-
raise NoReceivers(self)
711+
raise CopyFail(self, "no receivers")
714712

715713
exit_faults = {}
716714
for x in self.receivers:
@@ -743,7 +741,7 @@ def _service_producer(self):
743741
# Setup current data.
744742
if not self.receivers:
745743
# No receivers to take the data.
746-
raise NoReceivers(self)
744+
raise CopyFail(self, "no receivers")
747745

748746
try:
749747
nextdata = next(self.producer)

postgresql/test/test_copyman.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ def testNoReceivers(self):
399399
except copyman.Fault as cf:
400400
self.failUnless(sr1 in cf.faults)
401401
# Don't reconcile.
402-
except copyman.NoReceivers:
402+
except copyman.CopyFail:
403+
self.failUnless(not bool(copy.receivers))
403404
# Success.
404-
pass
405405
else:
406406
self.fail("did not raise expected error")
407407
# Let the exception cause a failure.

0 commit comments

Comments
 (0)