Type problem on Windows

Moshe Zadka moshez at math.huji.ac.il
Sat Jun 10 13:09:30 EDT 2000


On Sat, 10 Jun 2000, Jerome Chan wrote:

> I am using Pythonwin with win32-129, MacPython 1.5.2c1, and Linux Python 
> 1.5.2.
> 
> While working on some socket stuff, I check the input parameters to some 
> function which expects a socket. The code I use is:
> 
> def sendData(self,data,s):
>   if type(s)!=socket.SocketType:
      ^^^^^^^^^

The answer is yet again "don't do that". Never ever check type sanity in
Python. If the object is good enough, your function will work with it. And
if it isn't, well, it will raise a TypeError or AttributeError itself.
Polymorphism rulez!

> On Unix and Mac, this works. On Win98, this bombs.
> 
> I get the following Exception Message
> 
> Windows98 gives me <type 'instance'>
> Unix and Mac gives me <type 'socket'>

Because Win98 apparently has an emulation layer so sockets can be used
portably. 

> Is there a workaround for this? And should this be reported as a bug?

Yes: just don't type-check
No: the socket module only promises to give you objects with the right
interface.

--
Moshe Zadka <moshez at math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com





More information about the Python-list mailing list