@@ -92,8 +92,7 @@ def fill(self):
92
92
ready_to_read , _ , _ = select ((self .socket ,), (), (), 0 )
93
93
received = self .socket .recv (65539 )
94
94
if received :
95
- if __debug__ :
96
- log_debug ("S: b%r" , received )
95
+ log_debug ("S: b%r" , received )
97
96
self .buffer [len (self .buffer ):] = received
98
97
else :
99
98
if ready_to_read is not None :
@@ -174,8 +173,7 @@ def send(self):
174
173
""" Send all queued messages to the server.
175
174
"""
176
175
data = self .raw .getvalue ()
177
- if __debug__ :
178
- log_debug ("C: b%r" , data )
176
+ log_debug ("C: b%r" , data )
179
177
self .socket .sendall (data )
180
178
181
179
self .raw .seek (self .raw .truncate (0 ))
@@ -264,8 +262,7 @@ def append(self, signature, fields=(), response=None):
264
262
:arg fields: the fields of the message as a tuple
265
263
:arg response: a response object to handle callbacks
266
264
"""
267
- if __debug__ :
268
- log_info ("C: %s %s" , message_names [signature ], " " .join (map (repr , fields )))
265
+ log_info ("C: %s %r" , message_names [signature ], fields )
269
266
270
267
self .packer .pack_struct_header (len (fields ), signature )
271
268
for field in fields :
@@ -329,29 +326,36 @@ def fetch(self):
329
326
self .defunct = True
330
327
self .close ()
331
328
raise
332
- # Unpack from the raw byte stream and call the relevant message handler(s)
333
- self .unpacker .load (message_data )
334
- size , signature = self .unpacker .unpack_structure_header ()
335
- fields = [self .unpacker .unpack () for _ in range (size )]
336
329
337
- if __debug__ :
338
- log_info ("S: %s %r" , message_names [signature ], fields )
330
+ unpacker = self .unpacker
331
+ unpacker .load (message_data )
332
+ size , signature = unpacker .unpack_structure_header ()
333
+ if size > 1 :
334
+ raise ProtocolError ("Expected one field" )
339
335
340
336
if signature == SUCCESS :
337
+ metadata = unpacker .unpack_map ()
338
+ log_info ("S: SUCCESS (%r)" , metadata )
341
339
response = self .responses .popleft ()
342
340
response .complete = True
343
- response .on_success (* fields )
341
+ response .on_success (metadata or {} )
344
342
elif signature == RECORD :
343
+ data = unpacker .unpack_list ()
344
+ log_info ("S: RECORD (%r)" , data )
345
345
response = self .responses [0 ]
346
- response .on_record (* fields )
346
+ response .on_record (data or [] )
347
347
elif signature == IGNORED :
348
+ metadata = unpacker .unpack_map ()
349
+ log_info ("S: IGNORED (%r)" , metadata )
348
350
response = self .responses .popleft ()
349
351
response .complete = True
350
- response .on_ignored (* fields )
352
+ response .on_ignored (metadata or {} )
351
353
elif signature == FAILURE :
354
+ metadata = unpacker .unpack_map ()
355
+ log_info ("S: FAILURE (%r)" , metadata )
352
356
response = self .responses .popleft ()
353
357
response .complete = True
354
- response .on_failure (* fields )
358
+ response .on_failure (metadata or {} )
355
359
else :
356
360
raise ProtocolError ("Unexpected response message with signature %02X" % signature )
357
361
@@ -365,8 +369,7 @@ def close(self):
365
369
""" Close the connection.
366
370
"""
367
371
if not self .closed :
368
- if __debug__ :
369
- log_info ("~~ [CLOSE]" )
372
+ log_info ("~~ [CLOSE]" )
370
373
self .channel .socket .close ()
371
374
self .closed = True
372
375
@@ -476,7 +479,7 @@ def connect(address, ssl_context=None, **config):
476
479
# Establish a connection to the host and port specified
477
480
# Catches refused connections see:
478
481
# https://docs.python.org/2/library/errno.html
479
- if __debug__ : log_info ("~~ [CONNECT] %s" , address )
482
+ log_info ("~~ [CONNECT] %s" , address )
480
483
try :
481
484
s = create_connection (address )
482
485
except SocketError as error :
@@ -488,7 +491,7 @@ def connect(address, ssl_context=None, **config):
488
491
# Secure the connection if an SSL context has been provided
489
492
if ssl_context and SSL_AVAILABLE :
490
493
host , port = address
491
- if __debug__ : log_info ("~~ [SECURE] %s" , host )
494
+ log_info ("~~ [SECURE] %s" , host )
492
495
try :
493
496
s = ssl_context .wrap_socket (s , server_hostname = host if HAS_SNI else None )
494
497
except SSLError as cause :
@@ -514,9 +517,9 @@ def connect(address, ssl_context=None, **config):
514
517
# Send details of the protocol versions supported
515
518
supported_versions = [1 , 0 , 0 , 0 ]
516
519
handshake = [MAGIC_PREAMBLE ] + supported_versions
517
- if __debug__ : log_info ("C: [HANDSHAKE] 0x%X %r" , MAGIC_PREAMBLE , supported_versions )
520
+ log_info ("C: [HANDSHAKE] 0x%X %r" , MAGIC_PREAMBLE , supported_versions )
518
521
data = b"" .join (struct_pack (">I" , num ) for num in handshake )
519
- if __debug__ : log_debug ("C: b%r" , data )
522
+ log_debug ("C: b%r" , data )
520
523
s .sendall (data )
521
524
522
525
# Handle the handshake response
@@ -531,15 +534,15 @@ def connect(address, ssl_context=None, **config):
531
534
log_error ("S: [CLOSE]" )
532
535
raise ProtocolError ("Connection to %r closed without handshake response" % (address ,))
533
536
if data_size == 4 :
534
- if __debug__ : log_debug ("S: b%r" , data )
537
+ log_debug ("S: b%r" , data )
535
538
else :
536
539
# Some other garbled data has been received
537
540
log_error ("S: @*#!" )
538
541
raise ProtocolError ("Expected four byte handshake response, received %r instead" % data )
539
542
agreed_version , = struct_unpack (">I" , data )
540
- if __debug__ : log_info ("S: [HANDSHAKE] %d" , agreed_version )
543
+ log_info ("S: [HANDSHAKE] %d" , agreed_version )
541
544
if agreed_version == 0 :
542
- if __debug__ : log_info ("~~ [CLOSE]" )
545
+ log_info ("~~ [CLOSE]" )
543
546
s .shutdown (SHUT_RDWR )
544
547
s .close ()
545
548
elif agreed_version == 1 :
0 commit comments