sockets don't play nice with new style classes :(

P

Paul Rubin

Just a slight rant, I think I can find a workaround.

I wanted to trace all the output being sent through a socket:

from socket import *
sock = socket()
socket.connect((host, post))
socket.send('hello over there\n') # I want to log the string

Sounds like a job for new style classes:

class wsocket(socket):
def send(self, msg):
print 'socket sending (%s)'% repr(msg)
socket.send(msg)

sock = wsocket()
sock.connect(...)
# other stuff same as before

However, my print statement never got run.

After much head scratching it turns out that the library socket class
really makes a wrapper for an internal _socket.socket object. It goes
and manually sets the send and recv instance variables to the ones
from the _socket.socket, and does similar things for some other methods
and the doc strings.

It seems to me that the socket module itself should be rewritten to use
new style classes, so that socket.socket objects can extend _socket.socket
instead of wrapping them.

Am I missing something?
 
S

Skip Montanaro

Paul> It seems to me that the socket module itself should be rewritten
Paul> to use new style classes, so that socket.socket objects can extend
Paul> _socket.socket instead of wrapping them.

Paul> Am I missing something?

Probably not. The socket module could use some attention.

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top