"""
Compatability module to provide backwards compatability
for useful Python features.
This is mainly for use of internal Twisted code. We encourage you to use
the latest version of Python directly from your code, if possible.
"""
import sys, types, socket, struct, __builtin__, exceptions
if sys.version_info[:3] == (2, 2, 0):
__builtin__.True = (1 == 1)
__builtin__.False = (1 == 0)
def bool(value):
"""Demote a value to 0 or 1, depending on its truth value
This is not to be confused with types.BooleanType, which is
way too hard to duplicate in 2.1 to be worth the trouble.
"""
return not not value
__builtin__.bool = bool
del bool
if not hasattr(socket, 'inet_pton'):
def inet_pton(af, addr):
if af == socket.AF_INET:
parts = map(int, addr.split('.'))
return struct.pack('!BBBB', *parts)
elif af == getattr(socket, 'AF_INET6', None):
parts = addr.split(':')
elide = parts.count('')
if elide == 3:
return '\x00' * 16
elif elide == 2:
i = parts.index('')
parts[i:i+2] = ['0'] * (10 - len(parts))
elif elide == 1:
i = parts.index('')
parts[i:i+1] = ['0'] * (9 - len(parts))
parts = [int(x, 16) for x in parts]
return struct.pack('!HHHHHHHH', *parts)
else:
raise socket.error(97, 'Address family not supported by protocol')
def inet_ntop(af, addr):
if af == socket.AF_INET:
parts = struct.unpack('!BBBB', addr)
return '.'.join(map(str, parts))
elif af == getattr(socket, 'AF_INET6', None):
parts = struct.unpack('!HHHHHHHH', addr)
return ':'.join([hex(x)[2:] for x in parts])
else:
raise socket.error(97, 'Address family not supported by protocol')
socket.inet_pton = inet_pton
socket.inet_ntop = inet_ntop
if sys.version_info[:3] in ((2, 2, 0), (2, 2, 1)):
import string
def lstrip(s, c=string.whitespace):
while s and s[0] in c:
s = s[1:]
return s
def rstrip(s, c=string.whitespace):
while s and s[-1] in c:
s = s[:-1]
return s
def strip(s, c=string.whitespace, l=lstrip, r=rstrip):
return l(r(s, c), c)
object.__setattr__(str, 'lstrip', lstrip)
object.__setattr__(str, 'rstrip', rstrip)
object.__setattr__(str, 'strip', strip)
import types, socket, struct
True = True
False = False
bool = bool
dict = dict
StopIteration = StopIteration
iter = iter
isinstance = isinstance