473,771 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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] "Subclassin g 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(_so ck=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).__in it__. 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 1451

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

Similar topics

9
4964
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
3
1777
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 connect to database xxxx. However, the characters are gibberrish. At first I thought that my problem was that I forgot to give the user name also (seeing there was no apache user in postgresql). However that wasn't it. To make sure though, I...
11
3478
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
1379
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
2156
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 input and should make best use of availbale storage (I suppose it means dont just declare a big char array). I have made a solution that seems to be basically working (tail -1 doesnt work, I'll try to fix that later), but I'm having trouble...
4
3901
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 worked so far. Thanks for any help!
0
2587
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 WebMethods as the platform that receives these postings. I don't have any experience with that technology. The sample below is the from test app that I put together to post the XML (cXML Order Requests) documents. These documents, at least my
6
1300
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 clients. But they just don't suit me! 1. I don't know how to link to winsock. 2. The codes are extreemly long for many of the source codes I find. 3. They go over what i need. For the number 3, I'll explain, I just want like something that you...
2
1738
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 just to make sure. At work on my corporate network everything works perfectly but it seems once anyone uses the client on a direct high speed connection to the internet the incoming text seems to all get jumbled up! This is using...
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.