472,958 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

The trouble with sockets.... (fixing inheritance, etc.)

This topic has been addressed in limited detail in other threads:

[1] "sockets don't play nice with new style classes :(" May 14 2005.
http://groups.google.com/group/comp....849013e37c995b

[2] "Subclassing socket" Dec 20 2005 - Jan 14 2006.
http://groups.google.com/group/comp....581b9ee5e7ceaf

Briefly, the socket module ("socket.py") provides a wrapper for python
extension code ("_socket.so","Modules/socketmodule.*" in the source)
that is built on top of the system socket library ("socket.h",etc).
This wrapper attempts to provide a SocketType we can instantiate and
treat as a typical Python object. It does this by forwarding some of
its function calls (send, recv, etc.) to an object that it references
as self._sock. This "delegation" pattern has been used in the past to
allow built-in types to be treated like normal types.

The problem is that, in this instance, delegation fails to maintain the
inheritance behavior that we expect from normal types. (See [1] and [2]
for examples.) My question is: What can be done about this?

The issue is somewhat complicated. It seems to me that the crux of the
problem is that sockets can spawn other sockets via the .dup and
..accept methods. These calls pass right down to the system level and
return references to objects of the built-in type (ie, _socket.socket)
to interpreted code. All the interpreted code can do it try to "cast"
(in the sense of Java) the object to a non-built-in type. If "newsock"
is a reference to a _socket.socket object, the socket module does this
by calling (notionally) "SocketType(_sock=newsock)". The new object of
type SocketType delegates to the object of type _socket.socket via
self._sock. Please see the source code for a clearer picture.

Now I'll have a little chat with myself.

Q1) Can't we just have _socket.socket.dup and _socket.socket.accept
return something of type(self) rather than type _socket.socket?
A1) I don't think you can "tell" the compiled code about what "type" of
PyObject to return. Maybe the Python gods will bless us with a complete
answer here.

Q2) Ok, so it looks like we are stuck with delegation at some level or
another then, huh?
A2) Well, yeah, but in some sense Python is all about delegating data
and operations to compiled code. It's just a matter of how and where.
We want to maintain a clear object model in the interpreted code and
push the dirty work down to compiled code. The problem is that in the
case of the socket module, the "how and where" are not obvious because
of the way the accept and dup system calls work.

Q3) Are you saying that the way that sockets work is fundamentally
different than the way other built-in objects work?
A3) Yeah, I guess I am. There aren't really any other built-ins that
can reach down to compiled code and spawn themselves like that. When
stuff like that happens in Python, it is usually handled with a copy,
deepcopy, or some kind of factory function. Sockets just do some things
that are hard to shoe-horn into a standard Python idiom.

The easiest solution to this problem is probably to fix the delegation
mechanism so that it transparently supports inheritance. The .accept
and .dup methods would pass a _socket.socket object into
type(self).__init__. Some __getattribute__ magic would likely be
involved. It's not immediately obvious to me how to do this, but my
intuition is that it can be done fairly painlessly.

The ideal solution is to redesign both the _socket and socket modules
so that we don't have to resort to delegation. However, due to the
argument that I have given, this will likely result in an interface
change. Interface changes are painful, yes, but at least things become
more pythonic.

What does the peanut gallery think? How should we go about fixing this
problem?

Jan 9 '07 #1
0 1385

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: umagnum338 | last post by:
Hi, I'm having some troubles getting pg_connect() to connect over UNIX domain sockets. Something that really confuses me is the error string being served up by apache. It says it was unable to...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
9
by: zippy747 | last post by:
I would like to have a base class that does some work and can be instantiated. class a { public: virtual int foo() { return 0; }; virtual int bar() { return 0; }; };
7
by: slashdotcommacolon | last post by:
Hello, I'm working on the exercises from k&r, exercise 5-13 is to implement a simple replacement for the unix tail command. The brief says it should be able to cope no matter how unreasonable the...
4
by: ingoweiss | last post by:
Hi, I am having trouble passing parameters of a Javascript subclass constructor through to it's superclass constructor. I am trying all sorts of things, including the below, but nothing...
0
by: joshblair | last post by:
Hello, I am trying to post XML documents to a third party using the HttpWebRequest. This URL uses HTTPS (SSL) but I don't have a client certificate to deal with. Apparently they are using...
6
by: Braindrool12 | last post by:
Hello, of course i'm still having troubles with sockets... Though for the forum leaders, yes i put a post that my old thread was done! But let me get to the point! I see a lot of "simple" server and...
2
by: marcf | last post by:
Hello Everyone! I have so nearly finished a mud client for a customer to the point where I was about to send them the source code. Out of luck I took the project home to try it on my personal PC...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.