whats this mean?

Robert Amesz rcameszREMOVETHIS at dds.removethistoo.nl
Sun Jul 22 13:41:29 EDT 2001


TheDustbustr wrote:

> # BEGIN CODE BLOCK
> try:
>     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>     s.bind((HOST, PORT))
>     s.listen(1)
>     conn, addr = s.accept()
> except socket.error, why: ## reference 1
>     print "Server Error: " + errno.errorcode[why[0]] + ", " +
>     why[1] ## 
> reference 2
>     sys.exit(1)
> # END CODE BLOCK
> 
> what does reference 2 mean?  what do those brackets do?  And for
> reference 1, what will 'why' hold if an error occurs?

I don't know where this code comes from, but it contains an error: 
why[1] does *not* necessarily exist. Specifically, this happens for the 
error ('host not found',) [*]. Also, in that case 
errno.errorcode[why[0]] will not exist either, as why[0] will not be an 
error number but a string.

Presumably, having the socket.error object emulate a tuple is done for 
reasons of backward compatibility, but wouldn't it be better to make 
those available as normal attributes of the socket.error class? The use 
of why[0] and why[1] could be (gradually) replaced by, for instance, 
why.number and why.message. The advantages are:

 - Fewer questions like the one above. The code would be largely self
   explanatory.
 - The ('host not found',) anomaly could be removed from the new
   accessors, as would any other anomalies that I'm not aware of.
 - The old and new access methods could coexist without any conflicts
   indefinitely, and thus no old code would be broken. 

Any comments?


Robert Amesz
-- 
[*] At least under Windows, using Python 2.1. Socket errors represent 
themselves as if they were tuples, usually of the form (<number>, 
'<message>').



More information about the Python-list mailing list